Ask a question Report

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

Betty - Latest answer on Mar 26, 2013 03:28AM
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
Read more 
Answer
+51
moins plus
Set TDate=%date:~10,4%%date:~4,2%%date:~7,2%
FOR %%V IN (%1) DO Rename %%V %%V%TDate%

Answer
+23
moins plus
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 07:47AM
thanks.
Teng- Nov 7, 2010 09:30PM
Thanks for your sharing. it's really help me a lot.
Cheers
fubared- Oct 30, 2012 10:54AM
Thanks from me too.
jose- Feb 7, 2013 05:18PM
muchas gracias !
Sarath- Mar 26, 2013 03:28AM
thaks nd it works in batch format also... it helps me a lot :) :)
Answer
+16
moins plus
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

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

Answer
+2
moins plus
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

Answer
+1
moins plus
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

Answer
+1
moins plus
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 08:16AM
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 10:00AM
Thanks a lot LAS. I appreciate the prompt and quick reply!
Harish- Sep 2, 2010 10:12AM
I have Windows XP, so I have to replace %% in % when I execute it in Dos Shell?
LAS- Sep 2, 2010 11:31AM
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"
balaji- Oct 18, 2012 04:56AM
Good Script
Its very usefull to me
This document entitled « Rename file with current datetime appended to filename » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.

Not a member yet?

sign-up, it takes less than a minute and it's free!

Members get more answers than anonymous users.

Being a member gives you detailed monitoring of your requests.

Being a member gives you additional options.