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
Related
i have batch file which need to copy 3 last modified files from 3 different sources and need to rename it by removing last 33 characters.
i made it in 2 files but from som reason when i put both codes together its not working...
my code :
#echo off
set folderpath=C:\Users\tzahi.k\Desktop\testSource\des
for /F "delims=" %%a in ('dir /b /od "C:\Users\tzahi.k\Desktop\testSource\source\*.txt"') do set Youngest=%%a
xcopy /y "C:\Users\tzahi.k\Desktop\testSource\source\%Youngest%" %folderpath%
for /F "delims=" %%a in ('dir /b /od "C:\Users\tzahi.k\Desktop\testSource\source2\*.txt"') do set Youngest=%%a
xcopy /y "C:\Users\tzahi.k\Desktop\testSource\source2\%Youngest%" %folderpath%
cd %folderpath%
for /f %%a in ('dir /b "%folderpath%\*.txt"') do (
set "fname=%%~na"
ren "%%a" "!fname:~0,-33!.txt"
)
pause
when i split the code in 2 files it works but i want it in one...
any suggestion?
i use
setlocal EnableDelayedExpansion
and it works
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 have a directory that has a 10 sub-directories. Each of these holds different .bak files. I'm trying to create a script that will check to see if X number of files are there and if the number of files exceeds X, it deletes the oldest file. In other words, I want 20 iterations of a .bak file. When the 21st one shows up, I want the batch file to delete the oldest one.
Is this possible?
If so, can I create a single script that looks in all the sub-directories?
Thanks in advance.
Two options included. The first one will leave %maxFiles% bak files under each of the folders. The second one (windows Vista or later OS is required as robocopy is used to obtain the sorted list of files) will leave %maxFiles% in total
#echo off
setlocal enableextensions disabledelayedexpansion
set "rootFolder=%cd%"
set "maxFiles=20"
rem Option 1 - Keep %maxFiles% inside each of the subfolders
for /d /r "%rootFolder%" %%z in (*) do for /f "skip=%maxFiles% delims=" %%a in (
'dir /tc /o-d /a-d /b "%%~fz\*.bak" 2^>nul'
) do echo del "%%~fz\%%~nxa"
echo ------------------------------
rem Option 2 - Keep %maxFiles% in total under all the subfolders
for /f "skip=%maxFiles% tokens=2,*" %%a in ('
robocopy "%rootFolder%" "%rootFolder%" *.bak /l /nocopy /is /s /njh /njs /ndl /nc /ns /ts
^| findstr /v /r /e /i /c:"%rootFolder:\=\\%\\[^\\]*"
^| sort /r
') do echo del "%%b"
del commands are only echoed to console. If the output is correct, remove the echo command to remove the files
assumes that you want to check the number of files from the parent directory.Can be done also for each sub-directory.
#echo off
setlocal
set "max_number_files=20"
set "parrent_dir=c:\whatever_you_need"
set "extension=.bak"
pushd "%parrent_dir%"
set "count=0"
setlocal enableDelayedExpansion
for /f "delims=" %%a in ('dir /s /b /a:-d /o:-d /t:c *%extension%') do (
set /a count=count+1
if 1!count! GTR 1!max_number_files! (
rem --- remove the echo to activate deletion
echo del /q /f "%%~a"
)
)
popd
endlocal
endlocal
This will check each folder under d:\base\folder and if there are more than 20 *.bak files it will remove the oldest ones so only 20 *.bak file remain, in each folder.
Test it on some sample folders.
#echo off
for /d /r "d:\base\folder" %%a in (*) do (
pushd "%%a"
for /f "skip=20 delims=" %%b in ('dir /b /a-d /o:-d *.bak ') do del "%%b"
popd
)
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'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