Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

DOS/Other DOS/Other
Search Search
Search for:
Tech Support Guy > > >

Append a date to a filename in DOS


(!)

cgjoker's Avatar
cgjoker cgjoker is offline
Member with 205 posts.
THREAD STARTER
 
Join Date: Aug 2003
26-Jan-2005, 05:16 PM #1
Append a date to a filename in DOS
Hi there. Me again.

Thought id put out a new thread to see if anyone can help me.

Id like to rename a file in DOS to include 'date /T' in the filename.

Anyone?

thanks, cg.
squidboy's Avatar
squidboy squidboy is offline
Computer Specs
Senior Member with 403 posts.
 
Join Date: Dec 2004
Location: Bradenton, FL
Experience: Advanced
27-Jan-2005, 01:41 AM #2
Quote:
Originally Posted by cgjoker
Hi there. Me again.

Thought id put out a new thread to see if anyone can help me.

Id like to rename a file in DOS to include 'date /T' in the filename.

Anyone?

thanks, cg.
Howdy,

I remember your previous thread. It's somewhat tricky because those commands will output '/' and such which can't be used in file names.

I would suggest a site but there are many with folks that live and breathe this stuff /giggle. Just do search in google for dos batch files or scripting and that should get you started.
plejon's Avatar
plejon plejon is offline
Member with 665 posts.
 
Join Date: Jul 2001
Location: Willebroek / Belgium
Experience: Intermediate
29-Jan-2005, 08:51 AM #3
Try to rename the file using the date and time.

ren file.txt file-%date%-%time%.txt

I had some issues with the date not being displayed in the exact format I wanted, but you can use substrings and other techniques to get exactly what you want

E.g. : ren file.txt file%date:~3,10%-%time%.txt

For more info on the %date% and %time% variables and substringing, just open a Command Prompt box and type set /?
cgjoker's Avatar
cgjoker cgjoker is offline
Member with 205 posts.
THREAD STARTER
 
Join Date: Aug 2003
31-Jan-2005, 10:39 AM #4
thanks...
i actually figured it out though. well, with some help. here is the syntax i ended up with..

ren tmp.txt tmp%date:~4,2%%date:~7,2%%date:~10,4%
fmgazette's Avatar
fmgazette fmgazette is offline
Junior Member with 1 posts.
 
Join Date: Feb 2005
Experience: Advanced
22-Feb-2005, 08:46 PM #5
Quote:
Originally Posted by cgjoker
i actually figured it out though. well, with some help. here is the syntax i ended up with..

ren tmp.txt tmp%date:~4,2%%date:~7,2%%date:~10,4%
Thanks for this guys - this was just what I was looking for too.
I used...

%DATE:/=%

...to remove the offending / characters.
workingman's Avatar
workingman workingman is offline
Junior Member with 6 posts.
 
Join Date: Mar 2005
Experience: Intermediate
22-Apr-2005, 04:34 PM #6
I got the idea from this site as to how to append date/time. I noticed that when you execute the ren dos command makes a difference. See bat file below.


Echo Off
IF Not EXIST n:\dbfs\doekasp.txt GOTO DoNothing

copy n:\dbfs\doekasp.txt n:\history\doekasp.txt

Rem rename command is done twice (2) to allow for 1 or 2 digit hour,
Rem If before 10am (1digit) hour Rename starting at location (0) for (2) chars,
Rem will error out, as location (0) will have a space
Rem and space is invalid character for file name,
Rem so second remame will be used.
Rem
Rem if equal 10am or later (2 digit hour) then first remame will work and second will not
Rem as doekasp.txt will not be found (remamed)


ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.txt
ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.txt



oNothing
pdxbda's Avatar
pdxbda pdxbda is offline
Junior Member with 2 posts.
 
Join Date: Jun 2005
Experience: Beginner
07-Jun-2005, 12:08 PM #7
It's not working for me!
I've tried:

ren h:\QMDB_Archive\QMDB_be.mdb QMDB_be-%date:~4,2%-%date:~7,2%-%date:~10,4%.mdb

...and I get a syntax error message. Where am I going wrong?
cgjoker's Avatar
cgjoker cgjoker is offline
Member with 205 posts.
THREAD STARTER
 
Join Date: Aug 2003
07-Jun-2005, 04:44 PM #8
try removing the " - " in front of the %date ... so...

ren h:\QMDB_Archive\QMDB_be.mdb QMDB_be%date:~4,2%%date:~7,2%%date:~10,4%.mdb
pdxbda's Avatar
pdxbda pdxbda is offline
Junior Member with 2 posts.
 
Join Date: Jun 2005
Experience: Beginner
07-Jun-2005, 06:42 PM #9
Thanks. But, I still get the error message. I'm using XP. Would that affect the dos commands?
workingman's Avatar
workingman workingman is offline
Junior Member with 6 posts.
 
Join Date: Mar 2005
Experience: Intermediate
08-Jun-2005, 08:18 AM #10
Quote:
Originally Posted by workingman
I got the idea from this site as to how to append date/time. I noticed that when(time of day) you execute the ren dos command makes a difference. See bat file below.


Echo Off
IF Not EXIST n:\dbfs\doekasp.txt GOTO DoNothing

copy n:\dbfs\doekasp.txt n:\history\doekasp.txt

Rem rename command is done twice (2) to allow for 1 or 2 digit hour,
Rem If before 10am (1digit) hour Rename starting at location (0) for (2) chars,
Rem will error out, as location (0) will have a space
Rem and space is invalid character for file name,
Rem so second remame will be used.
Rem
Rem if equal 10am or later (2 digit hour) then first remame will work and second will not
Rem as doekasp.txt will not be found (remamed)


ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.txt
ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.txt



oNothing
workingman's Avatar
workingman workingman is offline
Junior Member with 6 posts.
 
Join Date: Mar 2005
Experience: Intermediate
08-Jun-2005, 08:36 AM #11
ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.txt

ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.txt


This is the output from commands above: doekasp-06-07-2005_@_8h01m09s.txt(note that "_" is in front of and behind "@" editor here displays all text as underlined)[FONT=System]

Note that if you use time in your file name.. be aware that you need to code for 1 and or 2 digit hour.

I had to put some dos pause commands to view what was going on and make sure I was not using invalid char(s) for file names.

Ps.. go to dos prompt and enter Date command and then Time command to see character strings your parsing in the rename command when your building your file name.

For Date: Wed 06/08/2005 You start counting from 0 starting with Wed and the add 1 as you move to the right.. The 5th char would be the nbr 6 in this example.

Do the same for time. just remember that you can not take first 2 chars. of time when time is displaying 1 digit hour as you are trying to rename your file with a space+number and space is invalid character for filename.

Last edited by workingman; 08-Jun-2005 at 08:57 AM..
Mark180's Avatar
Mark180 Mark180 is offline
Junior Member with 3 posts.
 
Join Date: Jul 2005
Experience: Intermediate
08-Jul-2005, 04:52 AM #12
Hi

I have trying to use the above solution and have come up with this syntax

REN E:\Sx3\FTP\pl.txt pl%date:~10,4%%date:~7,2%%date:~4,2%%time:~1,2%%time:~3,2%%.txt
ren E:\Sx3\FTP\llpg.txt llpg%date:~10,4%%date:~7,2%%date:~4,2%%time:~1,2%%time:~3,2%%.txt
pause

the required output is plYYYYMMDDHHmm.txt and llpgYYYYMMDDHHmm.txt

this runs fine from my machine - but the syntax is incorrect when i run from the server - I'm a complete novice in this area - can anyone help?

(NB I have tried a simple REN E:\Sx3\FTP\pl.txt mark.txt which works fine)

Thanks

Last edited by Mark180; 08-Jul-2005 at 05:40 AM..
Mark180's Avatar
Mark180 Mark180 is offline
Junior Member with 3 posts.
 
Join Date: Jul 2005
Experience: Intermediate
08-Jul-2005, 06:57 AM #13
Finally got it sussed used the following for my rename.bat

@echo off
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd=%%k%%i%%j
echo Date: %yyyymmdd%
for /F "tokens=1-2 delims=: " %%l in ('time /t') do set hhmm=%%l%%m
echo Time: %hhmm%
rem -- or use both
echo %yyyymmdd%%hhmm%
rem -- here is the copy command with just the date appended

REN E:\Sx3\FTP\pl.txt pl%yyyymmdd%%hhmm%.txt
REN E:\Sx3\FTP\llpg.txt llpg%yyyymmdd%%hhmm%.txt
Mark180's Avatar
Mark180 Mark180 is offline
Junior Member with 3 posts.
 
Join Date: Jul 2005
Experience: Intermediate
12-Jul-2005, 09:51 AM #14
oh that isn't working!

2 questions

1) why is my month and day coming out in the wrong position in the file name (it comes out yyyyddmm - i want yyyymmdd)

2) why if the scirpt is run at 0430 is the time hhmm coming out as 430 not 0430
rch's Avatar
rch rch is offline
Junior Member with 1 posts.
 
Join Date: Aug 2008
Experience: Intermediate
15-Aug-2008, 11:56 AM #15
If I type in the following command line in the dos window:

7z a -xr!*.tmp C:\backupfiles_%date:/=%.zip D:\backupfiles\

I will get the file name correctly as ==> backupfiles_08152008.zip

But if I run the same exactly command line in the schedule job in a bak.bat or bak.cmd file, the file name became ==> backupfile_Fri.7z

Any idea why?
I am using the freeware 7-zip in the command line. Suggestion is very welcome if something better than 7-zip and also free I can use as command line in the scheduled job.

Thank you very much!
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑