File Retention Script - batch-file

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
)

Related

Batch zipping multiple files in single directory

I need to make batch script, which will work this way:
There is a folder with one hundred files. I need to make a script which is zipping every 4 files into single archive. Then all created archives must be zipped into one, big archive file. That must be all done by one script.
I tried to make it, but I haven't idea how to make it this way.
This is what I wrote first:
#ECHO ON
SET SourceDir=C:\Users\Ridaan\Documents\sobol
SET DestDir=C:\folder\Destination
CD /D "C:\Program Files\7-Zip"
FOR /F "TOKENS=*" %%F IN ('DIR /B /A-D "%SourceDir%"') DO (
7z.exe a "%DestDir%\%%~NF.zip" "%SourceDir%\%%~NXF"
)
EXIT
And second:
#echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
robocopy C:\folder\Destination /COPY:DAT /V /XO /NJH /NP /R:1000 /W:10
7z u -mx9 "C:\folder\End.7z" "C:\folder\Destination"
rmdir C:\folder\End\ /Q /S
:END
pause
Just to give you an idea of counting files and
calculating the int of count/4 for the zip number
the modulus%4 for the file number (0..3) to determine if it's a new zip to create or to append to the zip.
:: Q:\Test\2018\11\11\SO_53251220.cmd
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
set Cnt=0
Set "SrcDir=X:\Your\Path"
For /F "delims=" %%A in ('Dir /B/A-D "%SrcDir%\*"') do (
Set /A "File=Cnt%%4,Zip=Cnt/4,Cnt+=1"
Echo Cnt=!Cnt! File=!File! Zip=!Zip! File=%%A
)

Batch script to delete files in folder that does not contain certain word

I am new to batch scripting . I need to delete all files in a folder that DOES NOT contains some word in the file
found this code
#echo off
setlocal
pushd C:\Users\admin\Desktop\bat
findstr /ip /c:"importantWord" *.txt > results.txt
popd
endlocal
So how i can WHITE list this files, and delete all other?
Or i think there is easy way with just check if !contains and delete
but i don`t know how?
Supposedly, this problem could be solved in a very simple way combining these findstr switches: /V that show results when the search string is not found, and /M that show just the name of the files; that is:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
for /F "delims=" %%a in ('findstr /ipvm /c:"importantWord" *.txt') do del "%%a"
Unfortunately, the combination of /V and /M switches don't properly work: the result of /V is based on lines (not files), so a modification in the method is needed:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
rem Create an array with all files
for %%a in (*.txt) do set "file[%%a]=1"
rem Remove files to preserve from the array
for /F "delims=" %%a in ('findstr /ipm /c:"importantWord" *.txt') do set "file[%%a]="
rem Delete remaining files
for /F "tokens=2 delims=[]" %%a in ('set file[') do del "%%a"
This method is efficient, particularly with big files, because findstr command report just the name of the files and stop searching after the first string match.
#echo off
setlocal
set "targetdir=C:\Users\admin\Desktop\bat"
pushd %targetdir%
for /f "delims=" %%a in ('dir /b /a-d *.txt') do (
findstr /i /p /v /c:"importantWord" "%%a" >nul
if not errorlevel 1 echo del "%%a"
)
popd
endlocal
Not really sure what you want to do with /pfiles - files containing non-ansi characters appear to return errorlevel 1for these. if not errorlevel 1 will echo the files that do not contain the required string - remove the echo to actually delete the file(s)
This should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "pathToFolder=C:\FolderToEmpty"
SET "wordToSearch=ImportantWord"
FOR /F "tokens=*" %%F IN ('dir %pathToFolder% /b *.txt') DO (
findstr /IP %wordToSearch% "%pathToFolder%\%%F">nul
IF !ERRORLEVEL!==1 (
DEL /Q "%pathToFolder%\%%F"
)
)
You will have to set the proper path to the folder you want to delete the files from and to replace ImportantWord with the substring you are looking for.

Delete the oldest file from the files in the folder and subfolders

setlocal
set Folder=C:\test\
set FileMask=*.*
set OldestFile=
for /f "delims=" %%a in ('dir /b /o:d "%Folder%\%FileMask%" 2^>NUL') do (
set OldestFile=%%a
goto Break
)
:Break
if "%OldestFile%"=="" (
echo No files found in '%Folder%' matching '%FileMask%'!
) else (
echo del "%Folder%\%OldestFile%"
)
pause
Here I delete the oldest file (the file in the folder or the directory in the folder). How to delete the oldest file from the files in the folder and subfolders? It is necessary that the subfolders be checked.
#echo off
setlocal enableextensions disabledelayedexpansion
set "folder=C:\test\"
set "fileMask=*.*"
set "oldestFile="
for %%f in ("%folder%\.") do for /f "tokens=2,*" %%a in ('
robocopy "%%~ff" "%%~ff" %fileMask% /njh /njs /nc /ns /l /is /ndl /ts /s
^| 2^>nul sort
^| find ":"
') do set "oldestFile=%%b" & goto :done
:done
echo "%oldestFile%"
This uses robocopy to list /l the files with time stamp in yyyy/mm/dd hh:nn:ss format, sort the list so the oldest file is the first and once the information of the first file has been retrieved, show it.

Batch file - copy two last modified files

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

Batch file: sorting folders and files alphabetically

The batch file below recursively echos files and folders while adding some simple formatting, such as indenting to show the recursion depth, adding "/ " before folder names, "*" before certain files, and skipping folders named "Archive". It works great except that files and folders are sorted randomly, rather than alphabetically. How could this be changed to sort both files and folders alphabetically?
#echo off
setlocal disableDelayedExpansion
pushd %1
set "tab= "
set "indent="
call :run
exit /b
:run
REM echo the root folder name
for %%F in (.) do echo %%~fF
echo ------------------------------------------------------------------
set "folderBullet=\"
set "fileBullet=*"
:listFolder
setlocal
REM echo the files in the folder
for %%F in (*.txt *.pdf *.doc* *.xls*) do echo %indent%%fileBullet% %%F - %%~tF
REM loop through the folders
for /d %%F in (*) do (
REM skip "Archive" folder
if /i not "%%F"=="Archive" (
REM if in "Issued" folder change the file bullet
if /i "%%F"=="Issued" set "fileBullet= "
echo %indent%%folderBullet% %%F
pushd "%%F"
set "indent=%indent%%tab%"
call :listFolder
REM if leaving "Issued folder change fileBullet
if /i "%%F"=="Issued" set "fileBullet=*"
popd
))
exit /b
Very little change required. Convert FOR loops to FOR /F running sorted DIR commands. The /A-D option lists files only, and /AD lists directories only.
This version sorts files by name
#echo off
setlocal disableDelayedExpansion
pushd %1
set "tab= "
set "indent="
call :run
exit /b
:run
REM echo the root folder name
for %%F in (.) do echo %%~fF
echo ------------------------------------------------------------------
set "folderBullet=\"
set "fileBullet=*"
:listFolder
setlocal
REM echo the files in the folder
for /f "eol=: delims=" %%F in (
'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* 2^>nul'
) do echo %indent%%fileBullet% %%F - %%~tF
REM loop through the folders
for /f "eol=: delims=" %%F in ('dir /b /ad /one 2^>nul') do (
REM skip "Archive" folder
if /i not "%%F"=="Archive" (
REM if in "Issued" folder change the file bullet
if /i "%%F"=="Issued" set "fileBullet= "
echo %indent%%folderBullet% %%F
pushd "%%F"
set "indent=%indent%%tab%"
call :listFolder
REM if leaving "Issued folder change fileBullet
if /i "%%F"=="Issued" set "fileBullet=*"
popd
))
exit /b
To sort by extension first, then by name, simply change /ONE to /OEN.
Try changing your for /d loop from
for /d %%F in (*) do
to
for /f "delims=" %%F in ('dir /b /o:n *.') do
and see whether that makes a difference. Actually, ordering by name is the default behavior for dir, so you could probably get away with
for /f "delims=" %%F in ('dir /b *.') do
If some of your directory names have dots in them, you'll need to change it a little.
for /f "delims=" %%F in ('dir /b') do (
rem Is this a directory?
if exist "%%F\" (
rem do your worst....
)
)

Resources