?
Solved

Batch file help with month variable

Posted on 2011-05-03
6
Medium Priority
?
544 Views
Last Modified: 2012-08-13
Hi Experts,

I hope some clever puppy can assist me with a little project.
My client wants an end of month copy of his backup Images spread across 4 x 2TB backup drives.

These are labeled as so:
G: Months1-5-9
H: Months-2-6-10
I: Months-3-7-11
J: Months-4-8-12

I therefore need to write a clever line in the batch file that copies to the right place, but only on the 1st of the month (scheduled task running on a Windows 7 Pro box)

My starting point would be to do something like this:

rem check date with if statement
If date = 01-Jan
if not errorlevel 1 goto next
robocopy D:\Images  G:\MonthlyImages\Jan /mir /log:Jan.txt
:next

If date = 01-Feb
if not errorlevel 1 goto next
robocopy D:\Images  H:\MonthlyImages\Feb /mir /log:Feb.txt
:next

and so on!

In case relevant we are in region\timezone Sydney Australia.

Any assistance hugely appreciated!
Cheers
Wilson
0
Comment
Question by:wilsonkjit
  • 3
  • 2
6 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 35511305
Time zone isn't relevant, only the contents of your %date% variable.
If you have the format mm/dd/yyyy, the following should work, otherwise please post the output of "echo %date%" (or adjust the "set dd=..." and "set mm=..." accordingly).
@echo off
setlocal enabledelayedexpansion
set dd=%Date:~3,2%
set mm=%Date:~0,2%
set DriveList=01:G 02:H 03:I 04:J 05:G 06:H 07:I 08:J 09:G 10:H 11:I 12:J
for %%a in (%DriveList%) do (for /f "tokens=1,2 delims=:" %%m in ("%%a") do (set MonthlyDrive[%%m]=%%n))
set m
if not "%dd%"=="01" (
  echo Not 1st of month, exiting ...
  goto :eof
)
set Drive=!MonthlyDrive[%mm%]!
echo 1st of month %mm%; selected drive: %Drive%

Open in new window

0
 
LVL 85

Expert Comment

by:oBdA
ID: 35511326
Oh, and you probably want the target folder set as well for use in robocopy, sorry; just add these at the end:
set TargetFolder=%Drive%:\MonthlyImages\%mm%
echo Target folder: %TargetFolder%

Open in new window

0
 

Author Comment

by:wilsonkjit
ID: 35512120
Sweet thanks OBdA, that all seems to make sense. Ill give it a go in the morning and advise results.

THANKS HEAPS (as they say downunder)

Cheers
String

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:wilsonkjit
ID: 35696512
Just some feedback
Seem to work a treat. Thanks so much

The only thing I had to change was that Australian short time format is dd/mm/yyyy so I had to swap

update lines 3 and 4 as follows:
set dd=%Date:~0,2%
set mm=%Date:~3,2%

You are a true genius
Many thanks
String
0
 

Author Closing Comment

by:wilsonkjit
ID: 35696513
Wicked!!
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35697936
To add to oBdA's script, you could also have used wmic to get your date. This is independent of your date format.

FOR /F "skip=1 tokens=1-4" %%A IN ('WMIC Path Win32_LocalTime Get DayOfWeek^,Day^,Month^,Year /Format:table') DO (
      IF %%A GTR 0 (
      SET DayOfWeek=%%B
      SET mm=%%C
      SET dd=%%A
      SET yy=%%D
      )
)

PS: Nice and clean script oBdA   ;-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

807 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