views:

428

answers:

2

So I am running a program currently and it outputs an html file to a static location/name and i want to keep all of them, a log pretty much.

How would i go about changing the name of that file to something different, and unique, and possibly change the location of that to a log folder via command line. I was thinking of adding a timestamp into the name of the file, but I have no clue how to go about that, which is why I'm here.

EDIT: I'm doing this on Windows Server 2003. So it will be in the cmd.exe shell. I'm running this all through CC.NET so I could easily run a batch file with commands in it. The CC.NET project builds an HTML file with the same name every time in the same location, so it overwrites the old one.

So i want to move the old one and rename it so I don't run into the same overwrite problem with moving them to a different location.

Short and simple: How do I take a file, move it, rename it uniquely(timestamp?), within the windows command shell?

+2  A: 

In Bash, you can do something similar to:

#!/bin/bash

FILE=$(date +%Y-%B-%d_%H:%M:%S)  # FILE will contain `2009-June-18_20:17:13'
mv OLD_FILE, FILE

Or, simply use the seconds elapsed since 1970: date +%s

Alan Haggai Alavi
+2  A: 

The easiest way to do this would be to use %RANDOM% - for example:

ren foo.txt foo-%RANDOM%.txt

You could go crazy and have two randoms to decrease the chance of collision (or just write a simple error handling loop) - like:

ren foo.txt foo-%RANDOM%-%RANDOM%.txt

An alternative, if you really want times, is that you can parse out the %TIME% environment variable. I don't know how this works in different locales and when the system time changed you be at risk to have a duplicate value ... but here's the process to understand it:

First look at:

> echo %TIME%

It's something like:

11:16:19.74

Let's pull out the 11:16 to "1116"

> echo %time:~,2%%time:~3,2%

What we're going there is pulling out the substring by indicating the start and stop points in the variable. You can take that all the way out to the hundredth of a second - i.e. you could have gotten:

11161974

Bubbafat
Did just the trick! Just used MOVE instead of ren and also added the same logic to the %date% and I got what I wanted :).
Jared