How to rename a file with date/time?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dominion
Posts: 6
Joined: 12 Feb 2011 12:35

How to rename a file with date/time?

#1 Post by dominion » 14 Feb 2011 08:26

Hi,

how is it possible to rename a file with current date and time.
i'm trying this but it's not working.

please help:

Code: Select all

@echo off
color 4
set N=
(for /f "tokens=1-5 delims=:" %%d in ("%time%") do ceho rename
file.txt file%%d-%%e-%%f.txt)


i'm using xp and Win7
pause

dominion
Posts: 6
Joined: 12 Feb 2011 12:35

Re: How to rename a file with date/time?

#2 Post by dominion » 14 Feb 2011 08:39

I found it but:

ren file.txt file%date:~0,2%%date:~3,2%%time:~0,2%%time:~3,2%.txt

i'm looking how to add day and second
thanks

dominion
Posts: 6
Joined: 12 Feb 2011 12:35

Re: How to rename a file with date/time?

#3 Post by dominion » 14 Feb 2011 08:52

I modify the previous code to:

r

Code: Select all

en file.txt file-%date:~0,2%-%date:~3,2%-%date:~6,4%-%time:~0,2%-%time:~6,2%-%time:~0,2%.txt

but when current is : 15:50:00
the file name is: file-14-02-2011-15-58-00 ?? why?

the time is incorect why?

thank you

dominion
Posts: 6
Joined: 12 Feb 2011 12:35

Re: How to rename a file with date/time?

#4 Post by dominion » 14 Feb 2011 08:55

Sorry for disturbance i made mistake.
now it's working well

Code: Select all

ren file.txt file-%date:~0,2%-%date:~3,2%-%date:~6,4%-%time:~0,2%-%time:~3,2%-%time:~6,2%.txt


Thank Dominion and have a nice week.

Post Reply