Batch Ren Files Of *.dem to Each File's Date

March 5, 2011 at 06:13:09
Specs: Windows XP
I've been trying to build a batch file to perform the following steps:

1. Take all files of a specific type (*.dem) in the same dir as the .bat file.
2. Get each individual file's creation time and date. (It has to be it's creation date)
3. Rename that file to it's creation date in the format "YYYY.MM.DD_HHMM.dem"
4. Move on to the next file.

I've spent the week trying to learn how this could be done from examples to already posted questions, and to be honest, it's time to ask for help.

It's clear to me that the FOR command is used here, with some delimiters, but all my attempts have failed. I know I'm close, I'm just getting the syntax orders wrong, or they're in the wrong place in each batch file I've tried. Or maybe I'm way off.

Currently, I can do it with each file with this:

dir demo.dem | find /i "demo.dem" > info.dat
for /f "tokens=1-10 delims=/:, " %%a in (info.dat) do set XDate=%%c.%%b.%%a_%%d%%e
rename demo.dem %XDate%.dem
del info.dat

Which works fine but I have to first rename the file in particular to demo.dem. I'm ok with this when it comes to only a few files as I can rename, paste, run batch, repeat manually as needed and it only takes a few seconds and achieves the desired result with the info I've got now.

I've got hundreds and hundreds of *.dem files that need renaming in the format YYYY.MM.DD_HHMM.dem (because Windows/Linux/OS X will then arrange them by their creation dates so I can quickly find the *.dem files I need).

I've also tried:

for %%Q in (*.dem) do ren %%~nQ.dem %%~tQ.dem

but no matter where I look I can't find how to remove the / or : from the time or even replace them with another kind of character. I've tried:

for "delims=/:" %%Q in (*.dem) do ren %%~nQ.dem %%~tQ.dem

and

for "tokens=1-9 delims=/:" %%Q in (*.dem) do ren %%~nQ.dem %%~tQ.dem

but these failed miserably. I'm soooooo close but I just can't get it right.

Can someone help? I don't need someone to break it down for me unless they really want to take the time to do so (that would be immensely appreciated!), but the technical documents I've read have only confused me so much further with their inspecific examples.

While I've so far been able to teach myself everything I know about scripting, I think at this stage it's time to admit to myself that I'm just not going to teach myself to this depth of scripting without some help ^^


See More: Batch Ren Files Of *.dem to Each Files Date

Report •


#1
March 6, 2011 at 08:29:09
Ninjaholic:

dtech10's batch file on this site (www.computing.net/answers/programming/use-creation-date-in-renaming-file/15094.html) was really close, but here's what you're looking for:

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=1-4,*" %%a in ('dir *.dem /tc /a-d ^| find /v "(s)"') do (
set CDate=%%a
set CTime=%%b
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren %%e !yr!.!mo!.!da!_!hh!!mi!%%~xe
)

Enjoy!


Report •

#2
March 6, 2011 at 09:52:56
The breakdown:

In the "for" line:

"tokens=1-4,*" %%a causes creation of 5 local variables a thru e, containing the output of the dir command.

its output is the creation date and time (/tc), filesize and name, without directories (/a-d), piped through find to output only those lines that do not have (s) in them (/V "(s)"), which are the last two lines from DIR that list the File(s) and Dir(s) count .

%a has the creation date, %b the creation time, %c the AM/PM flag, %d the file size, and %d the original filename

next, copy the first two variables into the CDate and CTime variables, to be able to use the delayed expansion features to extract the first 2 characters as the day, the next two after skipping the first three as the month, then the four characters after skipping 6 as the year (da/mo/year). Do the same for the hour (first two characters) and minute (skip 3, grab 2)

If your regional settings change the date and time layout, edit accordingly.

Finally, let's concatenate the year.month.day_hourminute, stamp the original extension on the end (%%~xe, the extension from the %e variable, which is the original file name and use the result to rename the file.

I just realized I neglected one detail... the timestamp is given as AM/PM, and the time extraction doesn't account for the added 12 hours for PM... and for the life of me I'm having trouble fixing it...

anyone have any suggestions how to add 12 to the hh variable if %c (the AM/PM part of the DIR command output) = PM? The DOS SET operator errors when given a mathematical expression with a variable that contains a leading zero, the assumption is that leading zero denotes an octal number, leading 0x denotes hexadecimal, and anything else decimal. Of course, every time from 1 (01:00) to 9:59 (09:59), both AM and PM, have leading zeros...


Report •

#3
March 6, 2011 at 17:02:08
Thanks so much for taking the time to have a look at this problem for me! I appreciate it!

However, I'm afraid that didn't work :(

With echo on I get this in the cmd window.

C:\test>setlocal EnableDelayedExpansion

C:\test>for /F "tokens=1-4,* skip=4" %a in ('dir *.dem /tc /a-d | find /v "(s)"'
) do (
set CDate=%a
set CTime=%b
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren %e !yr!.!mo!.!da!_!hh!!mi!%~xe
)

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren (10).dem !yr!.!mo!.!da!_!hh!!mi!.dem
)
The system cannot find the file specified.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren (11).dem !yr!.!mo!.!da!_!hh!!mi!.dem
)
The system cannot find the file specified.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren (12).dem !yr!.!mo!.!da!_!hh!!mi!.dem
)
The system cannot find the file specified.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren (13).dem !yr!.!mo!.!da!_!hh!!mi!.dem
)
The system cannot find the file specified.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren (14).dem !yr!.!mo!.!da!_!hh!!mi!.dem
)
The system cannot find the file specified.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren (15).dem !yr!.!mo!.!da!_!hh!!mi!.dem
)
The system cannot find the file specified.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren (16).dem !yr!.!mo!.!da!_!hh!!mi!.dem
)
The system cannot find the file specified.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren (17).dem !yr!.!mo!.!da!_!hh!!mi!.dem
)
The system cannot find the file specified.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren (9).dem !yr!.!mo!.!da!_!hh!!mi!.dem
)
The system cannot find the file specified.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren !yr!.!mo!.!da!_!hh!!mi!
)
The syntax of the command is incorrect.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren !yr!.!mo!.!da!_!hh!!mi!
)
The syntax of the command is incorrect.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren !yr!.!mo!.!da!_!hh!!mi!
)
The syntax of the command is incorrect.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren !yr!.!mo!.!da!_!hh!!mi!
)
The syntax of the command is incorrect.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren !yr!.!mo!.!da!_!hh!!mi!
)
The syntax of the command is incorrect.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren !yr!.!mo!.!da!_!hh!!mi!
)
The syntax of the command is incorrect.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren !yr!.!mo!.!da!_!hh!!mi!
)
The syntax of the command is incorrect.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren !yr!.!mo!.!da!_!hh!!mi!
)
The syntax of the command is incorrect.

C:\test>(
set CDate=05/03/2011
set CTime=13:02
set da=!CDate:~0,2!
set mo=!CDate:~3,2!
set yr=!CDate:~6,4!
set hh=!CTime:~0,2!
set mi=!CTime:~3,2!
ren !yr!.!mo!.!da!_!hh!!mi!
)
The syntax of the command is incorrect.

Also, the time is showing correctly, so no need to worry about it :)

Any ideas where it's going wrong?


Report •

Related Solutions

#4
March 6, 2011 at 17:06:32
I copied some test files and named them "test (#).dem", so I expect same creation dates.

I half fixed some of it inserting skip=4 into the for loop:

for /f "tokens=1-4,* skip=4" %%a in ('dir ...

Now it skips the volume and directory so seems to only be focusing on the files.

I think it's working up to the point where it's trying to rename the .dem file at the very end.

ren %%e !yr!.!mo!.!da!_!hh!!mi!%%~xe

saying it can't find the file specified.

Oddly %%e seems to be skipping the first part of the filename called "test (#).dem" even when I've named them test1.dem test2.dem the rename command shows double-spacing instead of any characters, and still omits the first part of the name. :(


Report •


Ask Question