Batch file help with month variable

Posted on 2011-05-03
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

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

and so on!

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

Any assistance hugely appreciated!
Question by:wilsonkjit
    LVL 82

    Accepted Solution

    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

    LVL 82

    Expert Comment

    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


    Author Comment

    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)



    Author Comment

    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

    Author Closing Comment

    LVL 10

    Expert Comment

    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   ;-)

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
    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 this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    733 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

    20 Experts available now in Live!

    Get 1:1 Help Now