0

I have two folders called Roads and Kerbs. I want to copy both folders (keeping the folder name) into a backup folder with the current year/month as the folder name.

Example: The backup folder name will be 201812 and if I open it the two folders Roads and Kerbs will be under it. I want to create a batch file with xcopy to do this function for me.

The current code only opens a folder named 01 and only copies the folder content. I want the month/year and then the two folders under it with their content.

@echo off
set curr_date=%DATE:~10,2%-%DATE:~7,2%
mkdir "C:\Roads_backup\%curr_date%"
xcopy "C:\Roads" "C:\Roads_backup\%curr_date%" /D/S/H/V/C/F/K

How to copy the two folders to new directory with current year and month in directory name?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Pauline
  • 3
  • 2
  • Format of date on usage of dynamic environment variable `DATE` depends on configured country for the used account. Have you verified with `echo Current date is %DATE:~10,2%-%DATE:~7,2%` in a command prompt window or with `echo Current date is %curr_date%` in batch file as third line that you really get the current date in format `YYYY-MM`? See [What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean?](https://stackoverflow.com/a/37236739/3074564) and [%date% produces different result in batch file when run from scheduled tasks](https://stackoverflow.com/a/44670322/3074564). – Mofi Dec 21 '18 at 06:02
  • Run in a command prompt window `xcopy /?` and read the output help. Have you used the correct options? What about `%SystemRoot%\System32\xcopy.exe "C:\Roads\" "C:\Backup\%DATE:~-4%%DATE:~-7,2%\Roads\" /D /S /C /I /Q /H /K /Y`? The destination path should end with a backslash to make it 100% clear for `xcopy` that the destination is a directory and not a file, see [batch file asks for file or folder](https://stackoverflow.com/a/35829012/3074564). The command `mkdir` is not needed because `xcopy` as used here creates the destination directory tree itself. – Mofi Dec 21 '18 at 06:20
  • Thank you it is greatly appreciated! – Pauline Jan 01 '19 at 23:29

1 Answers1

0

CMD is Horrible at Dates

For anything of any complexity whatsoever, I'd avoid using %DATE% and %TIME% in favor of not reinventing the wheel. PowerShell comes standard (though with different versions) on every Windows platform released in the past 10 years, and uses a reasonably full-featured date/time library.

FOR /F "usebackq" %%d IN (`PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Get-Date -UFormat '%%Y%%m'" ^<NUL`) DO SET "STAMP=%%~d"

This will set the envvar STAMP to the formatted date string, controlled by the -UFormat parameter. It won't matter what the computer's locale or language is, and it'll work on anything with PowerShell (standard on Win7 and beyond).

It is a little wordy, but you can also easily copy/paste it without having to recalculate all the string offsets needed to carve up the values in %DATE% and %TIME% every time you want format the current date.

You can also do the same thing with dates other than the current one, as well as subtract timespans from the current date to get things like "a week ago"—something excruciatingly painful to do with pure batch/CMD alone.

Copying Directories

You'll probably want to use the /I (target name is a dir) parameter of xcopy as well as specify the target directory name to use (otherwise, xcopy will put the contents of Roads into the target directory without creating a directory named Roads).

xcopy "C:\Roads" "C:\Roads_backup\%STAMP%\Roads" /I/D/S/H/V/C/F/K

I'm not sure if you need /D if you're always going to be creating new dirs, since that means "only copy newer files."

mojo
  • 4,050
  • 17
  • 24