TechKnow

Author Topic: [BAT] Date, Hour and Minute (time) in filename  (Read 5226 times)

0 Members and 1 Guest are viewing this topic.

Offline HcH

  • TechKnow Owner, Ethical Hacker
  • Administrator
  • Uber Member
  • *****
  • Posts: 9936
  • Karma: +2284/-111
[BAT] Date, Hour and Minute (time) in filename
« on: November 22, 2011, 10:49:29 PM »
Hello again! I get this question very many times and I read a lot of users asking for a code that is able to perform generation of a file (usually a backup) with the date and time included. Why is this so difficult you might wonder, well because certain characters are not supported for a filename, the file won't be created at all.

For EURO:
Code: [Select]
for /F "tokens=2-4 delims=- " %%i in ('date /t') do set yyyymmdd=%%k%%i%%j
for /F "tokens=1-2 delims=: " %%l in ('time /t') do set hhmm=%%l%%m
echo %yyyymmdd%_%hhmm%
pause

For USA:
Code: [Select]
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd=%%k%%i%%j
for /F "tokens=1-2 delims=: " %%l in ('time /t') do set hhmm=%%l%%m
echo %yyyymmdd%_%hhmm%
pause
[size=85]*see the / delimiter instead of the euro - delimiter[/size]

This script is most useful to combine with a backupscript, you can use the var "%yyyymmdd%_%hhmm%" to get the date/time in your filename, for example:

Code: [Select]
rar a "BACKUP_%yyyymmdd%_%hhmm%.rar" FILES
« Last Edit: October 20, 2016, 05:18:31 PM by HardcoreHacker »