Link to home
Start Free TrialLog in
Avatar of dsteph944
dsteph944

asked on

Syntax to timestamp a file in a batch file

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.
Avatar of NadeemV
NadeemV
Flag of United Kingdom of Great Britain and Northern Ireland image

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 . . .
Avatar of dsteph944
dsteph944

ASKER

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!

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 . . .
I'll take it.  I'm not picky!!  Thanks.

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 . . . .


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 . . .  
I'm getting a syntax error.  I'm running on a windows 2000 advanced server.  Is the syntax different for that?

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


Nadeem . . .
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 . . .

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

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 . . .


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 . . .


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 . . .
ASKER CERTIFIED SOLUTION
Avatar of NadeemV
NadeemV
Flag of United Kingdom of Great Britain and Northern Ireland 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
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.