# Append a date to a filename in DOS



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


----------



## squidboy

cgjoker said:


> 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

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

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

cgjoker said:


> 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

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%[email protected]_%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%[email protected]_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.txt



oNothing


----------



## pdxbda

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

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

Thanks. But, I still get the error message. I'm using XP. Would that affect the dos commands?


----------



## workingman

workingman said:


> 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%[email protected]_%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%[email protected]_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.txt
> 
> oNothing


----------



## workingman

ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%[email protected]_%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%[email protected]_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.txt

This is the output from commands above: [email protected]_8h01m09s.txt(note that "_" is in front of and behind "@" editor here displays all text as underlined)

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.


----------



## Mark180

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


----------



## Mark180

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

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

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!


----------



## TheOutcaste

It's generally best to start your own thread instead of bringing up an old one.

The reason for the difference is that the scheduled task is running under a different user account than the one you are using when you open the command prompt and it's regional settings are different.
The format of the %date% variable depends on the regional settings. For example, under English (United States) it will be Sat 08/16/2008 and under English (Caribbean) it will be just 08/16/2008.
The space after the day of the week is the problem. Your command line will look like this:

7z a -xr!*.tmp C:\backupfiles_Sat 08162008.zip D:\backupfiles\

It sees the archive name as C:\backupfiles_Sat and adds the default .7z extension creating an archive named C:\backupfiles_Sat.7z. It sees 08162008.zip and D:\backupfiles\ as the files to add, and most likely gives an error that it can't find 08162008.zip, though it should add all of the files from D:\backupfiles\. You might not see the error when running as a scheduled job as the window closes too fast.

Because of the space you must put the archive name in quotes:
7z a -xr!*.tmp "C:\backupfiles_%date:/=%.zip" D:\backupfiles\
This will create a file named C:\backupfiles_Sat 08162008.zip.

If you don't want the day of the week, you can either log into the account you run the scheduled tasks under and change the regional settings, or use a variable and then specify a substring to extract just the date portion like this:


Code:


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

HTH

Jerry


----------

