Rename file with current datetime appended to filename [Solved/Closed]

Betty - Sep 24, 2008 at 01:15 PM - Latest reply:  Sarath
- Mar 26, 2013 at 03:28 AM
Hello,

I have the following bat file:
FOR %%V IN (%1) DO FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO ECHO Rename "%%V" %%L%%J%%K_%%M%%N%%~xV

This will rename the files to the datetime stamp. However I want to append the datetime to the filename.
Example:
filename: betty.rpt rename to betty0924081530.rpt

Can you help me with the correct syntax?

Thanks, Betty
See more 

26 replies

Best answer
55
Thank you
Set TDate=%date:~10,4%%date:~4,2%%date:~7,2%
FOR %%V IN (%1) DO Rename %%V %%V%TDate%

Thank you, QSquared 55

Something to say? Add comment

CCM has helped 1667 users this month

23
Thank you
ren fud fud_%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%_%time:~6,5%.jpg

This will rename fud to fud_04-27-2010_2125_09.67.jpg
thanks.
Thanks for your sharing. it's really help me a lot.
Cheers
Thanks from me too.
muchas gracias !
thaks nd it works in batch format also... it helps me a lot :) :)
16
Thank you
I have been using biterscripting a lot to do stuff like this from command mode.

Some sample code:

# Get the time stamp.
var str timestamp
set $timestamp=gettime()

# Get the file extension - after the last dot (assuming file name is in $file)
var str extension
stex "[^.^l"$ $file >$extn # ^.^ means find a dot, l means the last instance, [ means strip
# off everything beginning with the last dot.

# Add timestamp to the file name
set $file = $file + $timestamp

# Add back the extension
set $file = $file + $extension

# $timestamp has the format yyyymmddhhmmss . You can format it further. For example, if you just want the
# date (and no time), use the following
chex "8[" $timestamp > null # 8 means 8th character, [ means extract everything after the 8th char,
# >null means don't show the extracted content on screen.

etc.

Sen Hu
8
Thank you
note above answer by sunwukong won't work if date or time has leading zero / blank space. This modification will work

ren fud "fud_%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,5%.jpg"
thanks sunwukong! your answer really help me
This piece of code is quite elegant sunwukong - very useful to me in a tight spot
mine says cannot find the file path specified...
The modified version doesn't work either with leading blank spaces.
is work thx
6
Thank you
Hi betty
i have a bat that can help u try this
set mm= %Date:~3,2%
set dd = %Date:~0,2%
set yy = %Date:~8,2%
pkzip -a AL%mm%dd%yy% *.doc

this zips all files in a folder and names it AL101008
Is it possible to do a reverse?

For example

1) Rename 11-10-08-File1.TXT to File1.TXT
2) Rename 11-10-08-File2.TXT to File2.TXT
to do the reverse assign the file to a variable.
set fileName=11-10-08-File1.TXT
set newName=%fileName:~9% (begin at position nine to end of string)

RESULT: %newName% = File1.TXT

NOTE: when assigning a variable the is no space before and after the equals sign (e.g. set x=1)
QSquared (Ben Personick) - Jul 9, 2009 at 05:32 PM
4
Thank you
Sorry first time I didn't fix the File extention, here I split everythgin out into simple steps, obviously you can combine several of these into one if you like. I hope you like it =) -Ben




:: Set the date to the format you desire once and store it as a variable
Set TDate=%date:~10,4%%date:~4,2%%date:~7,2%

::Start a Loop which goes to a subroutine through a 'call' function (This allow you to assign variables..
FOR %%V IN (%1) DO Call :DoLoop


::Start your complex processing inside this lable so that you can do as many lines as you line inside your loop
:DoLoop

:: Set Single-sided variable to double-sided variable
set OFullFileName=%%V

::Double-sided variable manipulation to get the original file's extention and name separated
Set OFileExt=%FileName:~-4%
Set OFileName=%OFullFileName:~0,-4%

::Build the completed new file Name from the variables already assigned
Set NFullFileName=%OFileName%%TDate%%OFileExt%

::Run your rename command
Rename %OFullFileName% %NFullFileName%

::Exit the sub routine, allowing you to continue your loop.
Goto :EOF
3
Thank you
This Might help you

SET date="%date:~6,6%%date:~3,2%%date:~0,2%"
SET time="%time:~0,2%%time:~3,2%%time:~6,2%"

ren D:\test.txt %date%%time%.txt
1
Thank you
Hi Betty,
You only need to replace the IN section ("%%~tV") with ("%date%%time%")
Found it does not resolve the %%~tV propperly

PS: If you add - and . to your delims it will work regardless the regional settings.

Nice day
1
Thank you
I have a list of files in my directory

"01 a B-c d.txt"
"02 a B-c d.txt"
"03 a B-c d.txt"

I want to rename them as:

"01 c d-a B.txt"
"02 c d-a B.txt"
"03 c d-a B.txt"

where c,d,a & B are strings.

How can it be done using DOS batch files? I am using Windows XP
As your

1. File extension will be Text (.txt) use this in the IN ("*.txt") section.

2. Delimiters will be dot(.), dash(-) and a space ( ) use these in the delims field.

3. This will split the file name into 6 pieces (variables) starting with %%J (0#)

4. The rest will be %%K (a), %%L (B), %%M (c), %%N (d) and %%o (txt).

5. Your required result swap a and c, it also swap B and d

6. Modify Betty's original one liner a bit to use these:

FOR %%V IN ("*.txt") DO FOR /F "tokens=1-9 delims=.- " %%J IN ("%%V") DO Rename "%%V" "%%J %%M %%N-%%K %%L.%%O"

PS: Running this in a command window require replacing all the batch file compliant %% with single % right through ;-)
Thanks a lot LAS. I appreciate the prompt and quick reply!
I have Windows XP, so I have to replace %% in % when I execute it in Dos Shell?
Correct.

Open a dos shell and change to the folder where your files reside and enter:

FOR %V IN ("*.txt") DO FOR /F "tokens=1-9 delims=.- " %J IN ("%V") DO Rename "%V" "%J %M %N-%K %L.%O"
Good Script
Its very usefull to me