Using batch to insert (file-creation) date-time stamp in filename

2746 views
Skip to first unread message

mmm

unread,
Jul 13, 2008, 1:30:26 PM7/13/08
to
My Canon camcorder generates files named 000001.MTS, 000002.MTS, and
so on.
This isn't useful. I've been looking for a method to flawlessly
rename folders full of those files with their date and time
inserted into the filename, so I can sort them accordingly etc.

For example, I'd like to have

000001.MTS
000002.MTS

renamed to

20080730_23h59m56s_000001.MTS
20080731_00h02m33s_000002.MTS

where the dates are pulled from their date-time stamp.
(so the desired format is:
yearmonthday_hourhminutemsecondss_orgfilename.MTS

To achieve this, I got pretty far creating a batch-file using the FOR
command
for Windows 32bit, and when I run the batch-file inside a folder it
renames the files in that folder. The batch file is currently:

@echo off
FOR %%q IN (*.MTS) DO FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tq")
DO Rename "%%q" %%J_%%K%%M%%N%%L_%%~nxq

but I can't seem to get the date-time notation how I want to have it.
The results look like this:

2008-07-30_2359_000001.MTS

because %~t pulls the date from the short date notation
as set in Windows. I want to get rid of the dashes, and I have
no idea how to obtain the seconds from the file creation time.

Anyone able to help me out here?


Thanks in advance. I'm creating a weblog entry about this too, so more
people can use it when it's finished.


Julius

001.d...@gmail.com

unread,
Jul 13, 2008, 1:46:55 PM7/13/08
to

Relative to native date-time format:
=========================
@echo off

for %%i in (*.mts) do call:ren "%%~ti" "%%i" %%~nxi
goto:eof

:ren
set datetime=%~1
set datetime=%datetime: =_%
set datetime=%datetime::=%
set datetime=%datetime:.=-%
ren %2 %datetime%%3

mmm

unread,
Jul 13, 2008, 2:19:19 PM7/13/08
to
On Jul 13, 7:46 pm, 001.dmi...@gmail.com wrote:

Using this,

000001.MTS
results in:
2008-06-08_17042008-06-08_1704000001.MTS

and
000002.MTS
results in
2008-06-08_1702000002.MTS

Same thing as before. What I need is a timedate notation without
dashes, with seconds as well. See my first post.

Thanks for the attempt anyway..

mmm

unread,
Jul 13, 2008, 2:45:16 PM7/13/08
to
On Jul 13, 7:30 pm, mmm <jul...@gmail.com> wrote:

> For example, I'd like to have
>
> 000001.MTS
> 000002.MTS
>
> renamed to
>
> 20080730_23h59m56s_000001.MTS
> 20080731_00h02m33s_000002.MTS
>
> where the dates are pulled from their date-time stamp.
> (so the desired format is:
> yearmonthday_hourhminutemsecondss_orgfilename.MTS

I now have

@echo off
FOR %%q IN (*.MTS) DO FOR /F "tokens=1-5 delims=-: " %%J IN ("%%~tq")
DO Rename "%%q" %%J%%K%%L_%%M%%N%%~nxq

that got the dash out, but I still don't have the "seconds" in there.

So for example
000002.MTS
results in
20080731_0002_000002.MTS

when I would prefer:

20080731_00h02m33s_000002.MTS


Julius

foxidrive

unread,
Jul 13, 2008, 4:05:59 PM7/13/08
to
On Sun, 13 Jul 2008 10:30:26 -0700 (PDT), mmm <jul...@gmail.com> wrote:

>My Canon camcorder generates files named 000001.MTS, 000002.MTS, and
>so on.
>This isn't useful. I've been looking for a method to flawlessly
>rename folders full of those files with their date and time
>inserted into the filename, so I can sort them accordingly etc.
>
>For example, I'd like to have
>
>000001.MTS
>000002.MTS
>
>renamed to
>
>20080730_23h59m56s_000001.MTS
>20080731_00h02m33s_000002.MTS

This uses WSH to create a batch file for the renaming
so you can view the changes before committing them.

It will write a rename command for all files in the
current folder, and at the moment is using the
date_created timestamp for the date/time.

This takes code from different places - I just assembled it. :)

Any lines starting in column 1 have wrapped.


@echo off
del %temp%\file.vbs 2>nul
>>%temp%\file.vbs echo. Set fso = CreateObject("Scripting.FileSystemObject")
>>%temp%\file.vbs echo. Set f = fso.GetFolder(".")
>>%temp%\file.vbs echo. WScript.Echo "@echo off"
>>%temp%\file.vbs echo. For Each f1 in f.Files
>>%temp%\file.vbs echo. c = f1.DateCreated
>>%temp%\file.vbs echo. a = f1.DateLastAccessed
>>%temp%\file.vbs echo. m = f1.DateLastModified
>>%temp%\file.vbs echo. n = c
>>%temp%\file.vbs echo. WScript.Echo "ren "+String(1,34)+f1.name+String(1,34)+" "+String(1,34)+CStr(Year(n))+Right(100+Month(n),2)+Right(100+Day(n),2)+"_"+Right(100+Hour(n),2)+"h"+Right(100+Minute(n),2)+"m"+Right(100+Second(n),2)+"s_"+f1.name+String(1,34)
>>%temp%\file.vbs echo. Next
cscript //nologo %temp%\file.vbs>%temp%\file.bat
del %temp%\file.vbs
copy /y %temp%\file.bat . >nul
rem call %temp%\file.bat

** Posted from http://www.teranews.com **

Todd Vargo

unread,
Jul 13, 2008, 7:43:21 PM7/13/08
to
mmm wrote:
> On Jul 13, 7:30 pm, mmm <jul...@gmail.com> wrote:
>
>> For example, I'd like to have
>>
>> 000001.MTS
>> 000002.MTS
>>
>> renamed to
>>
>> 20080730_23h59m56s_000001.MTS
>> 20080731_00h02m33s_000002.MTS
>>
>> where the dates are pulled from their date-time stamp.
>> (so the desired format is:
>> yearmonthday_hourhminutemsecondss_orgfilename.MTS
>
> I now have
>
> @echo off
> FOR %%q IN (*.MTS) DO FOR /F "tokens=1-5 delims=-: " %%J IN ("%%~tq")
> DO Rename "%%q" %%J%%K%%L_%%M%%N%%~nxq
>
> that got the dash out, but I still don't have the "seconds" in there.

That is because the FOR command's %%~tq returns output similar to the DIR
command which does not include seconds. foxidrive's post provides a
brilliant example of how to capture seconds using WSH/VBScript.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Message has been deleted
Message has been deleted

mmm

unread,
Jul 14, 2008, 8:09:35 AM7/14/08
to
On Jul 13, 10:05 pm, foxidrive <got...@woohoo.invalid> wrote:

> current folder, and at the moment is using the
> date_created timestamp for the date/time.
>
> This takes code from different places - I just assembled it. :)
>
> Any lines starting in column 1 have wrapped.
>
>    @echo off
>    del %temp%\file.vbs 2>nul
>    >>%temp%\file.vbs echo.     Set fso = CreateObject("Scripting.FileSystemObject")
>    >>%temp%\file.vbs echo.     Set f = fso.GetFolder(".")
>    >>%temp%\file.vbs echo.     WScript.Echo "@echo off"
>    >>%temp%\file.vbs echo.     For Each f1 in f.Files
>    >>%temp%\file.vbs echo.     c = f1.DateCreated
>    >>%temp%\file.vbs echo.     a = f1.DateLastAccessed
>    >>%temp%\file.vbs echo.     m = f1.DateLastModified
>    >>%temp%\file.vbs echo.     n = c

I found out I had to use n = m for the way Windows and Canon work with
files.

But thanks a million. Works like a charm! Such a time-saver.

Makes you wonder why they don't save files this way by default.
I mean, it solves the problem of not having unique filenames
when bateries die with sequential file-numbering etc.

Anyway, I'll credit you in my www message about this.
If you want your name to be something other than "foxidrive"
in that text, let me know by email ;)

Cheers, and thanks for the quick and useful help!


Julius

Tom Lavedas

unread,
Jul 14, 2008, 1:14:08 PM7/14/08
to

I couldn't resist tinkering ...

@goto :SkipVBS@
set oFldr=CreateObject("Scripting.FileSystemObject").GetFolder(".")
For Each f in oFldr.Files
c=f.DateCreated : a=f.DateLastAccessed : m=f.DateLastModified
n = m
WSH.Echo "ECHO ren """ & f.name & """ """ & Year(n) _
& P0(Month(n),"") & P0(Day(n),"_") & P0(Hour(n),"h") _
& P0(Minute(n),"m") & P0(Second(n),"s_") & f.name & """"
Next
function P0(DT,s) : P0=Right("0" & DT,2) & s : end function
:SkipVBS@
@find /v "@" < "%~f0" >"%temp%\file.vbs"
@for /f "delims=" %%a in (
'cscript //nologo "%temp%\file.vbs"^|find /v /i "%~nx0"') do @%%a
@del "%temp%\file.vbs"

I prefer this approach to a hybrid WSH/Batch because the WSH part can
be debugged and then just used as is (Placed between the GOTO and
Label). The only restriction is that the VBS part cannot contain the
@ character. If it is needed as a literal, it can be replaced with a
"firstpart" & CHR(64) & "lastpart" construct, instead. I can't think
of any other reason for using it, but that doesn't mean it isn't
needed.

BTW, I posted the test version - the ECHO needs to be removed from the
WSH.Echo line to make it functional.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

Esra Sdrawkcab

unread,
Jul 14, 2008, 3:03:35 PM7/14/08
to
Tom Lavedas wrote:

> I couldn't resist tinkering ...
>
> @goto :SkipVBS@
> set oFldr=CreateObject("Scripting.FileSystemObject").GetFolder(".")
> For Each f in oFldr.Files
> c=f.DateCreated : a=f.DateLastAccessed : m=f.DateLastModified
> n = m
> WSH.Echo "ECHO ren """ & f.name & """ """ & Year(n) _
> & P0(Month(n),"") & P0(Day(n),"_") & P0(Hour(n),"h") _
> & P0(Minute(n),"m") & P0(Second(n),"s_") & f.name & """"
> Next
> function P0(DT,s) : P0=Right("0" & DT,2) & s : end function
> :SkipVBS@
> @find /v "@" < "%~f0" >"%temp%\file.vbs"
> @for /f "delims=" %%a in (
> 'cscript //nologo "%temp%\file.vbs"^|find /v /i "%~nx0"') do @%%a
> @del "%temp%\file.vbs"
>
> I prefer this approach to a hybrid WSH/Batch because the WSH part can
> be debugged and then just used as is (Placed between the GOTO and
> Label). The only restriction is that the VBS part cannot contain the
> @ character. If it is needed as a literal, it can be replaced with a
> "firstpart" & CHR(64) & "lastpart" construct, instead. I can't think
> of any other reason for using it, but that doesn't mean it isn't
> needed.
>

For a trivial amount of extra tinkering (only with the batch part)


@find /v "@" < "%~f0" >"%temp%\file.vbs"
@for /f "delims=" %%a in (
'cscript //nologo "%temp%\file.vbs"^|find /v /i "%~nx0"') do @%%a
@del "%temp%\file.vbs"

@goto :exit
[Vbscript code goes here]

keeps the all the vbcode in one place. same caveats apply that the "@"
character can't appear in the vbscript lines.

As you're coding in vbscript why not use the File.Move property rather
than shelling to DOS to do it?

foxidrive

unread,
Jul 14, 2008, 3:34:43 PM7/14/08
to
On Mon, 14 Jul 2008 10:14:08 -0700 (PDT), Tom Lavedas <tglb...@cox.net>
wrote:

Nicely elegant Tom. :)

Tom Lavedas

unread,
Jul 14, 2008, 4:30:17 PM7/14/08
to

Brilliant! (but I think you meant to say) ...

@goto :EOF

That's what happens when the trees are so close they obscure the
forest. Having started with the script being ECHOed to the temporary
file at the start in the original, I was blinded to seeing the
obvious.

On the subject of the MOVE - then what's left to do in batch? ;-)

There is one reason I can think of - that is to wrap a WSH script so
that it is forced to execute under the cscript host. In this case, it
might look like this (no longer needs the FOR construct) ...

@rem Seconds.bat
@if '%1==' %0 "."


@find /v "@" < "%~f0" >"%temp%\file.vbs"

@cscript //nologo "%temp%\file.vbs" "%~1" %2
@del "%temp%\file.vbs" & @goto :EOF
' File.vbs
set oFSO=CreateObject("Scripting.FileSystemObject")
For Each f in oFSO.GetFolder(wsh.arguments(0)).Files
if instr(lcase(f.name), ".cmd") = 0 then


c=f.DateCreated : a=f.DateLastAccessed : m=f.DateLastModified
n = m

if wsh.arguments.count > 1 then wsh.echo "Renaming", f.name
f.name = Year(n) & P0(Month(n),"") & P0(Day(n),"_") & _
P0(Hour(n),"h") & P0(Minute(n),"m") & P0(Second(n),"s_") &
f.name
end if


Next
function P0(DT,s) : P0=Right("0" & DT,2) & s : end function

While I was diddling, I added two command line arguments: %1 names a
target folder for the files to be renamed and %2, if present, causes
the script to display the name of the file being renamed. If the
current folder is to be used with the second (verbose) argument, just
indicate it with a dot, as in ...

seconds . v

I guess a third could be added to provide an extension mask - it would
probably be best to make it the second argument and move the 'verbose'
switch to argument three.

BTW, I did not use the MOVE, because with the addition of a potential
source folder other than the current one, it would have proved
problematic. Instead, I just replaced the file's Name property with
the new naming string.

Todd Vargo

unread,
Jul 14, 2008, 4:38:21 PM7/14/08
to
Esra Sdrawkcab wrote:
...

>
> @find /v "@" < "%~f0" >"%temp%\file.vbs"
> @for /f "delims=" %%a in (
> 'cscript //nologo "%temp%\file.vbs"^|find /v /i "%~nx0"') do @%%a
> @del "%temp%\file.vbs"
> @goto :exit
> [Vbscript code goes here]
>
> keeps the all the vbcode in one place. same caveats apply that the "@"
> character can't appear in the vbscript lines.
>
> As you're coding in vbscript why not use the File.Move property rather
> than shelling to DOS to do it?

Either way is fine, however for clarification, his code is not "shelling to
DOS to do it". All it does is create a secondary batch file that can be
called by the batch (Tom left out the CALL line which was in foxidrive's
original batch code).

IMO, the batch method is easier to insert a plain ECHO or REM for testing
purposes. Also, the batch REN command requires the new name to be without
any path, whereas the VBS .Move Method requires the destination path to be
included (otherwise the file is moved to whatever folder happens to be
current at the time). As I said, either way is fine, you just have to choose
which way you like best.

Esra Sdrawkcab

unread,
Jul 15, 2008, 5:11:45 AM7/15/08
to
Tom Lavedas wrote:
> On Jul 14, 3:03 pm, Esra Sdrawkcab <ad...@127.0.0.1> wrote:
>> Tom Lavedas wrote:
>>> I couldn't resist tinkering ...

[snipped]

>> For a trivial amount of extra tinkering (only with the batch part)
>>
>> @find /v "@" < "%~f0" >"%temp%\file.vbs"
>> @for /f "delims=" %%a in (
>> 'cscript //nologo "%temp%\file.vbs"^|find /v /i "%~nx0"') do @%%a
>> @del "%temp%\file.vbs"
>> @goto :exit
>> [Vbscript code goes here]
>>
>> keeps the all the vbcode in one place. same caveats apply that the "@"
>> character can't appear in the vbscript lines.

[snipped]

> Brilliant! (but I think you meant to say) ...
>
> @goto :EOF
>

Bother! I forgot to either: say untested or to test it.

Timo Salmi

unread,
Jul 15, 2008, 3:27:22 PM7/15/08
to
mmm <jul...@gmail.com> wrote:
> My Canon camcorder generates files named 000001.MTS, 000002.MTS, and
> so on.
> This isn't useful. I've been looking for a method to flawlessly
> rename folders full of those files with their date and time
> inserted into the filename, so I can sort them accordingly etc.

That basically has the same ingredients as the task as covered in
134} How do I advance the dates of my certain JPG files by one year?
http://www.netikka.net/tsneti/info/tscmd134.htm

Also of relevance
FILEINFO.CMD CMD shell for FILEINFO.VBS
FILEINFO.VBS FileInfo Visual Basic Script
ftp://garbo.uwasa.fi/pc/link/tscmd.zip

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Tom Lavedas

unread,
Jul 15, 2008, 6:04:35 PM7/15/08
to
On Jul 14, 4:38 pm, "Todd Vargo" <tlva...@sbcglobal.netz> wrote:
> Esra Sdrawkcab wrote:
>
> ...
>
>
>
> > @find /v "@" < "%~f0" >"%temp%\file.vbs"
> > @for /f "delims=" %%a in (
> > 'cscript //nologo "%temp%\file.vbs"^|find /v /i "%~nx0"') do @%%a
> > @del "%temp%\file.vbs"
> > @goto :exit
> > [Vbscript code goes here]
>
> > keeps the all the vbcode in one place. same caveats apply that the "@"
> > character can't appear in the vbscript lines.
>
> > As you're coding in vbscript why not use the File.Move property rather
> > than shelling to DOS to do it?
>
> Either way is fine, however for clarification, his code is not "shelling to
> DOS to do it". All it does is create a secondary batch file that can be
> called by the batch (Tom left out the CALL line which was in foxidrive's
> original batch code).

I didn't leave "out the CALL line". I avoided the need for a
secondary batch procedure - first by executing the script within a FOR
and with Esra's suggestion I just moved it into the WSH script in the
second incarnation.

> IMO, the batch method is easier to insert a plain ECHO or REM for testing
> purposes. Also, the batch REN command requires the new name to be without
> any path, whereas the VBS .Move Method requires the destination path to be
> included (otherwise the file is moved to whatever folder happens to be
> current at the time). As I said, either way is fine, you just have to choose
> which way you like best.

Yes, I alluded to this problem with the MOVE. That's why I didn't use
it, but rather just renamed the file using its Name property.

> --
> Todd Vargo
> (Post questions to group only. Remove "z" to email personal messages)

Tom Lavedas
=========

Timo Salmi

unread,
Jul 16, 2008, 3:18:41 AM7/16/08
to
mmm <jul...@gmail.com> wrote:
> renamed to
> 20080730_23h59m56s_000001.MTS
> 20080731_00h02m33s_000002.MTS

> Thanks in advance. I'm creating a weblog entry about this too, so more
> people can use it when it's finished.

You might find of some interest the subsequently somewhat updated
55} How to get the creation, last modified and last access of a file?
http://www.netikka.net/tsneti/info/tscmd055.htm

It elaborates some of the logic ingredients to arrive at the fine
solutions posted by foxidrive and Tom Lavedas.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland

Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

mmm

unread,
Jul 17, 2008, 6:20:43 AM7/17/08
to
On Jul 16, 9:18 am, Timo Salmi <t...@uwasa.fi> wrote:
> mmm <jul...@gmail.com> wrote:
> > renamed to
> > 20080730_23h59m56s_000001.MTS
> > 20080731_00h02m33s_000002.MTS
> > Thanks in advance. I'm creating a weblog entry about this too, so more
> > people can use it when it's finished.
>
> You might find of some interest the subsequently somewhat updated
>   55} How to get the creation, last modified and last access of a file?
>  http://www.netikka.net/tsneti/info/tscmd055.htm
>
> It elaborates some of the logic ingredients to arrive at the fine
> solutions posted by foxidrive and Tom Lavedas.

Thanks, that's wonderfully useful indeed.

By the way, I just noticed how this batch comes in handy
with files moved to harddisk coming from about every
mobile device, like the mp4 files from my Samsung phone,
and the jpegs from my Minolta photocamera.

Which reminds me. I'm also trying to evolve this script
so it recognizes .jpg, .jpeg, .png and then automatically
tries to grab the date-time from the EXIF info in the files,
and if not uses the file creation time.

Regards, from a cloudy Amsterdam,

Julius
http://jult.net

Reply all
Reply to author
Forward
0 new messages