XP batch file

Microsoft Windows xp professional w/serv...
April 9, 2010 at 00:09:29
Specs: XP Pro , Quad Core/8GB
Hi I’m looking for some help with compiling a simple
batch file.

This is to be run on Win XP Pro x32 (SP3).

This is part 1 of what I am trying to achieve:

1. Create a separate directory for each movie; and
2. Move the relevant movie file(s) into that new
directory.

So let’s say the following video files are located in the
folder C:\Movies

Movie A – Pt1.mkv
Movie A – Pt2.mkv
Movie B.mkv
Movie C – Pt1.mkv
Movie C – Pt2.mkv
Movie C – Pt3.mkv

The script needs to:

1. Create three new directories (ie: C:\Movies\Movie
A, C:\Movies\Movie B and C:\Movies\Movie C). The
created directory should only contain part of the
filename, the name of the movie only. Consistent file
naming will be used and this naming convention can
be modified to suit the batch file – ie: to make it work.

2. Move the relevant movie files into the relevant
movie directory.

I would want to run the batch file from the c:\Movies
directory and probably create a right click context
menu option when highlighting the directory (which I
know how to do).

Part 2 of my plan is to add a playlist file which will
reside in each individual movie directory where there
is more than one part / file. I have some of this batch
file figured out already.


Thanks in advance.


See More: XP batch file

Report •


#1
April 9, 2010 at 10:00:05
need to know the exact format of the existing filenames, pls give an example.

Report •

#2
April 9, 2010 at 14:39:26
The file format is along the lines I stated, ie:

Movie A – Pt1.mkv
Movie A – Pt2.mkv
Movie B.mkv
Movie C – Pt1.mkv
Movie C – Pt2.mkv
Movie C – Pt3.mkv

Some actual examples would be:

"Godfather, The.mkv"
"Godfather, The - pt2.mkv"
"Seven Samurai.mkv"

All files are in .mkv (Matroska) format. Some larger movies are split into 2 sometimes 3 parts.

I am starting to do more HD conversions on my existing library and so will have more parts to the movies.

I use Handbrake to convert the files and if I do several movies at once using a queue feature, at the end of the process there might be 20 or more individual movie files in the output directory.

Do I need to change the overall file naming procedure?


Report •

#3
April 9, 2010 at 19:49:54
i would like to say "yes, get rid of spaces and commas", but no, you can make directories "as is".
this might seem inefficient and cumbersome, but due to programmer laziness, here's what i'd do. First, get all the " - pt.mkv" files out of the way, then make all the directories, then process the - pt?.mkvs.
@echo off & setlocal enabledelayedexpansion
:: a holding tank
md temp
:: -- step 1
for %%a in ("* - pt??.mkv") do move "%%a" temp
:: -- step 2
for %%a in ("*.mkv") do (
set xx=%%~na
md "!xx!"
move "%%a" "!xx!"
)
:: -- step 3
cd temp
for %%a in ("*.mkv") do (
set xx=%%~na
::-- this is making the egregious assumption that no pts are
::-- more than one digit! i might be grossly wrong here.
set xx=!xx:~0,-6!
move "%%a" "!xx!"
)
:: ------ end
i'm not even going to start on spaces-in-filenames and even worse commas-in-filenames.
its the burr up my ass from day one, but "let it go... let it go..." Lol!
i'm sure this script has lots of bugs... Orkin on the way!

Report •

Related Solutions

#4
April 9, 2010 at 21:05:45
The concept of moving the - Pt files into a temp directory seems fine to me.

i have tried the batch file and it creates a directory for all the files including the files with the " - Pt??" and it does not move any of the files into any of the directories... so you just end up with all the movie files sitting in the directory with new directories mirroring each and every movie file.

how can i debug from here?


Report •

#5
April 9, 2010 at 21:26:11
i think it's them friggin spaces done shot me down!
i hate'em i hate'em etc etc:
for %%a in ("* - pt??.mkv") do move "%%a" temp
...
move "%%a" "!xx!"
::---
note the addition of the dbl-quotes, and i hope whoever brought the "relaxed" features into the filesystem has their __ system equally bollixed for an equal measure of suffering! (not really, i try to forgive, but still...AAAAhhg!!!)
ps: prob'ly more fixes, but these are the ones that stood out.
ps: i think i found 1 or 2 more of the same error (lacking dbl quotes around %%a) at any rate, look out for these (what the hell was wrong with "_" for crap sake! go_to_hell looks just as good as "go to hell" and way less much bulls---). sorry... sorry...

Report •

#6
April 9, 2010 at 22:19:52
Great that has step 1 and 2 sorted.

step 3 is a bit weird. it ends up converging all pt files into one new file with the name of the file being the exact name of the directory in step 2.

i noticed that when i added the "/f" switch in the line:
for /f %%a in ("* - pt??.mkv") do move %%a temp
that solved the problem for step 1 but using it subsequently for steps 2 and 3 created other problems.

sorry about the file naming! i can appreciate the headaches this creates for programming.

p.s. forgot to add that in step 3, the converged pt files remain in the temp dir and are not moved into the relevant movie dir.


Report •

#7
April 9, 2010 at 23:01:29
sorry, i'm a dunce. forgot:
move "%%a" "!xx!"
the dot-dot:
move "%%a" ..\"!xx!"
-
at least, i think thats all, prob'ly more...
appreciate your patience with my messups, and
i can't blame this one on MS, dammit! only me! Lol!
as for filenameing, mostly people have to deal with whatever they get from other softwe, so i completely understand that. if i don't rave on this subject, i'm probably sedated. sorry...

Report •

#8
April 9, 2010 at 23:19:43
Beaut.. all good.

Now to push my luck..

part 2 of my puzzle is to create a playlist for those directories which have one or more of the pt files (so that when watching the movie it will automatically play all parts).

lets say we now have:

C:\Movies\Godfather, The\

which we created from step 2, and in that dir we have:

Godfather, The.mkv
Godfather, The - Pt2.mkv
Godfather, The - Pt3.mkv

i need to create a playlist file, in this case to be called:
Godfather, The.zpl

(extension just needs to be .zpl) and in that file it needs to say:

NM=C:\Movies\Godfather, The\Godfather, The.mkv
BR!
NM=C:\Movies\Godfather, The\Godfather, The - Pt2.mkv
BR!
NM=C:\Movies\Godfather, The\Godfather, The - Pt3.mkv
BR!

i created a batch file which would do this based on the dir command, but it only works if there are exactly the same number of movie parts in the relevant directory that are catered for in the script.

hurts my head to work out how to do this.


Report •

#9
April 10, 2010 at 02:58:38
Something like this ?

--- UNTESTED ---


p_name=Godfather, The

set p_dir=C:\Movies\%p_name%
set p_zpl=%p_name%.zpl

cd /d "%p_dir%"
del /q "%p_zpl%"
for %%f in (*.mkv) do (
echo NM=%p_dir%\%%f>>"%p_zpl%"
echo BR!>>"%p_zpl%"
)


Report •

#10
April 10, 2010 at 10:56:16
need quotes here?
(depends on the movieplayer software i would guess, since
it's internal to the .zpl script).
for %%f in (*.mkv) do (
echo NM="%p_dir%\%%f">>"%p_zpl%"
echo BR!>>"%p_zpl%"
)

Report •

#11
April 10, 2010 at 15:13:44

No, the program (Zoom Player) does not require the .zpl to contain quotes for the path/filename.

Tried this:
set p_dir=C:\Movies\%p_name%
set p_zpl=%p_name%.zpl

cd /d "%p_dir%"
del /q "%p_zpl%"
for %%f in (*.mkv) do (
echo NM=%p_dir%\%%f>>"%p_zpl%"
echo BR!>>"%p_zpl%"
)

Nothing happens (ie: no .zpl file created in any directory). I assume this can be run separately from the other batch file containing steps 1 to 3, as this is how I tested it? ..and that i can run this portion of the batch file from within c:\movies (ie: c:\movies\test2.bat)?

I assume:
set p_dir=C:\Movies\%p_name%

does not need quotes? (query that the movie directory will have spaces and commas etc)


Report •

#12
April 10, 2010 at 19:20:15
i did notice there was a "set" missing in line 1, but you must have caught that or the script would have thrown an error.
oh, i just saw, looks like you left it off entirely, hence the reason no error! you need line 1:
set p_name=Godfather, The
or, if you want to make it adaptable:
shift
set p_name=%*
if "%p_name%" equ "" set /p p_name=movie to play:
::----
other than that, his script should work, just put one or the other of those variations at the top. he added the quotes further down, so no need for those here.

Report •

#13
April 10, 2010 at 23:19:23
Ah my bad explanation.

this now works except that what i need is for the batch file to create the playlist file (.zpl file) for every (movie) sub-directory within c:\Movies, without having to enter the name of the movie... so it will look inside each sub-directory on c:\Movies and create the .zpl file based on the files that are in each directory. does that make sense?

the playlist itself and the activation of the movie occurs from within another application.

Also the only problem with the batch file is that the order is wrong.. it is placing the first movie file last rather than first (which is correct in terms of the files as they are listed in the directory), so it is doing this:
NM=C:\Movies\movie c\movie c - pt2.mkv
BR!
NM=C:\Movies\movie c\movie c - pt3.mkv
BR!
NM=C:\Movies\movie c\movie c.mkv
BR!

rather than:
NM=C:\Movies\movie c\movie c.mkv
BR!
NM=C:\Movies\movie c\movie c - pt2.mkv
BR!
NM=C:\Movies\movie c\movie c - pt3.mkv
BR!

so, is there a way to make the last file in the directory added first, followed by the order of the other parts?

really appreciate the help so far.


Report •

#14
April 11, 2010 at 03:17:12
> does not need quotes? (query that the movie directory
> will have spaces and commas etc)
>

Theoretically, you always need quotes.

Some commands do understand if you don't use quotes, and if the value given does not have whitespaces, you don't need them.

My advice, when the least amount of doubt, use them. That should be always ... but even I don't do that.


Report •

#15
April 11, 2010 at 11:47:33
yeah, kind of cumbersome, (two passes due to funky sort) but i can't think how to do it easier:
cd /d c:\movies
for /r %%z in (.) do (
del /y *.zpl
set aa=/v
call :xx
set aa=
call :xx
)
goto :eof
:xx
for /f "tokens=*" %%a in ('dir /b /o:n ^| find %aa% /i " - pt"') do (
>> "%%~na.zpl" echo NM=%cd%\%%a
>> "%%~na.zpl" echo BR!
)

::------ not tested!
also, not clear where you wanted the zpl filescripts to wind up.
this just leaves them in the movie-directory with the movie itself. you can look at the code and see some assumptions i took liberties with, such as no subdirectories beneath 1 layer.
There will be errors, so you might want to add some echos and/or pauses in at strategic points. note that it destroys existing .zpls assuming they're bad.
another approach could be take the last line of each existing zpl file and make it the first, but i think that would be more trouble than this way.


Report •

#16
April 11, 2010 at 19:49:35
I wouldn't mind that this part of the script which is creating
the playlist files is separate to the other part, ie: so I will run
these scripts separately.

the playlist script will be handy because it can overwrite all
the playlist files in each of the movies directories, which will
update any movie file/parts. There might be 400 individual
movie directories.

There are some problems with the script.

First: There is an invalid switch on the del command. Here
is the first part of the output from the script:

C:\Movies>cd /d c:\movies

C:\Movies>for /R %z in (.) do (
del /y *.zpl
set aa=/v
call :xx
set aa=
call :xx
)

C:\Movies>(
del /y *.zpl
set aa=/v
call :xx
set aa=
call :xx
)
Invalid switch - "y".

query: doesn't it default to "yes please" delete without asking and you use the /p switch if you want to be prompted?

Second: Then further it outputs this:

C:\Movies>for /F "tokens=*" %a in ('dir /b /o:n | find /v /i " -
pt"') do (
echo NM=C:\Movies\%a 1>>"%~na.zpl"
pause
echo BR! 1>>"%~na.zpl"
)

C:\Movies>(
echo NM=C:\Movies\movie A, The 1>>"movie A, The.zpl"
pause
echo BR! 1>>"movie A, The.zpl"
)
Press any key to continue . . .

So it creates the .zpl file and places it in the c:\Movies
directory. Can it place the .zpl file in the actual movie
directory (ie: the sub-directories, in this case
c:\Movies\movie A, The)?

Third: it puts this in the first line of the playlist file:
NM=C:\Movies\movie A, The

rather than this:
NM=C:\Movies\movie A, The.mkv

..and it puts this in the second line of the playlist file:
BR

rather than:
BR!

Fourth: it then does this for all sub directories on c:\movies
(great), but also then creates a .zpl for any other files sitting
in the c:\movies directory.. so in my case i have the two test
script .bat files sitting in c:\movies, so it created a test.zpl
and a test2.zpl

Fifth: it then cycles through again about 12 twelve times in
all cases and so you and up with this in each of the relevant
.zpl files:

NM=C:\Movies\movie A, The
BR!
NM=C:\Movies\movie A, The
BR!
NM=C:\Movies\movie A, The.zpl
BR!
NM=C:\Movies\movie A, The
BR!
NM=C:\Movies\movie A, The.zpl
BR!
NM=C:\Movies\movie A, The
BR!
NM=C:\Movies\movie A, The.zpl
BR!
NM=C:\Movies\movie A, The
BR!
NM=C:\Movies\movie A, The.zpl
BR!
NM=C:\Movies\movie A, The
BR!
NM=C:\Movies\movie A, The.zpl
BR!
NM=C:\Movies\movie A, The
BR!
NM=C:\Movies\movie A, The.zpl
BR!


and so in the case of the test2.zpl i get this:

NM=C:\Movies\test2.bat
BR!
NM=C:\Movies\test2.bat
BR!
NM=C:\Movies\test2.zpl
BR!
NM=C:\Movies\test2.bat
BR!
NM=C:\Movies\test2.zpl
BR!
NM=C:\Movies\test2.bat
BR!
NM=C:\Movies\test2.zpl
BR!
NM=C:\Movies\test2.bat
BR!
NM=C:\Movies\test2.zpl
BR!
NM=C:\Movies\test2.bat
BR!
NM=C:\Movies\test2.zpl
BR!
NM=C:\Movies\test2.bat
BR!
NM=C:\Movies\test2.zpl
BR!


again, the test2.bat is the script containing this:

cd /d c:\movies
for /r %%z in (.) do (
del /y *.zpl
set aa=/v
call :xx
set aa=
call :xx
)
pause
goto :eof
:xx
for /f "tokens=*" %%a in ('dir /b /o:n ^| find %aa% /i " - pt"')
do (
>> "%%~na.zpl" echo NM=%cd%\%%a
pause
>> "%%~na.zpl" echo BR!
)

..[except i have added the pauses]

Sorry for the length of the post.. but felt needed to give some
the output of the script.


Report •

#17
April 11, 2010 at 21:11:21
yeah, i deserved that, (Lol!) for not testing, since it's a fairly involved scenario.
here's reprise:
@echo off & setlocal enabledelayedexpansion
for /d %%z in (*.*) do (
pushd %%z
del *.zpl 2>nul
::debug: we should now be in the individ. movie directory.
cd
pause
del *.zpl
set aa=/v
call :xx
set aa=
call :xx
popd
)
goto :eof
:xx
for /f "tokens=*" %%a in ('dir /b /o:n *.mkv ^|find !aa! /i " - pt"') do (
>> "%%z.zpl" echo NM=%%a
setlocal disabledelayedexpansion
>> "%%~z.zpl" echo BR!
setlocal enabledelayedexpansion
)
::-------- end script
you're right about the DEL switch, i always get it mixed up with copy and move (which have "/y").
i "somewhat" tested this one. it seemed to work. waiting for the next "suprise" (ain't life dull withoutem!)


Report •

#18
April 11, 2010 at 22:09:07
Looking good.. just two issues:

in the .zpl file it is omitting the full path and putting this:

NM=movie c.mkv
BR!
NM=movie c - pt2.mkv
BR!
NM=movie c - pt3.mkv
BR!

rather than this:

NM=c:\Movies\movie b\movie b.mkv
BR!
NM=c:\Movies\movie b\movie b - pt2.mkv
BR!
NM=c:\Movies\movie b\movie b - pt3.mkv
BR!

second: when i run the script it is returning this for each
relevant movie directory.. not sure why and yet apart from
the issue noted above, it is creating the .zpl properly, in the
right location and with the right file order list?

this is the output:

C:\Movies\movie A, The
Press any key to continue . . .
Could Not Find C:\Movies\movie A, The\*.zpl
C:\Movies\movie B, The
Press any key to continue . . .
Could Not Find C:\Movies\movie B, The\*.zpl
C:\Movies\movie c
Press any key to continue . . .
Could Not Find C:\Movies\movie c\*.zpl
C:\Movies\movie d
Press any key to continue . . .
Could Not Find C:\Movies\movie d\*.zpl
C:\Movies\movie e
Press any key to continue . . .
Could Not Find C:\Movies\movie e\*.zpl
C:\Movies\temp
Press any key to continue . . .

Query: is it actually necessary to delete the .zpl in the first place? will it not just overwite any existing .zpl file?


Report •

#19
April 11, 2010 at 22:53:18
yah, forgot to add. change:
>> "%%z.zpl" echo NM=%%a
to:
>> %%z.zpl echo NM=%%dpnxa
this should get the "d"rive, "p"ath,"n"ame,"x"tension.
---
accidentally redundified the foll:
cd
pause
del *.zpl
::-
this was already done previous. just nix this stuff out (debugging leftovers - "you'll have that...")
should solve most outstanding !.
the other stuff, pipe out to null, remove all pauses.
ps: re: deletion of zpls: no, as it is here, it will only add to existing file. (">>" means "append-to"), so the zpl file needs to be deleted or re-initiated (@echo off > nnn.zpl)
"file not found" is only error i got and that due to miscellaneous directories in my test-space.

Report •

#20
April 11, 2010 at 23:04:59
crikey almost there.. beginning to enjoy this

first line of the .zpl is outputting this:
NM=%dpnxa


Report •

#21
April 11, 2010 at 23:53:23
it's late and i'm dum.
messed up, change:
>> %%z.zpl echo NM=%%dpnxa
to:
>> %%z.zpl echo NM=%%~pnxa
::----
forgot the tilde. hopefully, get close!


Report •

#22
April 12, 2010 at 00:49:26
fab!!!

i changed this:
>> %%z.zpl echo NM=%%~pnxa

to:
>> %%z.zpl echo NM=%%~dpnxa

as it left out the drive letter.

all i can say is you really know your craft.

this stuff does interest me. what/where is the best place to
learn the basics and build up some knowledge?


Report •


Ask Question