#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    172
    Rep Power
    93

    Windows CMD programming


    I've got this script that backs up and moves files at the beginning of every month. It works fine with the only problem being that I have to manually edit the file each month to change the months & years of directories I'm copying and moving files to. I'm trying to adjust it so that when it's run (scheduled task) it makes all these changes itself so I don't have to remember to do it. I've not forgotten yet (~6 years) but...

    Here's what I've got so far:

    Code:
    for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set currDate=%%c-%%a-%%b & set currYear=%%c & set currMonth=%%a)
    set currYear=%currYear:~0,4%
    
    rem Hard Code to test
    rem set currMonth=02
    rem set currYear=2012
    
    if %currMonth%==01 (set /a lastYear=%currYear%-1)
    if not %currMonth%==01 (set lastYear=%currYear%)
    
    if %currMonth%==01 (set currMonth=January &	set lastMonth=December)
    if %currMonth%==02 (set currMonth=February &	set lastMonth=January)
    if %currMonth%==03 (set currMonth=March &	set lastMonth=February)
    if %currMonth%==04 (set currMonth=April &	set lastMonth=March)
    if %currMonth%==05 (set currMonth=May &		set lastMonth=April)
    if %currMonth%==06 (set currMonth=June &	set lastMonth=May)
    if %currMonth%==07 (set currMonth=July &	set lastMonth=June)
    if %currMonth%==08 (set currMonth=August &	set lastMonth=July)
    if %currMonth%==09 (set currMonth=September &	set lastMonth=August)
    if %currMonth%==10 (set currMonth=October &	set lastMonth=September)
    if %currMonth%==11 (set currMonth=November &	set lastMonth=October)
    if %currMonth%==12 (set currMonth=December &	set lastMonth=November)
    
    @ECHO.
    @ECHO All the Past files will now be moved to the Reorder Drive...
    @ECHO.
    waitfor /t 5
    
    robocopy "s:\cmyk\past" "s:\reorders\cmyk\%currYear%\%lastMonth%" *.* /S /MOVE /R:5 /W:5 /FP /NDL /NP /LOG+:"S:\it\backup routines\Server\end of month\end of %currMonth% %currDate%.txt" /TEE /L
    robocopy "s:\spot\past" "s:\reorders\spot\%currYear%\%lastMonth%" *.* /S /MOVE /R:5 /W:5 /FP /NDL /NP /LOG+:"S:\it\backup routines\Server\end of month\end of %currMonth% %currDate%.txt" /TEE /L
    robocopy "s:\stamps labels\stamps\past" "s:\reorders\stamps\%currYear%\%lastMonth%" *.* /S /MOVE /R:5 /W:5 /FP /NDL /NP /LOG+:"S:\it\backup routines\Server\end of month\end of %currMonth% %currDate%.txt" /TEE /L
    robocopy "s:\stamps labels\labels\past" "s:\reorders\labels\%currYear%\%lastMonth%" *.* /S /MOVE /R:5 /W:5 /FP /NDL /NP /LOG+:"S:\it\backup routines\Server\end of month\end of %currMonth% %currDate%.txt" /TEE /L
    The problem is that the copy to directory is being written as
    s:\reorders\cmyk\2012 \June (note the space after 2012).

    I'm trying to get rid of that space. I used
    Code:
    set currYear=%currYear:~0,4%
    and when I echo it there is no space but for some reason it is still putting it in the robocopy line.

    I'm home free when I figure this out.

    Also, if there's a better way to anything above I'm all ears. Everything besides the robocopy commands were pulled from google searches so there's certainly better ways. A switch to set the month name comes to mind...

    Thanks,

    Mike

    EDIT: this will be run on a Windows Server 2008 box.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    55
    Rep Power
    3
    Weird issue. Do the other things printing the year produce the same space?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    172
    Rep Power
    93
    Well, I was gonna reply that anywhere currYear is printed the space is there but I just went back to hard code the year (not pulled from date) and it worked fine. So I put it back and tried it again and it's working properly now.

    Don't know what I did or didn't do since the last time but I even checked the last robocopy log to verify that the space was there and it was at one point.

    Oh well. Thanks for looking...
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0
    You don't even need this:
    Code:
    set currYear=%currYear:~0,4%
    Realize that YOU are putting those spaces in yourself. If you didn't have this line the year would actually be "2012 ", though with that you set it to a length of 4 which removes it. You don't need that if you don't add the space to that variable in the first place.

    Take a look at this line:
    Code:
    for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set currDate=%%c-%%a-%%b & set currYear=%%c & set currMonth=%%a)
    What i'm seeing is this:
    do (set currDate=%%c-%%a-%%b{space}& set currYear=%%c{space}& set currMonth=%%a)
    And instead of this:
    Code:
    @ECHO.
    @ECHO All the Past files will now be moved to the Reorder Drive...
    @ECHO.
    Why not @echo off at the top of your script?

    Look:
    Code:
    @echo off
    for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set currDate=%%c-%%a-%%b& set currYear=%%c& set currMonth=%%a)
    
    if %currMonth%==01 (set /a lastYear=%currYear%-1)
    if not %currMonth%==01 (set lastYear=%currYear%)
    
    if %currMonth%==01 (set currMonth=January& set lastMonth=December)
    if %currMonth%==02 (set currMonth=February& set lastMonth=January)
    if %currMonth%==03 (set currMonth=March& set lastMonth=February)
    if %currMonth%==04 (set currMonth=April& set lastMonth=March)
    if %currMonth%==05 (set currMonth=May& set lastMonth=April)
    if %currMonth%==06 (set currMonth=June& set lastMonth=May)
    if %currMonth%==07 (set currMonth=July& set lastMonth=June)
    if %currMonth%==08 (set currMonth=August& set lastMonth=July)
    if %currMonth%==09 (set currMonth=September& set lastMonth=August)
    if %currMonth%==10 (set currMonth=October& set lastMonth=September)
    if %currMonth%==11 (set currMonth=November& set lastMonth=October)
    if %currMonth%==12 (set currMonth=December& set lastMonth=November)
    
    echo Current Year: "%currYear%"
    echo Current Month: "%currMonth%"
    echo.
    echo Last Year: "%lastYear%"
    echo Last Month: "%lastMonth%"
    
    pause && goto :eof
    Test that out and see that there are no spaces in these variables.

IMN logo majestic logo threadwatch logo seochat tools logo