Moving the 10 oldest Backups to archive using Batch - windows

i looking for an solution using a Batchfile to do the following.
I do every day a backup of my databases in an folder, which named after the day eg.
2013-05-13
. This is already fine.
Now the Problem:
At the end of a month i want to combine the oldest 10 folders (including all files) to an archive folder.
All files from the 10 oldest folders should moved to the archive folder. Same files should overwritten (oldest to new) and the folder (now empty) should also removed.
Best Regards

This should move the contents of the oldest 10 folders to an archive folder (which should pre-exist), overwriting and keep the latest copies, and remove the folders: but it doesn't expect subdirectories in the backup folders.
After you test it you can schedule the batch file.
#echo off
setlocal enabledelayedexpansion
cd /d "d:\source\folder"
set "c=0"
for /f "delims=" %%a in ('dir /ad /b /od') do (
set /a c=c+1
if !c! LEQ 10 (
echo "%%a"
move /y "%%a\*.*" "D:\destination\archive\" >nul
rd "%%a"
)
)
pause

The easiest way would be to install UnixUtils to get mv, tail and head utilities
Then
dir /b /AD /OD|tail -n 10 > temp.txt
gives you list of 10 newest directories in current one
for /f "delims=" %%i in (temp.txt) do echo D|mv -f "c:\source\%%i" "c:\destination\%%i" /y >temp.bat
creates script moving them somewhere
call temp.bat
calls it.

Related

Batch Script to delete only files with .zip extension

I want to skip the latest 3 files created and delete the rest of the files. What I need is that if there is text, xml and zip files we need to delete only the zip files and leave the text and xml files behind and the total files left should be 3.If there are 3 or more files other than .zip files, delete all .zip files; if there are less, keep the newest .zip files so that there are 3 files left in total. Can anyone help. I am stuck with this
For example(inside bracket created date of files):
Folder A contains - aa.txt(2/1/18), bb.xml(3/1/18), cc.zip(4/1/18), dd.zip(2/1/18),ee.zip(5/1/18)
What I need after deleting is
aa.txt, bb.xml, ee.zip
This is what I have written
#ECHO OFF
SETLOCAL
SET "targetdir=C:\source"
SET /a retain=3
FOR /f "skip=%retain%delims=" %%a IN (
'dir /b /a-d /o-d "%targetdir%\*.zip" '
) DO DEL (DEL "%targetdir%\%%a.zip"
GOTO :EOF
Given the parameters of your question, a very simple method, although not the most efficient or speedy, would be to do this:
#Echo Off
Set "targetdir=C:\source"
Set "retain=3"
CD /D "%targetdir%" 2>Nul || Exit /B
For /F "Skip=%retain% Delims=" %%A In ('Dir /B/A-D/O-D/TC'
) Do If /I "%%~xA"==".zip" Del "%%A"

A batch file that will delete the newest folder if there's more than 10

I want to create a batch file (.bat) that can delete the newest folder in a folder of folders if there's more than 10 folders. If this is the directory tree:
\My Folders\
... \Old Videos\ (Created 2 Hours Ago)
... \Files\ (Created Last Month)
... \Pics\ (Created Last Week)
... \Powerpoints\ (Created Yesterday)
... \Videos\ (Created Several Days Ago)
... \Old Pics\ (Created Yesterday)
... \Movies\ (Created Yesterday)
... \Games\ (Created Yesterday)
... \Old Files\ (Created Last Month)
... \Downloads\ (Created Two Weeks Ago)
... \New Folder\ (Created Just Now)
So this batch file, when run, will delete New Folder (at the bottom) because there is more than 10 files in the folder (excluding subfolders), and it is the newest. If there was 12 folders in the directory tree (again, minus subfolders), the batch file would delete the 2 newest, and so on.
Is there a way to do this in Win10? I've messed around with it a bit, but haven't been able to do it.
dir has a lot of useful switches:
/ad to show folders only (no files)
/od to sort by date (oldest first)
/tc to take "created" date-time (default: "last-changed")
/b to show just the names.
Put a for loop around, skip the first 10 lines and you're done:
#echo off
cd "My Folders"
for /f "skip=10 delims=" %%a in ('dir /ad /od /tc /b') do ECHO rd /s /q "%%a"
remove the ECHO if the output is what you want.
The following batch script will do what you've asked for:
Usage: batch.bat "path\folder with spaces" 11
If it is okay with you, you can remove the echo in front of the rd command. You also could extend that to print the list first and then aks the user for confirmation to delete them.
This batch uses the dir command with the /O:D option to sort for dates (oldest first), /A:D option to show directories only, the /TC option to use the creation date and the /B to just get the names.
The dir command uses the last written time as default, if you want to change that use the /T switch (described here on technet):
/t[[:]<TimeField>]
Specifies which time field to display or use for sorting. The following list describes each of the values you can use for TimeField:
c Creation
a Last access
w Last written
#echo off
setlocal enabledelayedexpansion
rem Check if 2 arguments are passed
set INVARGS=0
if [%1] == [] set INVARGS=1
if [%2] == [] set INVARGS=1
if %INVARGS% == 1 (
echo Usage: %0 ^<folder^> ^<count^>
goto :EOF
)
set "folder=%~1"
set "count=%~2"
if not exist "%folder%" (
echo The directory does not exists:
echo "%folder%"
echo The program will exit.
goto :EOF
)
pushd "%folder%"
set idx=1
rem dir command with date sort just directories
for /f "delims=" %%F in ('dir /O:D /A:D /TC /B') do (
if !idx! GEQ %count% (
echo rd /s /q "%%F"
)
set /a idx=!idx!+1
)
popd

Copy and rename multiple files in multiple directories with windows CMD line

I want to copy and rename files that are stored in a stack of directories (year, month, day). The files are .ers files and I need them to maintain their extension but to go from file.ers to file_cloud.ers.
Then I would like the files for a given month to be saved in the cloud folder within each month.
Ideally I would do this in the Windows Command Line.
I understand a batch file is a good place to start, and so I have the following - this doesn't include saving in a different location - but I am struggling with just the copy and rename.
for /D %%y in ("C:\Data\20*") do (
pushd "%%~y"
for /D %%m in ("*") do (
pushd "%%~m"
for /D %%d in ("*") do (
pushd "%%~d"
for %%f in (soig_*.ers) do copy "%%~f.ers" "%%~f_cloud.ers"
popd
)
popd
)
popd
)
This might be easier than you are making it. If this echos the correct copy command, remove the echo. You get this by sticking your head way down into the output of FOR /?.
FOR /F "usebackq tokens=*" %%f IN (`DIR /S /B /A:-D "*.ers"`) DO (
echo COPY "%%~f" "%%~dpnf_cloud.%%~xf"
)
The following should work okay for you, (it is supposed to copy all matching .ers files with updated names to the existing cloud directory at the previous level of their tree).
#FOR /D %%Y IN ("C:\Data\20*") DO #FOR /D %%M IN ("%%Y\*"
) DO #FOR /D %%D IN ("%%M\*") DO #IF /I NOT "%%~nxD"=="cloud" FOR %%F IN (
"%%~D\soig_*.ers") DO #COPY "%%F" "%%~M\cloud\%%~nFcloud%%~xF">NUL
I have not included anything to prevent overwrites of files whose names already exist within that cloud directory

Batch file that deletes all but the last 10 per file name

I have multiple folders with multiple files with the same name and then a timestamp. Example:
foo_2017.01.16.png
foo_2017.01.15.png
foo_2017.01.14.png
bar_2017.01.16.png
bar_2017.01.15.png
bar_2017.01.14.png
file_2017.01.16.png
file_2017.01.15.png
file_2017.01.14.png
What I'm needing to do is create a batch file that will parse out only the name before the _timestamp and keep the last 10 of each file name. I'm not very well versed in batch and could really use some help here. The Date Modified for every file is the same, so I cannot just say keep only the last 10 days, otherwise I could handle that.
EDIT:
I have a batch file that deletes files in a specific folder older than 28 days that looks like the following, I just do not know how to modify it to iterate through for each file name specifically and to not go by Date Modified:
/c echo #FILE
forfiles -p "C:\workspace\SeleniumTestResults" -s -m *.* /D -28 /C "cmd /c del #path"
Again, this batch is definitely not my area
#echo off
setlocal
REM create some files for testing:
break>foo_2017.01.16.png
break>foo_2017.01.15.png
break>foo_2017.01.14.png
break>bar_2017.01.16.png
break>bar_2017.01.15.png
break>bar_2017.01.14.png
break>file_2017.01.16.png
break>file_2017.01.15.png
break>file_2017.01.14.png
dir *.png
set keep=2
for /f "delims=_" %%a in ('dir /b *.png') do (
for /f "skip=%keep% delims=" %%b in ('dir /b /o-n "%%~a*.png"') do del "%%b"
)
dir *.png

Searching for string in multiple zip archives

Here's what I have;
On a windows-based web server there are roughly 1,000 zip files, each with dozens of log files inside. I already have a script that goes through each archive and deletes all but one specific file type (in an attempt to save diskspace and delete things I don't need). Then, the script unzips each archive to their own folder. And I know how to code the reverse of that to zip them back when I'm done.
Here's what I need to figure out;
Once I run the previously mentioned script (we call it garbageman because it cleans out the garbage in the zip files) I need to go through the remaining 5 or 6 files in each of the newly created unzipped folders, and look for a specific string in each file. If I find the string, I delete everything that is not that string, and save it to a file called "export.txt" in that folder. Then, I move to the next unzipped file, and so on. Once completed, I need re-zip everything back together into their own archives
Here's what I have for code so far. Any help is extremely appreciated.
cd "C:\Program Files\7-Zip"
FOR %%c in (C:\Users\xxxxxx\Desktop\LogQueue\*.*) DO 7z d %%c "-x!xstore*" -r
FOR /R "C:\Users\xxxxxx\Desktop\LogQueue" %%I in ("*.zip") do (
"%ProgramFiles%\7-Zip\7z.exe" x -y -o"%%~dpnI" "%%~fI"
)
cd "C:\Users\xxxxxxx\Desktop\LogQueue"
FOR /R "C:\Users\xxxxxx\Desktop\LogQueue" %%I in ("*.*") do (
findstr "xxxxxxxx_eReceipt" %%~fI > %%~dpnI\export.txt
pause
)
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\"
This is an edit:
This script should do what you want.
Only Change sourcedir and mystring variables.
export.txt will be inside a file called Storage in the root directory of the batch.
:ScriptA
#ECHO ON
MKDIR "%CD%\Storage"
MKDIR "%USERPROFILE%\Desktop\Outx"
GOTO :ScriptB
:ScriptB
::REM ONLY CHANGE
SET "sourcedir=%USERPROFILE%\Desktop\Test"
FOR /R "%sourcedir%\" %%a in (*.txt) do copy "%%a" "%CD%\Storage"
:ScriptC
:ScriptC
#ECHO OFF
SETLOCAL
SET "VARA=%CD%\Storage"
SET "VARB=%USERPROFILE%\Desktop\Out"
::REM ONLY CHANGE
SET "mystring=PUT_STRING_HERE"
FOR %%a IN ("%VARA%\*.txt") DO FINDSTR "%mystring%" "%%a">nul&IF NOT ERRORLEVEL 1 FINDSTR "%mystring%" "%%a">"%VARB%\%%~nxa"
DEL /F "%CD%\Storage\*.txt"
GOTO :ScriptD
:ScriptD
#ECHO ON
COPY /B "%USERPROFILE%\Desktop\Outx\*.txt" "%CD%\Storage\export.txt"
RD /S /Q "%USERPROFILE%\Desktop\Outx"
goto :eof

Resources