-2

I want to know how I can use a Batch file to copy a file from one directory to another directory, then name it with a dynamic name that uses the date of the backup as the name. I know how to copy the file to the new DIR using xcopy (Which is sort of slow with the size of files I copy), but I can't figure out how to name the folder that they get copied to, dynamically. [if anyone has a faster solution, I'll hear it]

I've been trying to figure this out, and I know this question has been asked before, and I've tried to research it extensively, but I'm having trouble understanding how it works.

2 Answers2

0

Run this batch code and you can see how to get current date assigned to environment variable FolderDate for usage in name of backup folder.

@echo off

rem Get the current local date and time in a region and language
rem independent format by using Windows Management Instrumentation.

for /F "skip=1 delims=." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS get localdatetime') do set "LocalDateTime=%%T" & goto ReformatDate

rem Convert the date and time string in format yyyymmddhhmmss like
rem 20151231150357 to a readable format with date only like 2015-12-31.

:ReformatDate
set "FolderDate=%LocalDateTime:~0,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2%"

rem Output the date string for the backup folder and delete the 2 variables.

echo Date for backup folder is: %FolderDate%
echo.
set "LocalDateTime="
set "FolderDate="
pause

Read for example answer on Find out if file is older than 4 hours in Batch file and the answers on Batch file to delete files older than N days for details about getting and reformatting date and time string.

The usage of value of environment variable DATE reformatted for the folder name to format yyyy-mm-dd would be much faster than using wmic. But using DATE requires knowledge of date format on used machine because of depending on region and language settings.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • for /?
  • pause /?
  • rem /?
  • set /?
  • wmic /?

It is also advisable to run wmic OS get localdatetime from within a command line window to see the output of this command line being processed by command for and finally resulting in the date string in format yyyy-mm-dd for usage in name of backup folder.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thank you for this. I get where this is going, but the FOR command is unnecessary for what I was needing. I do appreciate the help though – Dakota Zinn Jan 06 '16 at 10:48
  • @DakotaZinn The command __FOR__ in answer above is needed to get output of __wmic__ usually written to console window captured by __FOR__ and already truncated on first period assigned to the environment variable `LocalDateTime`. You don't need __FOR__ in your batch code as you are using directly region dependent environment variables __DATE__ and __TIME__ as I have suggested, too. I could not offer a solution using __DATE__ and __TIME__ as you have not written in question which format the strings of those two variables have on your computer. – Mofi Jan 06 '16 at 11:43
0

I ended up using this:

@echo off

REM Find Current Year
set varYear=%date:~-4,4%

REM Find Current Month
set varMonth=%date:~-7,2%

REM Find Current Day
set varDay=%date:~-10,2%

REM Find Hour
set varHour=%time:~-11,2%

if %varHour% leq 9 (
call :trim %varHour%
goto :eof
:trim
set varHour=0%*
)
:eof

REM Find Minutes
set varMinutes=%time:~-8,2%

REM Compile Date
set DateTime=%varDay%.%varMonth%.%varYear%__%varHour%.%varMinutes%


mkdir "C:\New\Backup\Folder\%DateTime%_Backup"


xcopy /i "C:\Remote\Server" "C:\New\Backup\Folder\%DateTime%_Backup" /D/S/H/V/C/F/K/Y

exit

I didn't end up having to use the FOR command. I can't seem to understand how it works