0

I'm creating a batch file to run some back-ups. But when i try to give a file a name with the current datetime it gives a weird result.

I'm using:

backup.backup_%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%_%time:~0,2%%time:~3,2%%time:~6,2%.backupfile

Result: backup.backup_0130-5-_101151.backupfile

Does anyone know how to fix this?

Companjo
  • 1,789
  • 18
  • 24
  • Can you open a command shell and type `echo %DATE%` and post the result please. The `%DATE:~10,4%` bit is extracting 4 characters starting at char 4 of the system date variable. The problem is that your system has a different date format to the system that command was originally written for. So it will be a matter of identifying the appropriate substrings of %DATE% to supply year, month, and date – GregHNZ Oct 25 '13 at 08:37

4 Answers4

2

I think you should use wmic instead of the date and time to get your current date and time without getting affected by the regional settings

@echo off
SETLOCAL EnableDelayedExpansion


for /f "skip=1 tokens=1-6 delims= " %%a in ('wmic path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do (
    IF NOT "%%~f"=="" (
        set /a FormattedDate=10000 * %%f + 100 * %%d + %%a
        set FormattedDate=!FormattedDate:~0,4!!FormattedDate:~-4,2!!FormattedDate:~2,2!
        set /a FormattedTime=%%b * 10000 + %%c * 100 + %%e
        set FormattedTime=!FormattedTime:~0,2!!FormattedTime:~-4,2!!FormattedTime:~-2,2!
    )
)

echo backup.backup_!FormattedDate!_!FormattedTime!
PAUSE

The code above will be naming the file in the following format:

backup.backupYYYYMMDD_HHMMSS

e.g. backup.backup_20131013_163800

Hope this helps.

Community
  • 1
  • 1
Dale
  • 1,903
  • 1
  • 16
  • 24
2

try this:

@ECHO OFF &SETLOCAL
for /f "skip=1delims=." %%a in ('wmic os get localdatetime') do if not defined DateTime set "DateTime=%%a"
echo(backup.backup_%DateTime:~0,8%_%DateTime:~8%
Endoro
  • 37,015
  • 8
  • 50
  • 63
1

If you have to use this more than once in your batch file, It's nice to have it as a function. This might help:

:GetFileDateTime Format ret
@echo off & setlocal
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%" & set "timestamp=%HH%.%Min%.%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%.%Min%.%Sec%"
( ENDLOCAL
    if /i "%~1"=="DT" set "%~2=%datestamp% %timestamp%"
    if /i "%~1"=="TD" set "%~2=%timestamp% %datestamp%" 
    if /i "%~1"=="D" set "%~2=%datestamp%"
    if /i "%~1"=="T" set "%~2=%timestamp%"
    if /i "%~1"=="F" set "%~2=%fullstamp%"
)
exit /b


Usage Example:

call :GetFileDateTime TD timedate
echo %timedate%

call :GetFileDateTime D date
echo %date%
Matt Williamson
  • 6,947
  • 1
  • 23
  • 36
0

I also suggest Wmic for XP pro and higher. I post this snippet often:

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%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
foxidrive
  • 40,353
  • 10
  • 53
  • 68