Link to home
Start Free TrialLog in
Avatar of mjacobs2929
mjacobs2929

asked on

Simplest way to create unique filename in DOS batch file?

I have loads of tools for doing this in other programming languages but can't find equivalents in the various guides to batch file commands.

I need the simplest way, using standard dos commands and variables in a batch file,  to rename or copy a file called "image.bmp" to a unique filename, preferably, though not crucially, with the same bmp extension.

My first thought was to use an unambigous date time construct (eg 20140710221521 from yyyymmddhhmmss)

but couldn't see any examples of how to do that. So then went looking for any other form of unique id generator.

So far I've found random number generators, sha1 generators and a few other examples all capable of creating unique strings but it's not at all obvious how, for example to make that sha1 string parser [http://www.robvanderwoude.com/files/sha1_php_bat.txt] operate on the image.bmp file instead, capture the result and copy the file to a file of that name (with or without the bmp extension). And, in any case, that one and some of the others require the installation of php, which is definitely more hassle than the job's worth.

Suggestions?
Avatar of Kimputer
Kimputer

Does it have to be a batch file? From Windows 2000 on, vbscript is a very nice option (code is also more readable). You can run it from the commandline also if you want.
You can generate timestamp using a DOS batch file:
@ECHO OFF
SET TS=%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%-%TIME:~9,2%
ECHO %TS%

Open in new window

This will print yyyy-MM-dd-HH-mm-ss-tt (tt => milliseconds)

Of course, the code above works based on specific OS culture (date / time format). My PC has date format set to MM/dd/yyyy and time as HH:mm:ss.tt

You might have to tweak the second line based on your PC's culture settings.
Avatar of mjacobs2929

ASKER

That date time capture looks almost like what I need, but, of course, I don't want to display the result. I want to use it as a filename.

Just tried

SET TS=%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%-%TIME:~9,2%
copy image.bmp image%TS%.bmp

for example and it failed.

How should I have done it?
I performed exact steps you mentioned - it works for me. Nothing seems to be wrong in what you are doing. Can you mention what error are you getting (assuming you get some error)?

On a different note, you might want to "ECHO" the "TS" variable just to ensure it has appropriate characters. As I mentioned earlier, the code is specific to date and time formats of the PC.
aha. The echo reveals the problem:

TS comes out as

2014-7/--01-21-42-14-23

obviously the forward / makes it an illegal filename.

Any idea how to suppress it?
changing to
SET TS=%DATE:~-4%-%DATE:~4,1%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%-%TIME:~9,2%

produces
2014-7--01-21-42-14-23

and works (I get the file copied with that name)

but what will happen when we get to the the 2 digit months?
As I mentioned earlier, the code is very specific to OS date format. Your date format (OS culture setting in Control Panel) has M (meaning single digit months are shown without a leading 0)

If you change the date format in your PC to MM, my earlier code will work for all months. The format in your PC is a single M
yes, I understood that, but unfortunately my machine date format is already set to dd/MM/yyy (short) and dd MMMM yyyy (long) so I figured something else must be required. I've actually tried changing the format to YYYY MM DD which produces a filename in form: 7-12----1-13-59-57-57.bmp which, again, I could live with though it's not obvious what happened to the "YYYY" data or what will happen when we reach a 2 digit month number
On a command prompt, can you run this command and let me know what is printed?
ECHO %DATE%
currently 2014-07-14
ASKER CERTIFIED SOLUTION
Avatar of Raghu Mutalikdesai
Raghu Mutalikdesai
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bingo. That's got it. Thanks for your help and patience. Enjoy your 500 points
Cool! Good to know your problem is solved. Thanks!
thanks it swork