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?
mjacobs2929Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KimputerCommented:
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.
0
Raghu MutalikdesaiSenior Manager in a leading IT Services companyCommented:
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.
0
mjacobs2929Author Commented:
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?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Raghu MutalikdesaiSenior Manager in a leading IT Services companyCommented:
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.
0
mjacobs2929Author Commented:
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?
0
mjacobs2929Author Commented:
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?
0
Raghu MutalikdesaiSenior Manager in a leading IT Services companyCommented:
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
0
mjacobs2929Author Commented:
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
0
Raghu MutalikdesaiSenior Manager in a leading IT Services companyCommented:
On a command prompt, can you run this command and let me know what is printed?
ECHO %DATE%
0
mjacobs2929Author Commented:
currently 2014-07-14
0
Raghu MutalikdesaiSenior Manager in a leading IT Services companyCommented:
If the ECHO %DATE% produces 2014-07-14, then it is very simple:
SET TS=%DATE%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%-%TIME:~9,2%
COPY /Y image.bmp image%TS%.bmp

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mjacobs2929Author Commented:
Bingo. That's got it. Thanks for your help and patience. Enjoy your 500 points
0
Raghu MutalikdesaiSenior Manager in a leading IT Services companyCommented:
Cool! Good to know your problem is solved. Thanks!
0
Gunasekar vCommented:
thanks it swork
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.