Create unique filename - datetimestamp

  • Thread starter Thread starter Cliff Cotterill
  • Start date Start date
C

Cliff Cotterill

How do you create a unique filename in a batch file?
Can the TIME /t and DATE /t be concatenated to a filename variable?
 
Cliff said:
How do you create a unique filename in a batch file?
Can the TIME /t and DATE /t be concatenated to a filename variable?
- - - - - - - - - - begin screen capture WinXP - - - - - - - - - -
C:\cmd>demo\Timestamp123
20050317112924

C:\cmd>rlist demo\Timestamp123.cmd
=====begin C:\cmd\demo\Timestamp123.cmd ====================
1. @echo off
2. setlocal
3. for /f "tokens=2-4 delims=/ " %%a in (
4. 'date /t'
5. ) do set zdate=%%c%%a%%b
6. for /f "tokens=5-7 delims=:. " %%a in (
7. 'echo/^|time^|find "current"'
8. ) do set ztime=%%a%%b%%c
9. echo %zdate%%ztime%
=====end C:\cmd\demo\Timestamp123.cmd ====================
- - - - - - - - - - end screen capture WinXP - - - - - - - - - -
 
Cliff said:
How do you create a unique filename in a batch file?
Can the TIME /t and DATE /t be concatenated to a filename variable?
Hi

One way is to from your batch file create a temporary vbscript
file on the fly and pick up the result, more here:

http://groups.google.co.uk/[email protected]

and
http://www.jsiinc.com/SUBR/tip8600/rh8600.htm


You can do it with "pure" batch file as well, using e.g the batch date
time functions from Ritchie Lawrence batch library available at
http://www.commandline.co.uk/lib
 
Torgeir Bakken (MVP) said:
Hi

One way is to from your batch file create a temporary vbscript
file on the fly and pick up the result, more here:

http://groups.google.co.uk/[email protected]

and
http://www.jsiinc.com/SUBR/tip8600/rh8600.htm


You can do it with "pure" batch file as well, using e.g the batch date
time functions from Ritchie Lawrence batch library available at
http://www.commandline.co.uk/lib

A filename derived from the time and date is only guaranteed to be unique
if:

- the clock is never turned back, and if:
- such files are NEVER created outside of the script that uses the feature.

I would tend to add a random field (a la %random%), and check for existence
before assuming otherwise. If the file already existed, the script would
just re-build the name and try again.

/Al
 
Back
Top