如何修改QuickBooks每日备份批处理为6周周期的每周备份?
Solution for 6-Week Rotating QuickBooks Backup Cycle
I get it, switching from a weekday-based daily backup rotation to a 6-week cycle is tricky—you can’t just map to a specific day of the week, and you need to preserve the cycle even if backups are missed or extra ones are run. Here’s a robust batch script solution that tracks each week’s last backup date and selects the correct cycle week to reuse, prioritizing the one closest to 6 weeks old (42 days).
Core Logic
Instead of blindly picking the oldest folder, we:
- Convert the current date into a "total day count" (since a fixed point) to simplify date difference calculations.
- Scan existing
Week 1toWeek 6folders, extract their last backup date from the folder name. - Calculate the day difference between each folder’s date and today, then select the week folder whose date is closest to 42 days old.
- If no week folders exist yet, default to
Week 1.
Full Batch Script
@echo off setlocal enabledelayedexpansion :: Configure your backup paths and log location set "BackupPath=\\192.168.0.252\QB_BK" set "RoboLog=C:\QB_Backup_Log.txt" :: Grab current date components (adjust delims if your system uses a different date format) for /f "tokens=1-4 delims=/ " %%a in ('date /t') do ( set "CurDay=%%a" set "CurMonth=%%b" set "CurYear=%%c" set "dow=%%d" ) :: Standardize date format to YYYY-MM-DD for consistent parsing set "CurrentDate=%CurYear%-%CurMonth%-%CurDay%" :: Use PowerShell to get total days since 1970-01-01 (simplifies date difference math) for /f %%d in ('powershell -Command "(Get-Date -Date '%CurrentDate%').ToFileTimeUtc() / 864000000000 - 109205"') do set "CurrentDays=%%d" :: Initialize variables to track the best week to reuse set "TargetWeek=" set "MinDiff=9999" :: Check each week in the 6-week cycle for /l %%w in (1,1,6) do ( :: Find existing folders for this week number for /f "delims=" %%d in ('dir /ad /b "%BackupPath%\Week %%w*" 2^>nul') do ( set "DirName=%%d" :: Extract the YYYY-MM-DD date from the folder name (adjust token number if your naming changes) for /f "tokens=4 delims= " %%p in ("!DirName!") do set "DirDate=%%p" :: Calculate day difference if we found a valid date if not "!DirDate!"=="" ( for /f %%d in ('powershell -Command "(Get-Date -Date '!DirDate!').ToFileTimeUtc() / 864000000000 - 109205"') do set "DirDays=%%d" set /a "Diff=CurrentDays - DirDays" :: Get absolute value of the difference if !Diff! lss 0 set /a "Diff=-Diff" :: Update target week if this one is closer to 42 days old if !Diff! lss !MinDiff! ( set "MinDiff=!Diff!" set "TargetWeek=%%w" ) ) ) ) :: Default to Week 1 if no existing week folders are found if not defined TargetWeek set "TargetWeek=1" :: Rename the existing week folder (if it exists) with the current date echo Rotating existing Week %TargetWeek% backup... move "%BackupPath%\Week %TargetWeek%*" "%BackupPath%\Week %TargetWeek% %dow% %CurrentDate%" 2>nul :: Run Robocopy to mirror the QuickBooks files to the new week folder echo Starting QuickBooks backup to Week %TargetWeek%... robocopy "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files" "%BackupPath%\Week %TargetWeek%" ^ /XD "QuickBooksAutoDataRecovery" /MIR /FFT /V /NP /NDL /copy:DAT /dcopy:T /r:2 /W:2 ^ /log+:%RoboLog% /tee endlocal
Key Notes & Adjustments
- Date Format: The
date /tparsing assumes your system usesdd/mm/yyyyformat. If your system usesmm/dd/yyyy, swapCurDayandCurMonthin the firstforloop. - Folder Naming: The script extracts the date from the 4th token in the folder name (e.g.,
Week 2 Wed 2024-05-22). If you change the naming structure, adjust thetokens=4value to match where the date lives. - Permissions: Ensure the user running this script has read/write access to
\\192.168.0.252\QB_BK. - Edge Cases: If multiple folders exist for the same week number (e.g., accidental duplicates), the script will use the first one it finds—avoid this by ensuring only one folder per week number exists at a time.
内容的提问来源于stack exchange,提问作者James Richters
相关产品推荐
相关产品推荐

