# Solved: for /r help



## Ehrlichia (Jul 1, 2012)

I don't know if this is the best place to put this but I'm sure admin's will move it if necessary.
I use a program called mkvtoolnix to convert my kids videos from avi to mkv container. so I can place chapters, cut out unnecessary parts for viewing on the WDTV. they use winavi to capture to avi and I convert to mkv with mkvtoolnix and put chapters in with mkvchapterizer.
I reciently found out batch files could do this with just one click.
here is what I found so far

@echo off
for %%a in (*.avi) do (
"mkvmerge" -o "%%~na.mkv" -a 1 -d 0 -S "%%~na.avi" --track-order 0:0,0:1
) 

this works great but just in one folder at a time. can I do this recursively in all their folders i.e. alans games,paulas vacation,
just once.
thanks.


----------



## Squashman (Apr 4, 2003)

```
@echo off
for /F "delims=" %%G in ('dir /a-d /b /s *.avi') do (
	"mkvmerge" -o "%%~dpnG.mkv" -a 1 -d 0 -S "%%~G" --track-order 0:0,0:1
)
```


----------



## Ehrlichia (Jul 1, 2012)

I would like to say that this worked for me but it didn't put in a batch file or run from the cmd prompt all I got was a blip, and putting a pause in for some reason didn't work either,,?
do you have any other advice? thank-you for the rapid response Squashman.


----------



## Squashman (Apr 4, 2003)

You need to be a little more specific on what you tried. I am not understanding you because I have to assume ENGLISH is not your native language.

Based on the information you gave me on how MKVMERGE works, I setup a batch file that will find all AVI files in a directory and its sub-directories and uses MKVMERGE to process those files.


----------



## Ehrlichia (Jul 1, 2012)

First thank-you, I apologize if I sounded cryptic. English Is my native language, getting what is in my head typed in this post is difficult. 
I have a whole drive dedicated to my video crazed kids, on this drive there are two folders one named alan the other paula. and in those folders there are 10-15 folders each (i.e. toys,vacation, nails etc.) they both add or delete video files on a daily basis and place them in their folder depending on what was recorded.
I was asked if there was a way to add chapters like a DVD at 2-5 minute intervals (holding the fast forward on the WDTV all the time wasn't fun.) what I found was MKVToolnix and MKVChapterizer. convert the files to MKV with the batch file above, then I import all files into MKVChapterizer to add the chapters. I thought that just adding a /R to this bat file I could click once and it would do this for all folders in that directory

here is what I tried with the code you placed above. I copied from the @ symbol to the end and placed it in a new text file and renamed it convert.bat
placed it at the root of g:\ drive, double clicked it and the DOS window opened and closed quickly, I then edited the code with a PAUSE to see what came up and it still opened and closed quickly.
I then did a shift+right click and opened a command window at the root and pasted the above code, it comes back with alans was not expected at this time. I stopped and wrote the last post. once again I'm sorry I sounded cryptic your help is greatly appreciated.
Ehrlichia.


----------



## Squashman (Apr 4, 2003)

Since you already had an existing FOR LOOP I would have thought that you would know about how to run a FOR loop from a command prompt and how to run it from a batch file.

```
H:\>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

  %variable  Specifies a single letter replaceable parameter.
  (set)      Specifies a set of one or more files.  Wildcards may be used.
  command    Specifies the command to carry out for each file.
  command-parameters
             Specifies parameters or switches for the specified command.

[color=red]To use the FOR command in a batch program, specify %%variable instead
of %variable.  [/color]
```
So you are saying that your BATCH file works just fine when executed from one of the folders that has files that need to be converted?
When you execute your existing batch file that works, where is MKVMERGE located?


----------



## Ehrlichia (Jul 1, 2012)

The program is located at C:\Program Files (x86)\MKVToolNix and the for loop was something I found in a post somewhere. what I did was just copy and paste that into a batch file and it worked.
what I do now is copy and paste the batch file in every folder and double click each one to do the conversion every other day.
I assumed that the /r added after the for command would let me double click it once, and it would search through all 15 folders and convert all the avi files, I was wrong. hence my post for help.


----------



## Squashman (Apr 4, 2003)

You shouldn't be using code you don't understand. Just because it works doesn't make it right. There really isn't much difference between my batch file and yours. All mine is doing is using the DIR command to traverse your directory structure to find all AVI files. The only way it would not execute is if it could not find the mkvmerge program. I am assuming that is in your path because your batch file works with it being in the Program Files Directory. So if your batch file works then so should mine.

Try copying and pasting this code directly into the command prompt and run it. Don't use a batch file.


```
for /F "delims=" %G in ('dir /a-d /b /s *.avi') do "mkvmerge" -o "%~dpnG.mkv" -a 1 -d 0 -S "%~G" --track-order 0:0,0:1
```


----------



## Ehrlichia (Jul 1, 2012)

*that did the trick thank you again*
I figured out what happened with the original code, I forgot to copy the end bracket to close off the batch file. it's the little things that make you feel the worst.


----------



## Ehrlichia (Jul 1, 2012)

Just an update, I have now re-arranged the code to convert the files then delete the old avi file (if put into a batch file)
it works like a charm for me.

@echo off
for /F "delims=" %%G in ('dir /a-d /b /s *.avi') do (
"mkvmerge" -o "%%~dpnG.mkv" -a 1 -d 0 -S "%%~G" --track-order 0:0,0:1
)

@echo off
for /F "delims=" %%G in ('dir /a-d /b /s *.mkv') do (
del "%%~dpnG.avi"
)


----------



## Squashman (Apr 4, 2003)

Why are you doing another FOR Loop to delete the AVI. Just do it after the MKVMERGE step in the original FOR LOOP. You are doubling the amount of time it takes for your script to run that way.


----------

