1

I have a batch script that starts a server jar file, but before it runs I want it to make a copy of everything in the working directory and paste it in a new folder named with the current date and time, with the new folder outside the working directory. How do I fix this script?

@echo off
xcopy \ "C:\Users\my name\serverBackups\%date%" /s/e /h /z /f /l
java -Xms256M -Xms1G -d64 -jar server.jar 
pause
Dan Fiscus
  • 97
  • 1
  • 8
  • What error do you get? Besides depending on your date format (espacially the separator) you may not be able to create a folder/file with the date in it. It may contain `:` for eg. which is used to indicate a drive. – J.Baoby Feb 05 '17 at 07:29
  • Possible duplicate of [Batch: syntax error on %time% with environment variables](http://stackoverflow.com/questions/31241105/batch-syntax-error-on-time-with-environment-variables) – JosefZ Feb 05 '17 at 08:28

1 Answers1

0

This batch code could be used for the task:

@echo off
setlocal EnableExtensions
for /F "skip=1 tokens=1 delims=." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime') do set "DateTimeLocal=%%I" & goto ReformatDateTime
:ReformatDateTime
set "DateTimeFolder=%DateTimeLocal:~0,4%-%DateTimeLocal:~4,2%-%DateTimeLocal:~6,2%_%DateTimeLocal:~8,2%-%DateTimeLocal:~10,2%-%DateTimeLocal:~12,2%"
%SystemRoot%\System32\xcopy.exe * "%USERPROFILE%\ServerBackups\%DateTimeFolder%\" /C /E /H /I /K /Q /R /Y
java.exe -Xms256M -Xms1G -d64 -jar server.jar 
endlocal

The first command line executed by FOR is:

C:\Windows\System32\wbem\wmic.exe OS GET LocalDateTime

This command outputs to handle STDOUT for example following two lines:

LocalDateTime
20170205125750.125000+060

This output is processed now by command FOR.

The first line is not of interest and therefore skipped because of option skip=1.

From the second line just the first string referenced with tokens=1 up to the point used as string delimiter with delims=. is of interest as it contains the current local date and time in format YYYYMMDDhhmmss. The date/time string is assigned to environment variable DateTimeLocal.

The advantage of using wmic OS GET LocalDateTime is that the date/time format is independent on Windows Region and Language settings whereas the format of date string of environment variable DATE and format of time string of environment variable TIME depends on Windows Region and Language settings.

The disadvantage of using wmic OS GET LocalDateTime is that it takes much longer to execute this command in comparison to referencing the values of the environment variables DATE and TIME. But this disadvantage is of no problem here on running wmic just once per batch file execution.

The date/time string is next reformatted to YYYY-MM-DD_hh-mm-ss using string substitution for making date/time easier readable for a human. The folders sorted by name are automatically sorted also by date/time by using this format. But other formats like DD.MM.YYYY hh.mm.ss would be also possible.

So the string 20170205125750 is reformatted to 2017-02-05_12-57-50 and assigned to environment variable DateTimeFolder.

This reformatted date/time string is used next in the XCOPY command line where it is important that the target folder string ends with a backslash to make it clear for xcopy that the target specifies a directory and not a file.

Another solution is using additionally the wmic option /VALUE:

%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE

This option changes the output format to:

LocalDateTime=20170205125750.125000+060

This makes it possible to simplify the batch code a little bit:

@echo off
setlocal EnableExtensions
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "DateTimeLocal=%%I"
set "DateTimeFolder=%DateTimeLocal:~0,4%-%DateTimeLocal:~4,2%-%DateTimeLocal:~6,2%_%DateTimeLocal:~8,2%-%DateTimeLocal:~10,2%-%DateTimeLocal:~12,2%"
%SystemRoot%\System32\xcopy.exe * "%USERPROFILE%\ServerBackups\%DateTimeFolder%\" /C /E /H /I /K /Q /R /Y
java.exe -Xms256M -Xms1G -d64 -jar server.jar 
endlocal

Here is also an example using the environment variables DATE and TIME expecting date and time in format for German countries which means DATE is using the format DD.MM.YYYY and TIME is using the format hh:mm:ss,xx using 24 hours format and leading zeros for day, month, hour, minute and second smaller than 10 which means for the date/time example above 05.02.2017 for DATE and 12:57:50.12 for TIME.

@echo off
setlocal EnableExtensions
set "DateTimeFolder=%DATE:~-4%-%DATE:~-7,2%-%DATE:~-10,2%_%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%"
%SystemRoot%\System32\xcopy.exe * "%USERPROFILE%\ServerBackups\%DateTimeFolder%\" /C /E /H /I /K /Q /R /Y >nul
endlocal

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 /?
  • endlocal /?
  • for /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic OS /?
  • wmic OS GET /?
  • xcopy /?
Mofi
  • 46,139
  • 17
  • 80
  • 143