You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改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:

  1. Convert the current date into a "total day count" (since a fixed point) to simplify date difference calculations.
  2. Scan existing Week 1 to Week 6 folders, extract their last backup date from the folder name.
  3. Calculate the day difference between each folder’s date and today, then select the week folder whose date is closest to 42 days old.
  4. 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 /t parsing assumes your system uses dd/mm/yyyy format. If your system uses mm/dd/yyyy, swap CurDay and CurMonth in the first for loop.
  • 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 the tokens=4 value 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:52:01