I'm trying to write a batch file that will be run on a parent directory and will loop through subdirectories removing all but the newest 3 files from each subdirectory. What I have now recurses through the subdirectories but only skips the 3 newest files that it comes across, not the three newest files in each subdir. I think I need another loop in the code, but need help with where and what it should be. Help!
What I have so far - just ECHOing the output for now as a test.
#echo off
pushd "%~1"
for /f "skip=3 delims=" %%F in (
'dir /s /a-d /o-d /b') do ECHO del "%%F" /f
popd
You might try this:
#echo off
pushd "%~1"
for /D %%i in (*) do (
pushd "%%~i"
for /f "skip=3 delims=" %%F in (
'dir /a-d /o-d /b') do ECHO del /f "%%~F"
popd
)
popd
Related
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
)
I would like to write a .bat file to move the two last modified files of a specific extension *.bak in directory a to a different directory.
I used this line to copy files:
robocopy D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups *.bak /S
I'm new with this and have no idea how to tweak this to get the result I need.
Thanks
not tested:
#echo off
for /f "tokens=* delims=" %%# in (' dir /a:-d /o:-d /t:a /b "D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups\*.bak"') do (
if not defined last set "pre_last=%%~f#"
set "last=%%~f#"
)
copy /y "%last%" "c:\new_dir"
copy /y "%pre_last%" "c:\new_dir"
#echo off
setlocal EnableDelayedExpansion
cd "D:\DailyBackup\IDMRObjects\SQLBackups"
set copied=0
for /F "delims=" %%a in ('dir /B /A-D /O-D /T:W *.bak') do (
copy "%%a" "other\dir"
set /A copied+=1
if !copied! equ 2 goto break
)
:break
I have a window directory with 300 folders inside of it and 4 levels of folders within each folder. I need a listing of folders only up to 2nd level in a txt file? I need the full path of each directory as well. I would like to use this a BAT file or CMD line prompt. Ex:
Jon Done
Test001
Tester002
Test003
Tester004
Can anyone help me to do this?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR /f "delims=" %%a IN ('dir /b /a-d 2^>nul') DO ECHO %%~fa
FOR /f "delims=" %%a IN ('dir /b /ad') DO (
PUSHD "%%a"
FOR /f "delims=" %%x IN ('dir /b /a-d 2^>nul') DO ECHO %%~fx
popd
)
popd
GOTO :EOF
This should work - you would need to change the setting of sourcedir to suit your circumstances.
I'm triying to get a list of files of subfolders in a inverse order using this:
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f)
I'm getting this result
folder1\file2, folder1\file, folder2\file2, folder2\file1, folder3\file2, folder3\file1
And I want to get the reverse order in folders and files, not only files of folders. Something like this
folder3\file2, folder3\file1, folder2\file2, folder2\file1\, folder1\file2, folder1\file1
How can I do this?
#echo off
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f >> tempfile.123)
C:\Windows\System32\sort.exe /R tempfile.123
del tempfile.123
This just echoes your files to tempporary file and then revereses it. Sorry for the full path to sort.exe but I have cygwin installed. In case you want proper temporary file name I reccomend this http://unserializableone.blogspot.com/2009/04/create-unique-temp-filename-with-batch.html
try this
for /f "tokens=*" %%f in ('dir /s /b /o-n')
do (
SET OUTPUT=%OUTPUT%, %%f
)
echo %OUTPUT%
run a first loop to get first level subdir then run you command on each of them
#echo off
for /f "tokens=*" %%f in ('dir c:\temp\so\ /b /o-n /AD') do (call :filesOf %%f)
:next
echo next
pause
:filesOf
echo "files for ******** %1 *********"
if ("%1"=="") goto next else(
for /f "tokens=*" %%i in ('dir c:\temp\so\%1 /s /b /o-n ') do echo "***%%i***"
)
it will be difficult to handle multi level subdirs tough
I need to copy files from recent build folder to another folder used for testing. I'm having a hard time getting the name of the most recent build folder.
My current attempt is this:
#for /D %%i in ('dir e:\builds\projectA\* /O:D') do set target=%%i
echo %target%
xcopy "%target%\*.*" \\devbox\projectA /y /s
I was hoping target would be the newly created folder from which I could then copy the files from. However, when I echo target to the console it just says:
/O:D'
Does anyone know how I can get this to work (or know of an alternative)?
Replace the /D with /F and add /B to the bracketed dir command.
#for /F %%i in ('dir e:\builds\projectA\* /O:D /B') do set target=%%i
echo %target%
xcopy "%target%\*.*" \\devbox\projectA /y /s
pushd E:\builds\projectA
for /f "delims=" %%d in ('dir /b /a:d /o:d') do #echo %%d>latest.txt
for /f "delims=" %%l in (latest.txt) do xcopy "%%l\*.*" \\devbox\projectA /y /s
del latest.txt
popd