Windows批处理脚本仅将N个最新的FOLDERS保留在目录中

时间:2015-08-03 20:25:57

标签: windows batch-file directory

我有一个目录,我将MySQL dbs备份到当前日期的文件夹中。

现在30或60天后,我只想在该备份目录中保留n个最新文件夹,并删除其余文件夹,每个文件夹中都包含gzip压缩包。

对于备份MySQL dbs,我使用adityasatrio's batch script并创建包含当前日期的文件夹也是thanks to this answer

但是,为了这个例子,只保留最新的3个文件夹并删除备份目录中的所有其他文件夹似乎很难。

这就是我的尝试。

 :: deletes all files and folders in the backupDir
 :: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b /o-d') do echo del "%backupDir%\%%a"


 :: deletes only files in backupDir
 :: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b /a-d') do echo del "%backupDir%\%%a"


 :: deletes all files and folders BUT keeps 3 latest FOLDERS in backupDir
 :: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b') do echo del "%backupDir%\%%a"


 :: deletes all files and folders BUT keeps 3 latest FOLDERS in backupDir
 :: for /f "skip=3 delims=" %%a in (' dir "%backupDir%\" /o-d /b') do echo rd /S /Q "%backupDir%\%%a"

请注意我不打算删除该backupDir中可能包含的任何文件。我删除backupDir中除了最新的3个文件夹之外的所有文件夹,所以基本上删除了包含gzipped .sql文件的旧文件夹。

这些问题有所帮助,但我仍然没有以某种方式做到这一点。

delete all but X most recent folders

How do I delete old files from a directory while keeping the most recent ones on Windows(文件不是文件夹)

Batch file to delete files older than N days(同样,文件不是文件夹)

https://serverfault.com/questions/49614/delete-files-older-than-x-days  (好的措施,再次文件不是文件夹..)

此评论delete all but X most recent folders最接近我正在尝试的内容,但也会删除backupDir中可能包含的所有文件。

** edit1:**刚刚找到Delete all folders except 2 lastest folders in Windows并会看到它是否有任何帮助。

我在popd以下的谦逊努力的完整脚本就在这里。我希望在您的帮助或输入下对其进行排序以便进一步阅读。注意我对Batch Scripting完全不熟悉。对此有任何帮助非常感谢。请帮我把所有N个最新文件夹删除。 :)

     @echo off

 set dbUser=root
 set dbPassword=root
 set "backupDir=D:\MySQLDumps"
 set "mysqldump=C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump.exe"
 set "mysqlDataDir=C:\wamp\bin\mysql\mysql5.6.17\data"
 set "zip=C:\Program Files\7-Zip\7z.exe"

rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"

 set "dirname=%YYYY%-%MM%-%DD% %HH%-%Min%-%Sec%"

 echo "dirName"="%dirName%"
 :: pause

 :: switch to the "data" folder
 pushd "%mysqlDataDir%"

 :: create backup folder if it doesn't exist
 if not exist "%backupDir%\%dirName%\" mkdir "%backupDir%\%dirName%"

 :: iterate over the folder structure in the "data" folder to get the databases

 for /d %%f in (*) do (
 echo processing folder "%%f"

 "%mysqldump%" --host="localhost" --user=%dbUser% --password=%dbPassword% --single-transaction --add-drop-table --databases %%f > "%backupDir%\%dirName%\%%~nxf.sql"

 "%zip%" a -tgzip "%backupDir%\%dirName%\%%~nxf.sql.gz" "%backupDir%\%dirName%\%%~nxf.sql"

  del "%backupDir%\%dirName%\%%~nxf.sql"

 )
 popd

 :: delete all folders but the latest 3


 :: deletes all files and folders in the backupDir
 :: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b /o-d') do echo del "%backupDir%\%%a"


 :: deletes only files in backupDir
 :: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b /a-d') do echo del "%backupDir%\%%a"


 :: deletes all files and folders BUT keeps 3 latest FOLDERS in backupDir
 :: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b') do echo del "%backupDir%\%%a"


 :: deletes all files and folders BUT keeps 3 latest FOLDERS in backupDir
 :: for /f "skip=3 delims=" %%a in (' dir "%backupDir%\" /o-d /b') do echo rd /S /Q "%backupDir%\%%a"

pause

1 个答案:

答案 0 :(得分:0)

这两条不同的线路起作用。将所有文件保存在backupDir中,同时仅删除所选的N个最新nameDir文件夹。

这是根据@ JosefZ的评论Windows Batch Script to only keep the N latest FOLDERS in a directory

调整的
for /f "skip=2 delims=" %%a in ('dir "%backupDir%\" /B /O:-N /A:D') do echo rd rd /s /q "%backupDir%\%%a"

这也可以从@Magoo的答案https://stackoverflow.com/a/17521693/1010918

中获取
for /f "skip=2 delims=" %%a in (' dir "%backupDir%\" /b /ad-h /o-d') do rd /s /q "%backupDir%\%%a"

如果您愿意,请在Windows Batch Script to backup local MySQL databases & only keep N latest FOLDERS with backup files找到完整的批处理脚本。

谢谢大家的帮助!