我们有一些电子邮件存档,将所有的电子邮件转储到一个目录。 由于服务器的某些性能原因,我想设置一个自动化的任务,每天运行一个脚本,如果主目录中有超过3000个(或任何数量)的文件,则创build一个包含date的新目录并将所有的主目录文件移动到它。 我相信有人已经写了类似的东西,所以如果有人能指出我的意思,那将是很棒的。 batch file或Powershell都将罚款。
书面和testing。 将以下代码复制到* .bat文件中。 您需要在代码的开头修改电子邮件所在的目录。 variablescBig已经被设置为3000,但是如果你愿意,你可以改变它。 在底部,移动* .txt将不得不改变,以反映你正在移动的电子邮件的扩展。 一旦你testing了它,很高兴你可以删除暂停命令…他们只是帮助看看发生了什么事情。 祝你好运!
echo off REM **navigate to the directory cd\bat_test REM **store count of files to file count.txt (/ad removes folders from count) dir /b /ad | find /v /c "::" > count.txt REM **read count back in to variable (easiest way I knew how to do this) set /p myvar=<count.txt REM **set your upper limit (in your case 3000) set cBig=3000 REM **quick display of the number of files echo %myvar% pause REM **is the number of files larger than our upper limit? If so goto BIG if '%myvar%' gtr '%cBig%' goto BIG :SMALL REM **do nothing exit :BIG REM **create new directory with date and move all files Set FDate=%Date:~-10,10% Set Fdate=%FDate:/=-% MD %FDate% move *.txt ./%FDate% pause
未经testing的.CMD脚本。
REM @echo off setlocal enableextensions enabledelayedexpansion rem Print all filenames (excl. folders) in current directory into temporary text-file set TMPTXT=%TEMP%\%~n0.%RANDOM%.TMP dir /B /AD 1>%TMPTXT% rem Count number of files (lines) in text-file set FILECNT=0 for /F %%i in (%TMPTXT%) do ( set /A FILECNT=!FILECNT!+1 ) echo Number of files in folder: !FILECNT! rem Is number of files greater than expected? if /I !FILECNT! GTR 2999 call :MoveFiles del %TMPTXT% goto :EOF :MoveFiles rem Construct a folder-name based on date (remember date changes at midnight) rem Since the date value is locale specific, you might want to fiddle with string-replacing. set SUBFLDR=%DATE% mkdir "%SUBFLDR%" if /I !ERRORLEVEL! NEQ 0 ( echo Failed to create sub-folder '%SUBFLDR%'. goto :EOF ) rem Move only those files found in text-file to the new folder. for /F %%f in (%TMPTXT%) do ( move "%%f" "%SUBFLDR%\." if /I !ERRORLEVEL! NEQ 0 echo Failed to move file '%%f' ) goto :EOF