Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Batch: How to append the current date and time of a format (yyyymmdd.hhmmss) in a filename?

Tags:

batch-file

for /D %%A in () do "C:\Program Files\7-Zip\7z.exe" a -tzip "%%A.zip" -xr!.bat "%%A" -r -x!*.xls

The above batch will automatically zip the files in a directory.. While zipping I want to append date and time in a format (YYYYMMDD.HHMMSS).. For example,say the folder name is "University".If I run the batch file, the final zip name should create as "University_YYYYMMDD.HHMMSS.zip"..

like image 377
user2991622 Avatar asked Nov 15 '13 04:11

user2991622


People also ask

How do I get the current date and time in CMD?

On a Microsoft Windows system, you can obtain the current date using the date /t command (the /t option prevents the command from prompting for a change to the the date) or by using echo %date% to display the contents of the date environment variable.

How do I format a date in DOS?

Type date without parameters to display the current date setting and a prompt for a new date. Press Enter to keep the same date. If you provide a date in the format MM-DD-YY, the system date is set to that date. When specifying a two-digit year, the digits 00-99 correspond to the years 1980-2099.


1 Answers

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 "fullstamp=%YYYY%%MM%%DD%.%HH%%Min%%Sec%"

for /D %%A in (*) do "C:\Program Files\7-Zip\7z.exe" a -tzip "%%A_%fullstamp%.zip" -xr!.bat "%%A" -r -x!*.xls
like image 66
foxidrive Avatar answered Oct 23 '22 01:10

foxidrive