我创建了一个批处理文件,该文件应该保留网络上的4个最新文件夹。这是因为它是其中一台计算机的备份。 我首先在我自己的系统上测试它并且它正常工作。测试后,我将批处理复制到另一个系统,让它由Windows的任务计划程序运行。它确实备份,但它不会删除最旧的文件夹。我给了那个任务所需的所有权限。我会在这里粘贴代码,因为我认为代码中可能存在错误。
@echo off
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _yyyy=%%L
Set _mm=00%%J
Set _dd=00%%G
)
:s_done
Set _mm=%_mm:~-2%
Set _dd=%_dd:~-2%
Set _isodate=%_yyyy%-%_mm%-%_dd%
Echo %_isodate%
@echo off
for /F "skip=4 delims=" %%I in ('dir "\\server\Folder1\folder2\backup1\backup1_*" /AD /B /O-N 2^>nul') do rd /Q /S "\\server\Folder1\folder2\backup1\%%I"
for /F "skip=4 delims=" %%I in ('dir "\\server\Folder1\folder2\backup2\backup2_*" /AD /B /O-N 2^>nul') do rd /Q /S "\\server\Folder1\folder2\backup2\%%I"
for /F "skip=4 delims=" %%I in ('dir "\\server\Folder1\folder2\backup3\backup3_*" /AD /B /O-N 2^>nul') do rd /Q /S "\\server\Folder1\folder2\backup3\%%I"
for /F "skip=4 delims=" %%I in ('dir "\\server\Folder1\folder2\backup4\backup4_*" /AD /B /O-N 2^>nul') do rd /Q /S "\\server\Folder1\folder2\backup4\%%I"
@echo off
cd C:\
ROBOCOPY "C:\backup1" "\\server\Folder1\folder1\backup1\backup1"_%_isodate% /COPYALL /E /W:5 /R:5
ROBOCOPY "C:\backup2" "\\server\Folder1\folder2\backup2\backup2"_%_isodate% /COPYALL /E /W:5 /R:5
ROBOCOPY "C:\backup3" "\\server\Folder1\folder2\backup3\backup3"_%_isodate% /COPYALL /E /W:5 /R:5
ROBOCOPY "C:\backup4" "\\server\Folder1\folder2\backup4\backup4"_%_isodate% /COPYALL /E /W:5 /R:5
名称'文件夹'用于表示常规文件夹,名称'备份'用于表示备份文件夹。