Log in

View Full Version : windows batch question


audyovydeo
23rd August 2010, 13:21
Ever since I got my Panasonic Lumix I have to deal with the .MTS files.

I got a series of batch scripts that rename / demux / remux to mp4 (encoding when needed) in WinXP.

The first thing I do is rename the files, because when you get to have dozens of files called 00001.MTS, it's a bit of a shag (if you'll forgive the regional expression) -- I'm sure you all know the problem.

Anyhow, my script for renaming files is this :


@echo off

:: created : 07.09.2009
:: reads a file's creation date and renames it with it

SETLOCAL enabledelayedexpansion

for %%z in (*.MTS) do (
echo %%~nz
set name1=%%~nz
set name=!name1!
set day1=%%~tz
set day=!giorno1:~6,4!!day1:~3,2!!day1:~0,2!
rename %%z !name!_!day!.MTS
)

ENDLOCAL


this adds a formatted date to the filename.
Where I need help in that I cannot figure out why this does not work :

rename %%z !day!_!name!.MTS

that is, if I want to have the date before the filename, the script handles the first file of the bunch twice.
This is completely illogical to me, as the script logic is unchanged, it's merely the rename command parameters that changes.

any ideas / advice / explanations are welcome.

cheers
audyovydeo

TinTime
23rd August 2010, 13:50
I've found this before too. It loops through in alphabetical order and can pick up new stuff that wasn't there when the loop started, depending on whether the new items appear before or after the current bit of the loop. Why it does that I don't know.

Instead of the rename you could move the files to a temporary directory, and then move them all back after the loop has finished.

audyovydeo
23rd August 2010, 13:56
Instead of the rename you could move the files to a temporary directory, and then move them all back after the loop has finished.


Yes, I know about the MOVE workaround, but it seems to me : 1. inelegant 2. uneconomic.

I'm all the more frustrated as this test script works as expected :


for %%z in (*.MTS) do (
echo %%~nz
set name1=%%~nz
set name=!name1!
set day1=%%~tz
set day=!day1:~6,4!!day1:~3,2!!day1:~0,2!
:: rename %%z !day!_!name!.MTS
echo !name!_!day!.MTS
echo !day!_!name!.MTS
)

ie : it's the RENAME that screws up, and even then not really, as it works fine in the first example.
Damn frustrating !

cheers
a/v

TinTime
23rd August 2010, 14:07
How about...

for /f "usebackq delims=" %%z in (`dir "*.MTS" /b`) do (
etc.
etc.
)

...?

audyovydeo
23rd August 2010, 14:13
How about...

for /f "usebackq delims==" %%z in (`dir "*.MTS" /b`) do (
etc.
etc.
)

...?

I'm intrigued.
It works on my test set of files.
All that remains for me to do now is ... understand why .... !


thanks
a/v

TinTime
23rd August 2010, 14:22
I think what's happening with your original command is that at the start of each loop FOR is looking for the next file to process. This may turn out to be one you've just created with rename.

The command I proposed executes DIR first to bring back a set of results which FOR then loops through. In this case any "new" files created by rename are irrelevant because the set to process has already been created.

If that makes sense :)

Anyway, glad it works.

EDIT:
Ooops, made a typo. That'll teach me to copy and paste from the FOR help without checking first.
It should be:
for /f "usebackq delims=" %%z in (`dir "*.MTS" /b`) do (
ie. only one equals sign after "delims".

Guest
23rd August 2010, 14:26
Bravo, TinTime! That makes perfect sense to me.

audyovydeo
23rd August 2010, 14:44
Bravo, TinTime! That makes perfect sense to me.

Thanks indeed.
I understand your command.
What beats me is that rename behaves differently in the two cases I proposed. My loop is unchanged. I dont see why it should make an additional iteration in one case, and not the other.

Anyhow, as long as i have a working script...

Thanks to you both I'm updating my master script to use the excellent DGAVCindegDI to rename | demux | [encode] | remux to mp4 all my MTS files.

cheers
a/v

Guest
23rd August 2010, 15:03
Thanks to you both I'm updating my master script to use the excellent DGAVCindexDI to rename | demux | [encode] | remux to mp4 all my MTS files.
And to schweinsz for his fine decoder that I invoke dynamically.

audyovydeo
23rd August 2010, 15:05
And to schweinsz for his fine decoder that I invoke dynamically.

Yo ! Respect !

(man, you've been hanging around legal departments & lawyers lately, I can tell ... ;-)

cheers
a/v

TinTime
23rd August 2010, 16:01
Bravo, TinTime! That makes perfect sense to me.

Thanks very much!

What beats me is that rename behaves differently in the two cases I proposed. My loop is unchanged. I dont see why it should make an additional iteration in one case, and not the other.

To be honest I think that the behaviour of FOR when looping through a bunch of files isn't quite as simple as I described.

I come across your original problem quite a lot. When I'm encoding video I generate a batch script for each encode. I then run a master script that just uses a FOR loop to run each script sequentially. Quite often while the encodes are running I'll generate more scripts for more encodes.

When I do this I find that if the new script is alphabetically before the currently executing script then the FOR loop will never pick it up. If it's alphabetically after the currently executing script then the FOR loop will sometimes pick it up. Only sometimes though. I never investigated further because it doesn't matter to me one way or another, unlike your situation.

audyovydeo
23rd August 2010, 16:14
To be honest I think that the behaviour of FOR when looping through a bunch of files isn't quite as simple as I described.
... Only sometimes though....


Which can only make me think of the ancient proverb :


DOS batch files suck



(actually the originator is here :http://gmt.soest.hawaii.edu/gmt/gmt_download.html)


cheers !
a/v