Batch file 批处理文件以覆盖6周前的备份

Batch file 批处理文件以覆盖6周前的备份,batch-file,robocopy,Batch File,Robocopy,我有下面的批处理文件,每天备份并保存7天 @echo off C: CD\ProAuto setlocal enableextensions :: Get Numeric Date for /F "skip=1 delims=" %%F in ('wmic PATH Win32_LocalTime GET Day^,Month^,Year /FORMAT:TABLE') do ( for /F "tokens=1-3" %%L in ("%%F") do ( set CurDay=0%%

我有下面的批处理文件,每天备份并保存7天

@echo off
C:
CD\ProAuto
setlocal enableextensions

:: Get Numeric Date
for /F "skip=1 delims=" %%F in ('wmic PATH Win32_LocalTime GET Day^,Month^,Year /FORMAT:TABLE') do ( for /F "tokens=1-3" %%L in ("%%F") do (
   set CurDay=0%%L
   set CurMonth=0%%M
   set CurYear=%%N
   ))
set CurDay=%CurDay:~-2%
set CurMonth=%CurMonth:~-2%

:: Get numeric Day of the Week
for /f %%a in ('wmic path win32_localtime get dayofweek /format:list ^| findstr "="') do (set %%a)

:: Get Time and Date with full Month and Day of week
for /f "skip=8 tokens=2,3,4,5,6,7,8,9 delims=,: " %%D in ('robocopy /l * T: T: /ns /nc /ndl /nfl /np /njh /XF * /XD *') do (
   set "dow=%%D"
   set "month=%%E"
   set "DD=%%F"
   set "YYYY=%%G"
   set "HH=%%H"
   set "MM=%%I"
   set "SS=%%J"
   set "AMPM=%%K"
   )
IF "%AMPM%"=="AM" ( Set AMPM=am )
IF "%AMPM%"=="PM" ( Set AMPM=pm )

Set RoboLog="C:\ProAuto\Logs\~%DayofWeek% %dow%.log"
Echo QuickBooks Backup  %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >%RoboLog%

:: The following checks to see if the database is locked by attempting to rename the file to the same name.
ren "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files\XYZ.QBW" "XYZ.QBW" >nul && (goto UnLocked) || (goto Locked)
Goto Donelocked
:UnLocked
echo QuickBooks is UnLocked and will be backed up >>%RoboLog%
Net Use "\\192.168.0.246\Backup Files"
Net Use "\\192.168.0.252\QB_BK"
Move "\\192.168.0.252\QB_BK\%DayofWeek% %dow%*" "\\192.168.0.252\QB_BK\%DayofWeek% %dow% %CurYear%-%CurMonth%-%CurDay%"
robocopy "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files" "\\192.168.0.252\QB_BK\%DayofWeek% %dow% %CurYear%-%CurMonth%-%CurDay%" /XD "QuickBooksAutoDataRecovery" /MIR /FFT /V /NP /NDL /copy:DAT /dcopy:T /r:2 /W:2 /log+:%RoboLog% /tee
Call :RoboCheck
If %RoboRC% GTR 7 Goto QBBKFail
:QBBKGood
Echo QuickBooks Daily Backup Successful! >>%RoboLog%
goto doneQBBK
:QBBKFail
Echo QuickBooks Daily Backup Had Errors! >>%RoboLog%
Echo Sending Error Email with Log to Clients >> %RoboLog%
powershell -executionpolicy bypass -file %~dp0QB_KS_Fail.ps1
:DoneQBBK
Echo Copying Log to Backup Directory >>%RoboLog%
Copy %RoboLog% "\\192.168.0.252\QB_BK\" >>%RoboLog%
Goto Donelocked
:Locked
echo QuickBooks is Locked and cannot be backed up because it is open on a client computer >>%RoboLog%
echo Generating an Open File report and Emailing it to Clients
Set OpenFileLog="C:\ProAuto\Logs\QB_Open_Files %curmonth%-%curday%-%curyear%.log"
Echo Open QuickBooks Files >> %RoboLog%
Echo Open QuickBooks Files > %OpenFileLog%
Echo %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >> %RoboLog%
Echo %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >> %OpenFileLog%
openfiles /query  /v | findstr /rc:"^$" /c:"Locally" /c:"remotely" /c:"----------" /c:"======" /c:"Accessed" /c:"QuickBooks" >> %RoboLog%
openfiles /query  /v | findstr /rc:"^$" /c:"Locally" /c:"remotely" /c:"----------" /c:"======" /c:"Accessed" /c:"QuickBooks" >> %OpenFileLog%
Echo Sending Open Files Email to Clients >> %RoboLog%
powershell -executionpolicy bypass -file %~dp0Locked_Email.ps1
Del %OpenFileLog%
:DoneLocked
Goto :EOF
:RoboCheck
Set RoboRC=%ErrorLevel%
Echo Return Code: %RoboRC% >>%RoboLog%
    if %RoboRC% EQU 16 echo ***FATAL ERROR*** >>%RoboLog% & echo Serious error. Robocopy did not copy any files. >>%RoboLog% & echo Either a usage error or an error due to insufficient access privileges >>%RoboLog% & echo on the source or destination directories. >>%RoboLog%
    if %RoboRC% EQU 15 echo Code 15 = Code 1 OKCOPY + Code 2 XTRA + Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 14 echo Code 14 = Code 2 XTRA + Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 13 echo Code 13 = Code 1 OKCOPY + Code 4 - MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 12 echo Code 12 = Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 11 echo Code 11 = Code 1 OKCOPY + Code 2 - XTRA + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 10 echo Code 10 = Code 2 XTRA + Code 8 COPYFAIL: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 9  echo Code 9 = Code 1 OKCOPY + Code 8 - COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 8  echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 7  echo Code 7 = Code 1 OKCOPY + Code 2 XTRA + Code 4 MISMATCHES: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 6  echo Code 6 = Code 2 XTRA + Code 4 MISMATCHES: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 5  echo Code 5 = Code 1 OKCOPY + Code 4 MISMATCHES: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 4  echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 3  echo Code 3 = Code 1 OKCOPY + Code 2 XTRA: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog%
    if %RoboRC% EQU 2  echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog%
    if %RoboRC% EQU 1  echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog%
    if %RoboRC% EQU 0  echo No errors occurred, and no copying was done. >>%RoboLog% & echo The source and destination directory trees are completely synchronized >>%RoboLog%
Goto :EOF
它的作用是每周一重命名周一备份目录以包含当前日期,然后使用Robocopy将当前数据镜像到新目录。然后星期二,它对星期二目录等执行相同的操作,因此每个备份保留7天

@echo off
C:
CD\ProAuto
setlocal enableextensions

:: Get Numeric Date
for /F "skip=1 delims=" %%F in ('wmic PATH Win32_LocalTime GET Day^,Month^,Year /FORMAT:TABLE') do ( for /F "tokens=1-3" %%L in ("%%F") do (
   set CurDay=0%%L
   set CurMonth=0%%M
   set CurYear=%%N
   ))
set CurDay=%CurDay:~-2%
set CurMonth=%CurMonth:~-2%

:: Get numeric Day of the Week
for /f %%a in ('wmic path win32_localtime get dayofweek /format:list ^| findstr "="') do (set %%a)

:: Get Time and Date with full Month and Day of week
for /f "skip=8 tokens=2,3,4,5,6,7,8,9 delims=,: " %%D in ('robocopy /l * T: T: /ns /nc /ndl /nfl /np /njh /XF * /XD *') do (
   set "dow=%%D"
   set "month=%%E"
   set "DD=%%F"
   set "YYYY=%%G"
   set "HH=%%H"
   set "MM=%%I"
   set "SS=%%J"
   set "AMPM=%%K"
   )
IF "%AMPM%"=="AM" ( Set AMPM=am )
IF "%AMPM%"=="PM" ( Set AMPM=pm )

Set RoboLog="C:\ProAuto\Logs\~%DayofWeek% %dow%.log"
Echo QuickBooks Backup  %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >%RoboLog%

:: The following checks to see if the database is locked by attempting to rename the file to the same name.
ren "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files\XYZ.QBW" "XYZ.QBW" >nul && (goto UnLocked) || (goto Locked)
Goto Donelocked
:UnLocked
echo QuickBooks is UnLocked and will be backed up >>%RoboLog%
Net Use "\\192.168.0.246\Backup Files"
Net Use "\\192.168.0.252\QB_BK"
Move "\\192.168.0.252\QB_BK\%DayofWeek% %dow%*" "\\192.168.0.252\QB_BK\%DayofWeek% %dow% %CurYear%-%CurMonth%-%CurDay%"
robocopy "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files" "\\192.168.0.252\QB_BK\%DayofWeek% %dow% %CurYear%-%CurMonth%-%CurDay%" /XD "QuickBooksAutoDataRecovery" /MIR /FFT /V /NP /NDL /copy:DAT /dcopy:T /r:2 /W:2 /log+:%RoboLog% /tee
Call :RoboCheck
If %RoboRC% GTR 7 Goto QBBKFail
:QBBKGood
Echo QuickBooks Daily Backup Successful! >>%RoboLog%
goto doneQBBK
:QBBKFail
Echo QuickBooks Daily Backup Had Errors! >>%RoboLog%
Echo Sending Error Email with Log to Clients >> %RoboLog%
powershell -executionpolicy bypass -file %~dp0QB_KS_Fail.ps1
:DoneQBBK
Echo Copying Log to Backup Directory >>%RoboLog%
Copy %RoboLog% "\\192.168.0.252\QB_BK\" >>%RoboLog%
Goto Donelocked
:Locked
echo QuickBooks is Locked and cannot be backed up because it is open on a client computer >>%RoboLog%
echo Generating an Open File report and Emailing it to Clients
Set OpenFileLog="C:\ProAuto\Logs\QB_Open_Files %curmonth%-%curday%-%curyear%.log"
Echo Open QuickBooks Files >> %RoboLog%
Echo Open QuickBooks Files > %OpenFileLog%
Echo %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >> %RoboLog%
Echo %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >> %OpenFileLog%
openfiles /query  /v | findstr /rc:"^$" /c:"Locally" /c:"remotely" /c:"----------" /c:"======" /c:"Accessed" /c:"QuickBooks" >> %RoboLog%
openfiles /query  /v | findstr /rc:"^$" /c:"Locally" /c:"remotely" /c:"----------" /c:"======" /c:"Accessed" /c:"QuickBooks" >> %OpenFileLog%
Echo Sending Open Files Email to Clients >> %RoboLog%
powershell -executionpolicy bypass -file %~dp0Locked_Email.ps1
Del %OpenFileLog%
:DoneLocked
Goto :EOF
:RoboCheck
Set RoboRC=%ErrorLevel%
Echo Return Code: %RoboRC% >>%RoboLog%
    if %RoboRC% EQU 16 echo ***FATAL ERROR*** >>%RoboLog% & echo Serious error. Robocopy did not copy any files. >>%RoboLog% & echo Either a usage error or an error due to insufficient access privileges >>%RoboLog% & echo on the source or destination directories. >>%RoboLog%
    if %RoboRC% EQU 15 echo Code 15 = Code 1 OKCOPY + Code 2 XTRA + Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 14 echo Code 14 = Code 2 XTRA + Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 13 echo Code 13 = Code 1 OKCOPY + Code 4 - MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 12 echo Code 12 = Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 11 echo Code 11 = Code 1 OKCOPY + Code 2 - XTRA + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 10 echo Code 10 = Code 2 XTRA + Code 8 COPYFAIL: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 9  echo Code 9 = Code 1 OKCOPY + Code 8 - COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 8  echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 7  echo Code 7 = Code 1 OKCOPY + Code 2 XTRA + Code 4 MISMATCHES: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 6  echo Code 6 = Code 2 XTRA + Code 4 MISMATCHES: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 5  echo Code 5 = Code 1 OKCOPY + Code 4 MISMATCHES: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 4  echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 3  echo Code 3 = Code 1 OKCOPY + Code 2 XTRA: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog%
    if %RoboRC% EQU 2  echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog%
    if %RoboRC% EQU 1  echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog%
    if %RoboRC% EQU 0  echo No errors occurred, and no copying was done. >>%RoboLog% & echo The source and destination directory trees are completely synchronized >>%RoboLog%
Goto :EOF
我想修改它,每周备份一次并保存6周。我想我会将目录名更改为第1周到第6周,并按顺序回收目录,但现在我没有一个简单的方法来确定我在哪一周,因为没有日历事件会像我使用一周中的某一天进行每日备份那样每6周重复一次

因此,我要寻找的是一些方法或建议,告诉我如何确定哪个备份目录最接近6周前的备份目录,以便重新循环使用正确的备份目录。有可能在6周前没有备份,在这种情况下,它只会创建一个备份,就像日常备份一样,如果星期三丢失,它只会创建一个新的星期三备份。我也不想总是使用最旧的备份,例如,如果机器停机,它错过了一个备份,我不想改变顺序,我想使用6周前的备份或创建一个新的备份

我想找出一种方法来计算哪个%WeekofCycle%,以便使用以下代码:

Move "\\192.168.0.252\QB_BK\Week %WeekofCycle%*" "\\192.168.0.252\QB_BK\Week %WeekofCycle%* %dow% %CurYear%-%CurMonth%-%CurDay%"
robocopy "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files" "\\192.168.0.252\QB_BK\Week %WeekofCycle%* %dow% %CurYear%-%CurMonth%-%CurDay%" /XD "QuickBooksAutoDataRecovery" /MIR /FFT /V /NP /NDL /copy:DAT /dcopy:T /r:2 /W:2 /log+:%RoboLog% /tee

我试图找出一种方法来计算周期中的哪一周,而不管可能已经或可能还没有完成哪些备份,也不管是否有额外的备份或不存在的备份。如果我计算我在第3周,我会将以前的备份重命名为\192.168.0.252\QB_BK\Week 3*,现在将日期和日期附加到备份中,然后使用Robocopy的新名称更新更改的文件。

我要做的是将每个目标目录命名为yyyymmdd-根据需要附加dayname/date/其他数据,并将目标目录名附加到backups.log文件中

成功备份当前数据后,然后

for /f "skip=42,delims=" %%a in (`sort /r backups.log`) do (
 ECHO rd /s /q "%%a"
 findstr /v /x "%%a" backups.log >dummy.txt
 move dummy.txt backups.log
)
这将按与日期相反的顺序对备份目录的名称进行排序,以使最新的目录名位于第一位。跳过前42=6周*7天的行,并使用rd删除最早的任何其他条目,然后使用findstr从backups.log文件中删除这些条目,以输出与刚刚删除的目录/x不完全匹配的行,因此,在文件系统上保留的任何备份都将从文件和文件系统中删除


请注意,for将处理读取backups.log文件的反向排序结果。此结果保存在内存中,并且不受for循环对backups.log所做更改的影响。

请参阅以及为什么Magoo的解决方案比所有那些旧的备份存储策略更好,而不考虑是否同时创建了备份。我不想只删除6周以上的备份,我想选择6周前的备份,并通过重命名目录将其回收,然后使用Robocopy仅修改更改的文件,如我的示例所示。当然,您可以根据需要使备份策略变得复杂。这是你的选择。发布了一个奇妙而简单的解决方案,以了解上次创建或更新每个备份目录的时间。在从42个现有备份目录中了解最旧的备份目录后,您可以选择在的命令块体中执行哪些命令。备份文件夹可以重命名或移动,不再是备份文件夹中删除的现有文件和文件夹,也不再是复制到备份文件夹中的较新文件和文件夹,等等。我知道解决方案会找到最旧的备份,但备份还不存在。我遇到的问题是知道备份有多少周了,如果最后一个备份还没有6周,那么我只想做一个新的备份,而不去管最后一个。备份仅每周发生一次,而不是每天。要想弄清楚一件东西有多少周的历史并不像看上去那么容易。我可以用上次更新日期的目录命名,但以后我怎么知道它们有多少周了?因为每个月都有不同的天数,这并不简单