Moving Files up one Directory - batch-file

I'm looking to make a batch script that will move all images in sub subfolders down to the layer above the batch script. For Example:
comics\move.bat
comics\series 000\series 000 blah\01.jpg,02.jpg, etc.
comics\series 001\series 001 blah\series blah\01.jpg,02.jpg, etc.
into
comics\series 000\01.jpg,02.jpg, etc.
comics\series 001\01.jpg,02.jpg, etc.
I've tried several variations of
for /r %%i in (*.jpg) do move "%%~fi" "%%~pi*.jpg"
But it won't do anything. One way I had it would move all images into the same folder as I was running the script from but that's the closest I've gotten. A clean up of the now empty folders would be nice too!

This should move the images in each branch into the first level folder, but doesn't deal with filename collisions.
Test it on a folder of copies of your files.
#echo off
for /d %%a in (*) do (
pushd "%%a"
for /r %%b in (*.jpg) do move "%%b" .
popd
)
pause
for /L %%a in (1,1,5) do for /d /r %%b in (*) do rd "%%b" 2>nul
The last line should remove only empty folders up to 5 levels deep, if they exist.

Create a .bat file with following content on the root folder and double click it.
#echo off
for /f "usebackq tokens=*" %%d in (`dir /s /b /o:n /ad`) do (
cd "%%d"
for /f "usebackq tokens=*" %%f in (`dir /b /o:n /a-d`) do (
move /y "%%f" ..
)
)
pause
To explain the code, the outer loop iterates directories recursively and cd into it. The inner loop iterates all files inside the directory and move it one up above. /y flag on move is to suppress prompting. Use dir /? for explanation of all the dir flags. As for the for part I took it from https://stackoverflow.com/a/2768660/179630

Try this:
move comics\series 000\series 000 blah\*.jpg comics\series 000

Related

Batch create a Folder in every subfolders and Move all the .jpg files in it

I made this batch file that makes a folder named Pictures and puts all the .jpg files in it.
#echo off
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
end
Is there anyway to make the script iterate through subdirectories as well? I want to run the batch file on a root directory and make it work for all subfolders.
Based upon the answer you've supplied, I would suggest you could do that like this, in a single line batch-file:
#For /D %%G In (*) Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
As an alternative, because For /D may not pick up all of your directories, (it ignores directories with the hidden and system attributes):
#For /F "EOL=? Delims=" %%G In ('Dir /B /AD') Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
Actually I just figured it out
#echo off
setlocal
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
rem enter the directory
pushd %%a
echo In Directory: %%a
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
rem leave the directory
popd
)
endlocal
It does the same thing but it works through subfolders. Took me sometime to figure it out. Anyway, hopefully it'll help others too.
Try this:
for /f %%a in ('dir /b /ad "Filepath"') do (
md "%%~fa\Pictures"
for %%b in ("*.jpg") do robocopy "%%~fb" "%%~fa\Pictures\" /mov
)

How do I move empty folders and files into a new folder?

How do I get the contents of a folder (including empty folders) to be moved (not copied) into a new folder excluding one folder?
I want to move the contents in the Entity folder into my newly created folder 2013\November Estimates that is in each entity.
The folder layout is 2013\2 - November Projections\Fund Family (32 of them, plus other folder not concerned with)\Entity (600 different entities unique to each fund family)
This is the current layout for each entity:
Entity_01
Entity_01\2013
Entity_01\2013\November Estimates
Entity_01\entity.pdf
Entity_01\entity.xls
Entity_01\entity.doc
Entity_01\PY folder
Entity_01\Trash Folder
Entity_01\2012
So all the files and folders are moved to November Estimates but the folder 2012 stays where it is (producing the following layout)
Entity_01
Entity_01\2013
Entity_01\2013\November Estimates
Entity_01\2013\November Estimates\entity.pdf
Entity_01\2013\November Estimates\entity.xls
Entity_01\2013\November Estimates\entity.doc
Entity_01\2013\November Estimates\PY folder
Entity_01\2013\November Estimates\Trash Folder
Entity_01\2012
Issues with the following command line:
FOR /f "delims=" %%b IN (entitylist.txt) DO (
pushd "%destdrive%:\%year%\%projections%\%%~a\%%~b\" && (
for /d %%c in (*) do if /i not "%%c"=="2012" move "%%c" "%estimates%"
move *.* "%estimates%"
popd
)
)
Complete BAT
#echo off
setlocal
SET "sourcedrive=Z"
SET "destdrive=C"
SET /a year=2013
SET "projections=2 - November Projections"
SET "Estimates=November Estimates"
SET "Yearend=Year End Filings"
DIR /b /ad "%sourcedrive%:\%year%\%projections%" >fundfamily.txt
start /wait "Fund Family" C:\Windows\System32\notepad.exe "fundfamily.txt"
FOR /f "delims=" %%a IN (fundfamily.txt) DO (
MD "%destdrive%:\%year%\%projections%\%%~a"
DIR /b /ad "%sourcedrive%:\%year%\%projections%\%%~a" >entitylist.txt
start /wait "%%~a entities" C:\Windows\System32\notepad.exe "entitylist.txt"
FOR /f "delims=" %%b IN (entitylist.txt) DO MD "%destdrive%:\%year%\%projections%\%%~a\%%~b\%year%\%estimates%"
FOR /f "delims=" %%b IN (entitylist.txt) DO MD "%destdrive%:\%year%\%projections%\%%~a\%%~b\%year%\%yearend%"
FOR /f "delims=" %%b IN (entitylist.txt) DO MD "%destdrive%:\%year%\%projections%\%%~a\%%~b\2012"
FOR /f "delims=" %%b IN (entitylist.txt) DO (
pushd "%destdrive%:\%year%\%projections%\%%~a\%%~b\" && (
for /d %%c in (*) do if /i not "%%c"=="2012" move "%%c" "%estimates%"
move *.* "%estimates%"
popd
)
)
)
regarding the line that produces error, if there're only one folder you want to avoid moving, you can check if the folder to move is not the one:
FOR /f "delims=" %%b IN (entitylist.txt) DO (
if "%destdrive%:\%year%\%projections%\%%~a\%%~b\2012" NEQ "%%b" MOVE /Y "%destdrive%:\%year%\%projections%\%%~a\%%~b\%all%" "%destdrive%:\%year%\%projections%\%%~a\%%~b\%year%\%estimates%"
)
Try this as your last loop as you will need a loop to move the folders one by one, and another command to move the files in the main folder.
Perhaps something like this will do it.
FOR /f "delims=" %%b IN (entitylist.txt) DO (
pushd "%destdrive%:\%year%\%projections%\%%~a\%%~b\" && (
for /d %%c in (*) do if /i not "%%c"=="2012" move "%%c" "%estimates%"
move *.* "%estimates%"
popd
)
)
This does the following (Im going to use pictures, don't know why i didnt think of that before... -_-
CURRENT RESULTS:
DESIRED RESULTS:
it basically renames the the 2013 folder in the entity and moves everything except 2012 into that folder, which now has the November Estimates name

recursive .bat file to apply jpegtran and pngout to all images in subfolders

I have this script to optimize images inside a directory, I need to make this recursive in order to optimize all images in subdirectories, leaving filenames and folder structure unchanged.. Anyone can help to modify this code in order to modify images in subdirectories too?
#echo none
cd %1
md "%~1\OptimizedJPEGS"
for %%i in (*.jpg) do "C:\imageoptimization\jpegtran.exe" -optimize -progressive -copy none "%%i" "%~1\OptimizedJPEGS\%%i"
move /Y "%~1\OptimizedJPEGS\*.*" "%~1"
rd "%~1\OptimizedJPEGS"
for %%i in (*.png) do "C:\imageoptimization\pngout.exe" "%%i"
This should work when launched in the main folder of the image tree.
Test it on some sample folders first.
#echo none
for /d /r %%a in (*) do (
pushd "%%a"
echo processing "%%a"
md "OptimizedJPEGS"
for %%i in (*.jpg) do "C:\imageoptimization\jpegtran.exe" -optimize -progressive -copy none "%%i" "OptimizedJPEGS\%%i"
move /Y "OptimizedJPEGS\*.*" .
rd "OptimizedJPEGS"
for %%i in (*.png) do "C:\imageoptimization\pngout.exe" "%%i"
popd
)

windows batch to recursively scan files in 1 level subfolders

I have many projects (let's say D:\Projects\), each of which has it's own directory. In some of directories (not all) there is bat file (sonar-runner.bat). I want to write windows batch script to run this .bat files sequentialy for each subfolders:
for /r %%i in (sonar-runner.bat) do (
#call "%%i"
pause
)
pause
However using this approach I faced problem where the same files in inner directories are executed (for example D:\Projects\Main\Inner\sonar-runner.bat). I don't want this to happen. I only want to look for file through projects folders (let's say subfolders of 1st level). Could anybody tell me how can I achieve that?
Edit
I have tried another approach:
for /D %%i in (*) do (
for %%j in (%%i\sonar-runner.bat) do (
#call "%%j"
pause
)
)
pause
But i'm not sure about performance in that case.
try this:
for /d /r "D:\Projects" %%i in (*) do (
pushd "%%~i"
echo(%%i|findstr /r "^.:\\[^\\]*\\[^\\]*$" >nul&&call sonar-runner.bat
popd
)
This is much more efficient.
#echo off
pushd "d:\folder"
for /f "delims=" %%a in ('dir /ad /b ') do (
pushd "%%a"
if exist "sonar-runner.bat" call "sonar-runner.bat"
popd
)
popd
pause

Batch File Delete multiple file in multiple folders keeping last X files in each folder

I have a problem with the script I wrote:
#echo off
e:
cd folder1
cd logs
cd App1
for /F "skip=20 delims=" %%F in ('dir /b/o-d/a-d log11101*.*') do del "%%F"
The script works well for 1 folder. It keeps the 20 latest log files, and delete all the rest of the files starting with "log11101" in the folder (because I have other log files in these folder I do not want to delete with this script...
I want to apply this script in multiple folders, but that are all in FOLDER1, using the same "for /f "skip=20..................................do del "%%F" .
I tought of doing something like this since the parent folder is FOLDER1/LOGS:
#echo off
e:
cd folder1
cd logs
cd App1
for /F "skip=20 delims=" %%F in ('dir /b/o-d/a-d log11101*.*') do del "%%F"
cd ..
cd App2
for /F "skip=20 delims=" %%F in ('dir /b/o-d/a-d log11101*.*') do del "%%F"
But it doesn't work ! It works for 1 folder, but the other, it deletes all but 1 file, instead of keeping 20 files and deleting the rest.
Would there be a way for me to put all folders I want to target with the script, something like this and run only this script once ? :
x:/folder1/logs/app1
x:/folder1/logs/app2
x:/folder1/logs/app4
for /f "skip=20...............
Thanks
I don't see how your 2nd code base only preserves 1 file in the 2nd folder - the 2nd folder should behave identically to the 1st. Are you sure the App2 folder had more than 1 log11101* file to begin with?
Recommendation - insert ECHO before your DEL command and look at the results. Once the output looks correct, then remove the ECHO to make the script functional.
It takes very little code to conveniently perform your delete action on a list of folders that reside in your root:
#echo off
for %%D in ("App1" "App2" "App4") do (
cd /d "e:\folder1\logs\%%~D"
for /f "skip=20 delims=" %%F in ('dir /b /o-d /a-d log11101*.*') do echo del "%%F"
)
If you want to perform your delete on all folders within your root:
#echo off
for /d %%D in ("e:\folder1\logs\*") do (
cd /d "%%D"
for /f "skip=20 delims=" %%F in ('dir /b /o-d /a-d log11101*.*') do echo del "%%F"
)
I've added the ECHO before DEL, so once everything looks good, remove ECHO to make it functional.

Resources