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?

1
  • 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 Commented Oct 25, 2013 at 8:37

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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%

1 Comment

Fails when time is 9:00 instead of 09:00
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%

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.