Solved

daily MSDOS batch script (.bat/.cmd) to do xcopy/e/c/h/D:d-m-y wrt 1st Sunday of the month

Posted on 2008-10-14
3
2,461 Views
Last Modified: 2012-05-05
Hi,

I need a daily .bat/.cmd MSDOS script that runs at 1am that will :

a)If it's the first Sunday of the month, do :
xcopy/E/C/H/Y  n:\folder1  n:\backup\folder1\yyyymmdd  > n:\log\yyyymmdd_folder1.txt 2>> n:\log\yyyymmdd
xcopy/E/C/H/Y  n:\folder2  n:\backup\folder2\yyyymmdd  > n:\log\yyyymmdd_folder2.txt 2>> n:\log\yyyymmdd
     (ie take a full backup of folder1 & folder2;   yyyymmdd is the date the script run
      & n:  is a shared drive/folder)

b)If it's other days, do :
xcopy/E/C/H/Y/D:d-m-y  n:\folder1  n:\backup\folder1\yyyymmdd  > n:\log\yyyymmdd_folder1.txt 2>> n:\log\yyyymmdd
xcopy/E/C/H/Y/D:d-m-y  n:\folder2  n:\backup\folder1\yyyymmdd  > n:\log\yyyymmdd_folder2.txt 2>> n:\log\yyyymmdd
(this is a differential backup   to backup files that have been modified or  created since the
  last first Sunday of the month)

c)
in step (a) above, if it's the first Sunday of the month, it would delete away (using "deltree ?")
the folders that are 5 months old & subsequent differential backups' folders with respect to
that full backup's date.


Purpose of the >> and 2>>  is to create logs for review next morning if there's
errors with backup.

If you need to break this up into multiple scripts, it's fine with me.
Single script would be nice but not essential


0
Comment
Question by:sunhux
  • 2
3 Comments
 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 500 total points
ID: 22713799
In a command prompt can you paste the output of

echo %date%
0
 

Author Comment

by:sunhux
ID: 22719573
On my PC's MSDOS prompt, the output is :
15-10-2008
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 500 total points
ID: 22722199
OK. Give this a try. You'll need to remove the 6 ECHO in captials for the script to actually perform the operation.

The removing of the 5 month old backup folders will only remove differentials only in that month. Differentials that spill over to the next month will be removed next month's first Sunday.
FOR /f "Tokens=2,*" %%a in ('REG QUERY "HKCU\Control Panel\International" /v sShortDate ^| Find /i "sShortDate"') do Set CurrentsShortDate=%%b

REG ADD "HKCU\Control Panel\International" /v sShortDate /d "d-M-yyyy" /f

Set DayOfTheWeek=%date:~0,3%

Set Day=%date:~4,2%

Set Month=%date:~7,2%

Set Year=%date:~10,4%

REG ADD "HKCU\Control Panel\International" /v sShortDate /d "%CurrentsShortDate%" /f

Echo Day of the week=%DayOfTheWeek%

Echo Day=%Day%

Echo Month=%Month%

Echo Year=%Year%
 

Set MonthsAGoYear=%Year%

Set /a MonthsAGo=1%Month%-5
 

If %MonthsAGo% lss 101 Set /a MonthsAGoYear-=1

If %MonthsAGo% lss 101 Set /a MonthsAGo+=12
 

Set MonthsAGo=%MonthsAGo:~-2%
 

IF /i "%DayOfTheWeek%" NEQ "sun" GOTO DIFFERENTIAL

IF %Day% GTR 7 GOTO DIFFERENTIAL
 

:FULL

ECHO    xcopy/E/C/H/Y  n:\folder1  n:\backup\folder1\%Year%%Month%%Day%\  > n:\log\%Year%%Month%%Day%_folder1.txt 2>> n:\log\%Year%%Month%%Day%

ECHO    xcopy/E/C/H/Y  n:\folder2  n:\backup\folder2\%Year%%Month%%Day%\  > n:\log\%Year%%Month%%Day%_folder2.txt 2>> n:\log\%Year%%Month%%Day%
 

FOR /f "Tokens=*" %%a in ('DIR /ad /b "n:\backup\folder1\%MonthsAGoYear%%MonthsAGo%*"') DO ECHO RD /q /s "n:\backup\folder1\%%a"

FOR /f "Tokens=*" %%a in ('DIR /ad /b "n:\backup\folder2\%MonthsAGoYear%%MonthsAGo%*"') DO ECHO RD /q /s "n:\backup\folder2\%%a"

GOTO :EOF
 

:DIFFERENTIAL

ECHO    xcopy/E/C/H/Y/D:%Day%-%Month%-%Year%  n:\folder1  n:\backup\folder1\%Year%%Month%%Day%\  > n:\log\%Year%%Month%%Day%_folder1.txt 2>> n:\log\%Year%%Month%%Day%

ECHO    xcopy/E/C/H/Y/D:%Day%-%Month%-%Year%  n:\folder2  n:\backup\folder2\%Year%%Month%%Day%\  > n:\log\%Year%%Month%%Day%_folder2.txt 2>> n:\log\%Year%%Month%%Day%

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now