Renaming a file

G

Guest

Hello.
I'm trying to create a batch file that renames a certain zip file. The new
name should include the system date.

How can i this?

Thanks

Luis
 
T

Torgeir Bakken \(MVP\)

Luis said:
Hello.
I'm trying to create a batch file that renames a certain zip file.
The new name should include the system date.

How can i this?
Hi,

--------------------8<----------------------
@echo off

setlocal
echo D = Now : WScript.Echo Year(D) ^& _ >%tmp%\today.vbs
echo Right(100+Month(D),2) ^& Right(100+Day(D),2) >>%tmp%\today.vbs

for /f "tokens=1" %%a in (
'cscript.exe //Nologo %tmp%\today.vbs') do set today=%%a

del %tmp%\today.vbs
echo Todays date: %today%
echo New file file: %today%.zip

pause
endlocal
--------------------8<----------------------


You can do it with "pure" batch file as well, using e.g the batch date
time functions from Ritchie Lawrence batch library available at
http://www.commandline.co.uk/lib
 
G

Guest

Hi.
Thanks for your quick answer. I'm a beginner in batch file programmer so i
have a question.

If the file i want to rename is in directory, for example, C:\TempDir\ and
the file is called Myfilename.zip, where do i define this in the code you
gave me?
 
T

Torgeir Bakken \(MVP\)

Luis said:
Hi.
Thanks for your quick answer. I'm a beginner in batch file programmer so i
have a question.

If the file i want to rename is in directory, for example, C:\TempDir\ and
the file is called Myfilename.zip, where do i define this in the code you
gave me?

Hi,

This will rename C:\TempDir\Myfilename.zip to
C:\TempDir\20050908.zip (if run today):


--------------------8<----------------------
@echo off

setlocal
echo D = Now : WScript.Echo Year(D) ^& _ >%tmp%\today.vbs
echo Right(100+Month(D),2) ^& Right(100+Day(D),2) >>%tmp%\today.vbs

for /f "tokens=1" %%a in (
'cscript.exe //Nologo %tmp%\today.vbs') do set today=%%a

del %tmp%\today.vbs
ren C:\TempDir\Myfilename.zip %today%.zip
endlocal

--------------------8<----------------------
 
B

billious

Luis said:
Hello.
I'm trying to create a batch file that renames a certain zip file. The new
name should include the system date.

How can i this?

Thanks

Luis

ren c:\tempdir\myfilename.zip %date:~8,2%%date:~3,2%%date:~0,2%.zip

would rename c:\tempdir\myfilename.zip to c:\tempdir\050908.zip today (Sept.
08, 2005) PROVIDED your date format is dd/mm/yyyy.

There are methods to suit any date format you may be using. If you

ECHO %date%

at the prompt, this will show the date format you have selected. It may
contain dayname as well - and month,daynumber and year in a variety of
formats. If you post the format YOU have selected, the formula can be
customised.

The method is simple: %date:~m,n% is the n characters of the string,
starting at character m, where the FIRST character of the string is m=0
(C-style)

So, if your date format is

Thu 09-08-2005

and you want

20050908

then you use
%date:~10,4%%date:~4,2%%date:~7,2%

If you want to change the DIRECTORY (folder) that contains the file, then
you need to use the MOVE command, not REN.

move c:\tempdir\myfilename.zip
c:\where\I\want\it\to\be\%date:~10,4%%date:~4,2%%date:~7,2%.zip

(all on one line) will change the name AND move it to a different folder.

These commands could be either typed from the prompt or as a line in a batch
file.

Possible better newsgroups for batch methods: alt.msdos.batch.nt and
microsoft.public.win2000.cmdprompt.admin

HTH

....Bill
 
G

Guest

Perfect!

Thank you all for your help!

Luis

billious said:
ren c:\tempdir\myfilename.zip %date:~8,2%%date:~3,2%%date:~0,2%.zip

would rename c:\tempdir\myfilename.zip to c:\tempdir\050908.zip today (Sept.
08, 2005) PROVIDED your date format is dd/mm/yyyy.

There are methods to suit any date format you may be using. If you

ECHO %date%

at the prompt, this will show the date format you have selected. It may
contain dayname as well - and month,daynumber and year in a variety of
formats. If you post the format YOU have selected, the formula can be
customised.

The method is simple: %date:~m,n% is the n characters of the string,
starting at character m, where the FIRST character of the string is m=0
(C-style)

So, if your date format is

Thu 09-08-2005

and you want

20050908

then you use
%date:~10,4%%date:~4,2%%date:~7,2%

If you want to change the DIRECTORY (folder) that contains the file, then
you need to use the MOVE command, not REN.

move c:\tempdir\myfilename.zip
c:\where\I\want\it\to\be\%date:~10,4%%date:~4,2%%date:~7,2%.zip

(all on one line) will change the name AND move it to a different folder.

These commands could be either typed from the prompt or as a line in a batch
file.

Possible better newsgroups for batch methods: alt.msdos.batch.nt and
microsoft.public.win2000.cmdprompt.admin

HTH

....Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top