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

Ask a question Betty - Last answered on Mar 26, 2013 at 03:28 AM by Sarath

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.
filename: betty.rpt rename to betty0924081530.rpt

Can you help me with the correct syntax?

Thanks, Betty
See more 
plus moins
Set TDate=%date:~10,4%%date:~4,2%%date:~7,2%
FOR %%V IN (%1) DO Rename %%V %%V%TDate%
Was this answer helpful?  
plus moins
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
tinkiwinki- Jul 8, 2010 at 07:47 AM
Teng- Nov 7, 2010 at 09:30 PM
Thanks for your sharing. it's really help me a lot.
fubared- Oct 30, 2012 at 10:54 AM
Thanks from me too.
jose- Feb 7, 2013 at 05:18 PM
muchas gracias !
Sarath- Mar 26, 2013 at 03:28 AM
thaks nd it works in batch format also... it helps me a lot :) :)
plus moins
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.


Sen Hu
plus moins
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"
seri175- Jun 25, 2010 at 03:17 AM
thanks sunwukong! your answer really help me
Rusticles- Jun 30, 2010 at 01:58 PM
This piece of code is quite elegant sunwukong - very useful to me in a tight spot
name- Jul 6, 2010 at 11:09 AM
mine says cannot find the file path specified...
skaszyk- Jul 23, 2010 at 08:25 AM
The modified version doesn't work either with leading blank spaces.
mbhoo- Aug 3, 2010 at 09:24 PM
is work thx
plus moins
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
script-help- Nov 18, 2008 at 05:49 PM
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
ecciethetechie- Apr 22, 2010 at 08:11 AM
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)
plus moins
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

:: 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
plus moins
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
plus moins
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
plus moins
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
LAS- Sep 2, 2010 at 08:16 AM
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 ;-)
Harish- Sep 2, 2010 at 10:00 AM
Thanks a lot LAS. I appreciate the prompt and quick reply!
Harish- Sep 2, 2010 at 10:12 AM
I have Windows XP, so I have to replace %% in % when I execute it in Dos Shell?
LAS- Sep 2, 2010 at 11:31 AM

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"
balaji- Oct 18, 2012 at 04:56 AM
Good Script
Its very usefull to me

Member requests are more likely to be responded to.

Members can monitor the statuses of their requests from their account pages.

A CCM membership gives you access to additional options.

Not a member yet?

Sign up now. It takes less than a minute and is completely free!