Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename CMD

Status
Not open for further replies.

rstum2005

Programmer
Jun 9, 2005
117
US
All I would like to do is , in the cmd prompt window "Rename" a file and place todays date in the file name.

thanks.
 
Rename Test.txt "Test 12-2-05.txt"

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Sorry,

Id like to use something like a getdate() or Date function to do this.
 
Aah...Can't be done as a single line at the command prompt afaik. You could do it in a batch file or as a VBScript however.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
that would be fine any clue of how to do such a thing?

the file name will always be the same name at first and id like to change the name to the same name + date.

for example.

Report.xls

Report 12-02-2005.xls

or even
Report12022005

I just dont want my process to over write the file thats why i need to incorporate somthing to change the name to a distinct name.

Thanks
 
This will work with some caveats:
1) There can be no periods in the path or filname except for the one seperating the file name from the file extension.
2) There cannot be a file named date.txt in the same directory as the batch file.
3) There cannot be a file named name.txt in the same directory as the batch file.
4) You cannot have an environment variable named TEMPFILENAME in your environment.

Code:
@Echo off
date /t > date.txt
echo %1% > name.txt
for /F "tokens=1 delims=." %%i in (name.txt) do Set TEMPFILENAME=%%i
for /F "eol=; tokens=2,3,4 delims=/ " %%i in (date.txt) do Set TEMPFILENAME=%TEMPFILENAME%%%i-%%j-%%k
for /F "tokens=2 delims=." %%i in (name.txt) do Set TEMPFILENAME=%TEMPFILENAME%.%%i
rename %1 %TEMPFILENAME%
Del date.txt
Del name.txt
Set TEMPFILENAME=

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 

ok so i changed name.txt to my filename? its not working for me.
 
No you call the bat passing it the name of the file you want to rename. So for instance if you saved that code in a file named DateName.bat and you wanted to rename a file named foo.txt, then you would use this command line:

DateName.bat Foo.txt

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hey thanks a bunch man. It does work great but ive got still another question. Im running this file accross the network on a server and the file I want to rename is on my local. I tried typing cd \\\\mycomputer and of coarse that did not work. any clues there?
 
The first thing that I would sugest is to just run the bat locally. Its not like it takes up a lot of room. Next I would suggest using \\mycomputer instead of \\\\mycomputer.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top