Create a file name as a timestamp in a batch job

We have a batch job that runs every day and copies the file to a folder for sending. I also want to take a copy of this file and transfer it to the archive folder with the file name

yyyy-MM-dd.log 

What is the easiest way to do this in a Windows batch job?

I am basically looking for the equivalent of this Unix command:

 cp source.log `date +%F`.log 
+67
windows timestamp cmd batch-file
Jun 30 '09 at 16:03
source share
15 answers
 CP source.log %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.log 

But it depends on the language. I'm not sure if %DATE% localized or depends on the format specified for the short date on Windows.

Here is a locale-independent way to extract the current date from this answer , but it depends on WMIC and FOR /F :

 FOR /F %%A IN ('WMIC OS GET LocalDateTime ^| FINDSTR \.') DO @SET B=%%A CP source.log %B:~0,4%-%B:~4,2%-%B:~6,2%.log 
+85
Jun 30 '09 at 16:19
source share

This worked for me and was a safe file solution:

 C:\ set SAVESTAMP=%DATE:/=-%@%TIME::=-% C:\ set SAVESTAMP=%SAVESTAMP: =% C:\ set SAVESTAMP=%SAVESTAMP:,=.%.jpg C:\ echo %SAVESTAMP% 11-04-2012@20-52-42.79.jpg 

The first command accepts DATE and replaces / with - , takes TIME and replaces : with - and combines them into DATE @TIME format. The second set statement removes any spaces and adds the .jpg extension.

The above code is used in a small script that extracts images from a security IP camera for further processing:

 :while set SAVESTAMP=%DATE:/=-%@%TIME::=-% set SAVESTAMP=%SAVESTAMP: =% set SAVESTAMP=%SAVESTAMP:,=.%.jpg wget-1.10.2.exe --tries=0 -O %SAVESTAMP% http://admin:<password>@<ip address>:<port>/snapshot.cgi timeout 1 GOTO while 
+39
Apr 12 2018-12-12T00:
source share

For French Locale (France) ONLY , be careful because / displayed:

 echo %DATE% 08/09/2013 

For our problem with the log file, here is my suggestion for French Locale ONLY :

 SETLOCAL set LOGFILE_DATE=%DATE:~6,4%.%DATE:~3,2%.%DATE:~0,2% set LOGFILE_TIME=%TIME:~0,2%.%TIME:~3,2% set LOGFILE=log-%LOGFILE_DATE%-%LOGFILE_TIME%.txt rem log-2014.05.19-22.18.txt command > %LOGFILE% 
+14
Sep 08 '13 at 7:10
source share

This is a locale-independent solution (copy to a file named SetDateTimeComponents.cmd):

 @echo off REM This script taken from the following URL: REM http://www.winnetmag.com/windowsscripting/article/articleid/9177/windowsscripting_9177.html REM Create the date and time elements. for /f "tokens=1-7 delims=:/-, " %%i in ('echo exit^|cmd /q /k"prompt $d $t"') do ( for /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do ( set dow=%%i set %%a=%%j set %%b=%%k set %%c=%%l set hh=%%m set min=%%n set ss=%%o ) ) REM Let see the result. echo %dow% %yy%-%mm%-%dd% @ %hh%:%min%:%ss% 

I put all my .cmd scripts in the same folder (% SCRIPTROOT%); any script that needs date / time values ​​will call SetDateTimeComponents.cmd, as in the following example:

 setlocal @echo Initializing... set SCRIPTROOT=%~dp0 set ERRLOG=C:\Oopsies.err :: Log start time call "%SCRIPTROOT%\SetDateTimeComponents.cmd" >nul @echo === %dow% %yy%-%mm%-%dd% @ %hh%:%min%:%ss% : Start === >> %ERRLOG% :: Perform some long running action and log errors to ERRLOG. :: Log end time call "%SCRIPTROOT%\SetDateTimeComponents.cmd" >nul @echo === %dow% %yy%-%mm%-%dd% @ %hh%:%min%:%ss% : End === >> %ERRLOG% 

As the example shows, you can call SetDateTimeComponents.cmd when you need to update the date / time values. Hiding the parsing script in it’s own SetDateTompocents.cmd file is a good way to hide ugly details and, more importantly, avoid typos.

+10
Jun 30 '09 at 18:15
source share

This ensures that the output is a two-digit value ... you can rearrange the output to your liking and testing without commenting on the diagnostic section. Enjoy it!

(I did a lot of this from other forums ...)

 :: ------------------ Date and Time Modifier ------------------------ @echo off setlocal :: THIS CODE WILL DISPLAY A 2-DIGIT TIMESTAMP FOR USE IN APPENDING FILENAMES :: CREATE VARIABLE %TIMESTAMP% for /f "tokens=1-8 delims=.:/-, " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do ( for /f "tokens=2-4 skip=1 delims=/-,()" %%a in ('echo.^|date') do ( set dow=%%i set %%a=%%j set %%b=%%k set %%c=%%l set hh=%%m set min=%%n set sec=%%o set hsec=%%p ) ) :: ensure that hour is always 2 digits if %hh%==0 set hh=00 if %hh%==1 set hh=01 if %hh%==2 set hh=02 if %hh%==3 set hh=03 if %hh%==4 set hh=04 if %hh%==5 set hh=05 if %hh%==6 set hh=06 if %hh%==7 set hh=07 if %hh%==8 set hh=08 if %hh%==9 set hh=09 :: --------- TIME STAMP DIAGNOSTICS ------------------------- :: Un-comment these lines to test output :: echo dayOfWeek = %dow% :: echo year = %yy% :: echo month = %mm% :: echo day = %dd% :: echo hour = %hh% :: echo minute = %min% :: echo second = %sec% :: echo hundredthsSecond = %hsec% :: echo. :: echo Hello! :: echo Today is %dow%, %mm%/%dd%. :: echo. :: echo. :: echo. :: echo. :: pause :: --------- END TIME STAMP DIAGNOSTICS ---------------------- :: assign timeStamp: :: Add the date and time parameters as necessary - " yy-mm-dd-dow-min-sec-hsec " endlocal & set timeStamp=%yy%%mm%%dd%_%hh%-%min%-%sec% echo %timeStamp% 
+6
Sep 14 '11 at 19:08
source share

I often use this and put everything in one copy command. The following instances of example.txt as example_YYYYMMDD_HHMMSS.txt and of course you can change it to suit your preferred format. Quotation marks are needed only if there are spaces in the specification file. If you want to reuse the same date / timestamp, you need to save it in a variable.

 copy "{path}\example.txt" "{path}\_%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt" 
+5
May 4 '16 at 10:04
source share

Perhaps this may help:

 echo off @prompt set date=$d$_ set time=$t$h$h$h echo some log >> %date% %time%.log exit 

or

 echo off set v=%date%.log echo some log >> %v% 
+2
Jun 30 '09 at 16:19
source share

Create a file with the current date as the file name (for example, 2008-11-08.dat)

 echo hello > %date%.dat 

With current date, but without "-" (for example, 20081108.dat)

 echo hello > %date:-=%.dat 
+2
Jun 30 '09 at 17:33
source share

I put together a little C program to print the current timestamp (safe for locale, no bad characters ...). Then I use the FOR command to save the result in an environment variable:

 :: Get the timestamp for /f %%x in ('@timestamp') do set TIMESTAMP=%%x :: Use it to generate a filename for /r %%x in (.\processed\*) do move "%%~x" ".\archived\%%~nx-%TIMESTAMP%%%~xx" 

Here is the link:

https://github.com/HarryPehkonen/dos-timestamp

+2
Feb 20 '14 at 6:14
source share

You can simply determine the current local format and get the date in your format, for example:

 ::for 30.10.2016 dd.MM.yyyy if %date:~2,1%==. set d=%date:~-4%%date:~3,2%%date:~,2% ::for 10/30/2016 MM/dd/yyyy if %date:~2,1%==/ set d=%date:~-4%%date:~,2%%date:~3,2% ::for 2016-10-30 yyyy-MM-dd if %date:~4,1%==- set d=%date:~,4%%date:~5,2%%date:~-2% ::variable %d% have now value: 2016103 (yyyyMMdd) set t=%time::=% set t=%t:,=% ::variable %t% have now time without delimiters cp source.log %d%_%t%.log 
+2
Oct 30 '16 at 12:32
source share

I know this is an old post, but there is a simpler FAR answer (although perhaps it only works on newer versions of Windows). Just use the / t option for the DATE and TIME commands to display only the date or time and not request it, for example:

 @echo off echo Starting test batch file > testlog.txt date /t >> testlog.txt time /t >> testlog.txt 
+2
Jul 20 '17 at 15:39
source share

1) You can download GNU coreutils , which comes with GNU date

2) you can use VBScript , which simplifies date handling in Windows:

 Set objFS = CreateObject("Scripting.FileSystemObject") strFolder = "c:\test" Set objFolder = objFS.GetFolder(strFolder) current = Now mth = Month(current) d = Day(current) yr = Year(current) If Len(mth) <2 Then mth="0"&mth End If If Len(d) < 2 Then d = "0"&d End If timestamp=yr & "-" & mth &"-"& d For Each strFile In objFolder.Files strFileName = strFile.Name If InStr(strFileName,"file_name_here") > 0 Then BaseName = objFS.GetBaseName(strFileName) Extension = objFS.GetExtensionName(strFileName) NewName = BaseName & "-" & timestamp & "." & Extension strFile.Name = NewName End If Next 

Run the script as:

 c:\test> cscript /nologo myscript.vbs 
+1
Jul 04 '09 at 3:41
source share

I know this thread is outdated, but I just want to add it here because it really helped me figure this out and its clean look. The best part is that you can put it in a loop for a batch file that always works. Server log or something like that. This is what I use anyway. Hope this ever helps someone.

 @setlocal enableextensions enabledelayedexpansion @echo off call :timestamp freshtime freshdate echo %freshdate% - %freshtime% - Some data >> "%freshdate - Somelog.log" :timestamp set hour=%time:~0,2% if "%hour:~0,1%" == " " set hour=0%hour:~1,1% set min=%time:~3,2% if "%min:~0,1%" == " " set min=0%min:~1,1% set secs=%time:~6,2% if "%secs:~0,1%" == " " set secs=0%secs:~1,1% set FreshTime=%hour%:%min%:%secs% set year=%date:~-4% set month=%date:~4,2% if "%month:~0,1%" == " " set month=0%month:~1,1% set day=%date:~7,2% if "%day:~0,1%" == " " set day=0%day:~1,1% set FreshDate=%month%.%day%.%year% 
+1
Aug 18 '15 at 10:58
source share

This works well with (my) German, it should be possible to customize it to your needs ...

 forfiles /p *PATH* /m *filepattern* /c "cmd /c ren @file %DATE:~6,4%%DATE:~3,2%%DATE:~0,2%_@file" 
0
Oct 28 '17 at 19:26
source share

Because the idea of ​​breaking %DATE% and %TIME% to one side and merging them together seems fragile at best, here is an alternative that uses powerhell oneliner:

 for /f %i in ('powershell -c "get-date -format yyyy-MM-dd--HH-mm-ss"') do @set DATETIME=%i set LOGFILE=my-script-%DATETIME%.txt 

The link for get-date here , with format options for the .NET and UNIX style.

0
Feb 08 '18 at 16:14
source share



All Articles