Syntax to timestamp a file in a batch file

dsteph944
dsteph944 used Ask the Experts™
on
Hi,
I need to date/timestamp a file as I move it to an archive directory.  The current syntax I'm using is:

copy imports\data\mftp*.* imports\data\archive
cd imports\data\archive
for %i in (*) do rename *.* %date%%i.*

I get invalid syntax on the last command.  I thought I had this working previously.  Please help.  Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hi There,

I have checked your script and come up with a solution. I need a little more info though.

The files you are archiving all start with mftp according to your script. Do they all have different file extensions.

ie. mftpdata.bin , mtfpuser.dat, mftpsettings.cfg etc.

If not, the script I have won't work because you will get filename duplications when you rename to date.*

as mtfpdata.bin would be 05-12-02.bin and mftpuser.bin would be the same.

If all extensions are different then no problem.

I do have an alternative script that will workaround but please advise first what your exact requirements are.

Regards,


Nadeem . . .

Author

Commented:
The files all end with .txt.  They are names like mftptrade.txt, mftpposition.txt etc.  I would like them to be mftptrade12042002-12:30:00.txt or something like that.  The time doesn't have to be there but i need the original file name plus the date and then the file extension of .txt.  Thanks!

Commented:

Hi Again,

I can wrap this one up for you now if we can agree on 1 issue.

How about rather than mftptrade05-12-2002.txt I could get you 05-12-2002mftptrade.txt

Would that suffice ? ? ?

Regards,


Nadeem . . .
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
I'll take it.  I'm not picky!!  Thanks.

Commented:

Ok,

I haven't had to do this before, so it took some effort.

But,

Here,

It,

Is ! ! !

copy imports\data\*.* imports\data\archive
cd imports\data\archive
for %%i in (*.*) do rename %%i %date%%%i

Obviously you can see that I have used *.* rather than mftp.* but you can adjust this fine.

I have tested and all works OK.


Regards,


Nadeem . . . .

Commented:

Oh. by the way I forgot to mention.

You have to change the date seperator in regional settings to a - rather than a / as you cannot have / in a filename.

Regards,


Nadeem . . .  

Author

Commented:
I'm getting a syntax error.  I'm running on a windows 2000 advanced server.  Is the syntax different for that?

Commented:

I can see the problem, give me a couple of minutes.


Nadeem . . .

Commented:
Hi There,

OK the problem is that in Windows 2000 the date command returns Day Date.

ie Thu 05-12-2002, whereas other O/s returns only 05-12-2002

I am not a batch expert, but you need to trim the THU from the Date string.

You should be able to do this by declaring a variable and trim statement on the results of the date string.

I will try to do this for you asap, but if you want to give it a shot as well then maybe we can resolve together.

Other that that, the script works 100% it's just a Win2k querk I'm afraid.


Regards,


Nadeem . . .

Commented:
I think you need to look at the Short Date Fromat settings in the Regional Options. I beleive that is what is causing the descrepency.


The Crazy One

Commented:

Hi Crazyone,

Already tried that, my first choice.

No facility in Regional Settings to get rid of the day.

It seems to be a Windows 2000 Command Shell function to return the day as part of the result.

I'm sure we can trim in batch as you can do in VB etc.

Regards,


Nadeem . . .

Commented:

Hi Crazyone,

Already tried that, my first choice.

No facility in Regional Settings to get rid of the day.

It seems to be a Windows 2000 Command Shell function to return the day as part of the result.

I'm sure we can trim in batch as you can do in VB etc.

Regards,


Nadeem . . .

Commented:

Ok,

I've finally got it and it works ! ! !

Insert this line in the batch file and it will omit the day from the result.

FOR /f "tokens=2-4 delims=/ " %%a in ('DATE/T') do SET date=%%c%%a%%b


Regards,


Nadeem . . .
Commented:

Hi dsteph944,

What Outcome? ? ?. Worked 100% for me on Windows 2000 Server. Have re-tested many times now.

It would be nice to recieve the points, had to work hard on this one.

Regards,


Nadeem . . .

Author

Commented:
Thanks Nadeem.  Sorry I didn't reply sooner.  As soon as it worked I started fixing and testing things and lost track of updating the question.  Great job.  Thanks again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial