Robocopy script that created folder in destination with date
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
Admin
Updated on January 28, 2020Comments
-
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 over 10 yearsSorry, it wont let me paste the output...too many characters.
-
Admin over 10 years2014/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 over 10 yearsYou mucked up the variables. Evidently you have XP Home which doesn't support WMIC.
-
Admin over 10 yearsGot 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 over 10 yearsI'm on Windows 7 PRO x64
-
Admin over 10 yearsI 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 over 10 yearsHow did you paste the screen shot?
-
Admin over 10 yearsEureka!!! 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 over 10 yearsJust click on the picture icon.