rename old files by adding data&time variable

September 6, 2011 at 06:43:36
Specs: Linux i686
To rename lots of old files by adding their data & time variables into the file name. Eg

Oldfilename.mp3 created 02 Aug 2009 7 19pm >>>> Oldfilename 02082009 1917.mp3

I.e Not insert the current date of operation but rather the date the file was created, using Linux or XP.
At the moment I do not know much code except to run ls in linux, but learning.
[Separate issue, some files that need renaming have acquired new date time stamps when I copied them from disc to disc, thus uncertain if i can access the original "date created" variable?]
thank you in advance


See More: rename old files by adding data&time variable

Report •


#1
September 13, 2011 at 11:21:59
This is do-able, but I don't have time right now to actually write it out in proper syntax. Here is the sudo version until then.

You will need to use dir to list the names of the files into a txt file like this:

dir /t:c /b > files.txt

That command lists the files sorted by creation date like you wanted. Now you want to use dir to list those creation dates in the same order in another file like this:

dir /t:c > dates.txt

dates.txt will have to be modified to remove everything but the dates. This can be done following the methods listed here:

http://www.dostips.com/DtTipsString...

Something like this should pull out the dates only:

for /F "tokens=*" %%A in (c:\dates.txt) do set str=%str:~0,11% > datesmod.txt

Here you will need to figure out a loop that will join all the lines together, after that you can loop through the result of that and rename your files.


Report •

#2
September 13, 2011 at 16:10:49
set workdir = c:\
for /f "tokens=*" %%a in ('dir %workdir% /b') do (
set T=%%~Ta
set Month1=!T:~0,2!
set Day=!T:~3,2!
set Year=!T:~6,4!
set Hour=!T:~11,2!
set Minute=!T:~14,2!
ren %%a %%a%Month%%Day%%year% %hour%%Minute%.*
)

Off the top of my head not tested. You might want to replace ren with copy until you test it Also, do not forget to set your workingdir because you probably do not want to rename the files in your root.


Report •

#3
September 13, 2011 at 18:00:14
Tested
@echo off
setLocal EnableDELAYedExpansion
for /f %%a in ('dir/b/a-d') do (
  set T=%%~Ta
  set FileDay=!T:~3,2!
  set FileMonth=!T:~0,2!
  set FileYear=!T:~6,4!
  set Hour=!T:~11,2!
  set Minute=!T:~14,2!  
ren "%%a" "!FileMonth!!FileDay!!FileYear! !Hour!!Minute!%%a"
)

Run it in the directory you want. If you run it from windows copy the file to you directory before you run it.


Report •

Related Solutions


Ask Question