Windows批处理脚本只能将N个最新的FOLDERS保存在目录中 [英] Windows Batch Script to only keep the N latest FOLDERS in a directory
问题描述
现在30或60天后,我只想保留最新的文件夹,并删除其余的文件夹,每个都有gzipped的dbs。
为了备份MySQL dbs我使用 adityasatrio的批处理脚本,并创建具有当前日期的文件夹也会排序感谢这个答案。
但是,为了保护本例,最新的3个文件夹和删除备份目录中的所有其他文件夹似乎是一个斗争。
这就是我已经尝试过。
::删除backupDir中的所有文件和文件夹
:: for / fskip = 3 delims =%% a in('dir%backupDir% \\/ b / o-d')do echo del%backupDir%\ %% a
::仅删除backupDir中的文件
:: for / fskip = 3 delims =%% a in('dir%backupDir%\/ b / a-d')do echo del%backupDir%\ %% a
::删除所有文件和文件夹但是在backupDir中保留3个最新的文件夹
:: for / fskip = 3 delims =%% a in('dir%backupDir%\/ b')做echo echo%backupDir%\ %% a
::删除所有文件和文件夹BUT保留3个最新的文件夹在backupDir
:: for / fskip = 3 delims =%% a in('dir%backupDir%\/ od / b')do echo rd / S / Q%backupDir%\ %% a
请注意,我不希望删除该backupDir中可能存在的任何文件。我删除了backupDir中最新的3个文件夹以外的所有内容,所以基本上删除旧文件夹中的gzip压缩的.sql文件。
这样做是正确的。
如何在Windows上保留最新的文件时从目录中删除旧文件(文件不是文件夹)
批量文件删除超过N天的文件(再次,文件不是文件夹)
https://serverfault.com/questions/49614/delete-files-older-than-x-days
(为了很好的措施,再次文件不是文件夹..)
这个评论删除除X个最新文件夹之外的所有文件夹最接近我正在尝试的操作,但也会删除backupDir中可能存在的任何文件。
** edit1:**刚刚发现删除Windows中的最后一个文件夹以外的所有文件夹,并将看到是否有任何帮助。
完整的脚本与我的谦虚 popd
之下的努力就在这里。我希望通过您的帮助或输入得到这个进一步阅读。注意我是批量脚本的新功能。对此的任何帮助非常感激。帮助我得到所有,除了那些N最新的文件夹删除。 :)
@echo off
/ pre>
设置dbUser = root
设置dbPassword = root
setbackupDir = D:\MySQLDumps
setmysqldump = C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump.exe
设置mysqlDataDir = C:\wamp\bin\mysql\mysql5.6.17\data
setzip = C:\Program Files\7-Zip \7z.exe
rem以下四行将为您提供XP Pro及更高版本中可靠的YY DD MM YYYY HH Min Sec MS变量。
for / ftokens = 2 delims ==%% a in('wmic OS Get localdatetime / value')do setdt = %% a
setYY = %dt:〜2,2%&设置YYYY =%dt:〜0,4%&设置MM =%dt:〜4,2%&设置DD =%dt:〜6,2%
setHH =%dt:〜8,2%&设置Min =%dt:〜10,2%&设定Sec =%dt:〜12.2%&设置MS =%dt:〜15,3%
setdirname =%YYYY% - %MM% - %DD%%HH% - %Min% - %Sec%
echodirName=%dirName%
:: pause
::切换到data文件夹
pushd%mysqlDataDir%
::创建备份文件夹(如果不存在)
如果不存在%backupDir%\%dirName%\mkdir%backupDir%\%dirName%
::迭代数据文件夹中的文件夹结构,以获取数据库
for(*)do中的/ d %% f(
echo processing文件夹%% f
%mysqldump%--host =localhost--user =%dbUser%--password =%dbPassword%--single-transaction -add-drop -table - 数据库%% f>%backupDir%\%dirName%\ %%〜nxf.sql
%zip%a -tgzip%backupDir%\ %dirName%\ %%〜nxf.sql.gz%backupDir%\%dirName%\ %%〜nxf.sql
del%backupDir%\%dirName% \ %%〜nxf.sql
)
popd
::删除所有文件夹,但最新的3
::删除所有文件s和backupDir中的文件夹
:: for / fskip = 3 delims =%% a in('dir%backupDir%\/ b / o-d')do echo del%backupDir %\ %% a
::仅删除backupDir中的文件
:: for / fskip = 3 delims =%% a in('dir) %backupDir%\/ b / a-d')do echo del%backupDir%\ %% a
::删除所有文件和文件夹,但保留3最新文件夹在backupDir
:: for / fskip = 3 delims =%% a in('dir%backupDir%\/ b')do echo del%backupDir%\ %% a
::删除所有文件和文件夹但是在backupDir中保留3个最新的文件夹
:: for / fskip = 3 delims =%% a in('dir %backupDir%\/ od / b')do echo rd / S / Q%backupDir%\ %% a
pause
解决方案这两个不同的行工作。保留所有文件可能在backupDir中,而仅删除所选的N个最新的名称Dir文件夹。
这是从@ JosefZ的评论调整 Windows批处理脚本只保留N最新FOLDERS in a directory
for / fskip = 2 delims =%% a in('dir) %backupDir%\/ B / O:-N / A:D')do echo rd rd / s / q%backupDir%\ %% a
pre>
这也是从@ Magoo的答案从这里 https:// stackoverflow.com/a/17521693/1010918
for / fskip = 2 delims =%% a in('dir%backupDir%\/ b / ad-h / o-d')do rd / s / q%backupDir%\ %% a
在这里找到完整的批量脚本 Windows批处理脚本来备份本地MySQL数据库&如果您愿意,只保留N个最新的FOLDERS与备份文件。
感谢大家的帮助!
I have a directory into which I backup MySQL dbs in a folder with the current date.
Now after 30 or 60 days I like to only keep the n latest folders in that backup directory and delete the rest of the folders, each having gzipped dbs in them.
For backing up the MySQL dbs I use adityasatrio's batch script and creating the folder with the current date is also sorted thanks to this answer.
However only keeping the, for sake of this example, latest 3 folders and deleting all other folders in the backup directory seems a struggle.
This is what I have tried.
:: 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"
Please note I am not looking to delete any files that might be in that backupDir. I am after deleting all but the latest 3 folders in the backupDir, so basically remove old folders with gzipped .sql files in them.
These questions helped but still I am not doing it right somehow.
delete all but X most recent folders
How do I delete old files from a directory while keeping the most recent ones on Windows (files not folder)
Batch file to delete files older than N days (again, files not folder)
https://serverfault.com/questions/49614/delete-files-older-than-x-days (for good measure, again files not folders..)
This comment delete all but X most recent folders comes closest to what I am trying to do, but that also deletes any files that might be in the backupDir.
**edit1:**Just found Delete all folders except 2 lastest folders in Windows and will see if that helps in any ways.
The complete script with my humble efforts below
popd
is here. I hope to get this sorted with your help or input for further reading. Note I am totally new to Batch Scripting. Any help towards this is really much appreciated. Help me get all but those N latest folders deleted please. :)@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
解决方案These two different lines work. Keeping all files there might be in the backupDir while deleting only the selected N latest nameDir folders.
This is adjusted from @JosefZ's comment 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"
This also works taken from @Magoo's answer from here 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"
Find the complete working Batch Script here Windows Batch Script to backup local MySQL databases & only keep N latest FOLDERS with backup files if you like.
Thank you all for your help!
这篇关于Windows批处理脚本只能将N个最新的FOLDERS保存在目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!