Robocopy script that created folder in destination with date

35,646

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%"

Alter the order of %yyyy% and %mm% and %dd% to suit you and then you can add the variable to Robocopy:

robocopy "E:\Batch Files" "I:\Backups\%datestamp%" /MIR
Share:
35,646
Admin
Author by

Admin

Updated on January 28, 2020

Comments

  • Admin
    Admin over 4 years

    I'm trying to write a script that copies a folder every week night. I would like it to create a folder each night in the destination directory.

    So I'm trying to make a script that copies "C:\logs" to "I:\logs\today's date"

    So the destination folder should look like this

    "I:\logs\Monday_1272014" then the next night it should look like this "I:\logs\Tuesday_1282014"

    and so on for each weekday. I'm not sure if I'm making sense, but any help would be appreciated.

    So far I have this for the Robocopy script: robocopy "E:\Batch Files" "I:\Backups\Monday" /MIR

    But that would just create a folder named Monday. Any help would be greatly appreciated. Also, it does not have to be robocopy. I just use that because it's the only one I know. I wouldn't mind learning how to do it as a batch file or VB Script.

    Thanks!

  • Admin
    Admin over 10 years
    Sorry, it wont let me paste the output...too many characters.
  • Admin
    Admin over 10 years
    2014/01/28 00:14:10 ERROR 267 (0x0000010B) Creating Destination Directory I:\Bac kups\%dt:~4,2%DD%YYYY\ The directory name is invalid.
  • foxidrive
    foxidrive over 10 years
    You mucked up the variables. Evidently you have XP Home which doesn't support WMIC.
  • Admin
    Admin over 10 years
    Got it to work. I had to change: for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" to for /f "tokens=2 delims==" %a in ('wmic OS Get localdatetime /value') do set "dt=%a"
  • Admin
    Admin over 10 years
    I'm on Windows 7 PRO x64
  • Admin
    Admin over 10 years
    I got the script to work, but now I have another issue. I saved it as test_script.bat .When I double click the .bat file, nothing happens. What am I doing wrong? I'm running Windows 7 Pro x64. I put the file in notepad and did "save as" "all files" and named it "test_script.bat"
  • Admin
    Admin over 10 years
    How did you paste the screen shot?
  • Admin
    Admin over 10 years
    Eureka!!! I got it figured out. When I wanted to just type the script into the cmd window I had to change %%a to %a. However, when i wanted to save it as a .bat file I have to change it back to %%a.
  • Endoro
    Endoro over 10 years
    Just click on the picture icon.