PDA

View Full Version : Writing Windows batch files


Blue_MiSfit
28th March 2007, 00:27
Hey everyone,

So I need to automate the execution of AVS2AVI. I have several large folders of AviSynth scripts that I created with ABatchScript, and they use the QTOutput function, which encodes QuickTime MOVs inside the script, instead of returning uncompressed video to an external encoder in the typical AviSynth fashion.

As such, the quickest and easiest way for me to get these output files is to run the scripts through AVS2AVI with the "p 0 -o n -c null" switches.

As you can imagine hand writing the commands in a batch file like this:

C:\avs2avi.exe "H:\Blur\Scripts\Cematary grave_107-1_1.avs" -p 0 -o n -c null
C:\avs2avi.exe "H:\Blur\Scripts\Cematary grave_107-1_2.avs" -p 0 -o n -c null
C:\avs2avi.exe "H:\Blur\Scripts\Wedding_23-3_1.avs" -p 0 -o n -c null


is extremely time consuming. First I have to get the raw file names by doing dir /b > list.txt, then I have to copy and paste the C:\avs2avi.exe to the beginning, the quotation marks, and finally the options to the end.

Dreadful when you have several hundred files to process.

Is there any way to do it like this? *pseudocode*
*get list of files
*iterate through list, running avs2avi.exe with specified options. If any files fail, then write an error to a log, and keep going
Surely using variables is possible with batch scripting?

Thanks!

~MiSfit

shakey
28th March 2007, 19:38
I could tell you how to do pretty much everything in that list except for finding the list of files. The one time I tried to do it I gave up and wrote a small C program using the "findfirst" and "findnext" functions.

Blue_MiSfit
28th March 2007, 19:55
I can get a list into a text file no problem.. dir /b >foo.txt.

Can I read these text files into a batch file?

~MiSfit

SvT
28th March 2007, 20:01
When I face these problems my solution is not pretty but it works.

Select the files and copy filename to a text file.

do a "search and replace" for "H:\Blur\Scripts\ to C:\avs2avi.exe "H:\Blur\Scripts\ and .avs" to .avs" -p 0 -o n -c null

There is your batchfile !

Not pretty but it works.

jeffy
28th March 2007, 22:02
Save the following text as "AVS_Batch_QT_Misfit.bat" and create a SHORTCUT to it anywhere.

@ECHO OFF
echo REM Misfit's batchfile :)>AVS_List.bat
For %%A IN ("%1"\*.avs) Do echo C:\avs2avi.exe "%%A%" -p 0 -o n -c null>>AVS_List.bat
@ECHO ON


Then drag & drop your folder onto the shortcut, you will find a file named "AVS_List.bat" in the folder where the batch file is.

Post your feedback :)

And now, something different: why does it not work when a folder is dropped directly onto the batch file? You tell me please, clever guys! :confused:

A little Thank you for the know-how:
*** CREDITS ****
http://www.robvanderwoude.com/index.html
http://www.fpschultze.de/
http://mirror.href.com/thestarman/DOS/DOS7INT.htm#for
http://forum.doom9.org/showthread.php?t=89920

foxyshadis
29th March 2007, 02:18
You should use "%1\*.avs" instead of "%1"\*.avs. In some circumstances the latter will work, but it'll usually just confuse the parser. But in this case it won't hurt, apparently.

If you mean you want it to just start the encode immediately, remove the echo statements and it'll just start encoding instead of writing a file to start encoding later.

jeffy
29th March 2007, 07:56
You should use "%1\*.avs" instead of "%1"\*.avs. In some circumstances the latter will work, but it'll usually just confuse the parser. But in this case it won't hurt, apparently.

If you mean you want it to just start the encode immediately, remove the echo statements and it'll just start encoding instead of writing a file to start encoding later.

Unfortunately, even with your change, it still doesn't work; it will not create the output file (AVS_List.bat) when you just drag & drop the folder onto the batch file AVS_Batch_QT_Misfit.bat. However, when you create a shortcut to this batch file (AVS_Batch_QT_Misfit.bat), it works. You just drag & drop the folder onto this shortcut and the output file (AVS_List.bat) will be created. The question is why and where my error is.... Thank you for your time.

Blue_MiSfit
30th March 2007, 03:45
Wow guys.

Thanks so much! I haven't had time to test it yet, but I should tonight :)

Exactly what I needed...

[edit]
Well, I tried it and get the same strange behavior. Only the shortcut works. Unfortunately, it doesn't totally work.

When I use the following file:

@ECHO OFF
echo REM Misfit's batchfile :)>AVS_List.bat

For %%A IN ("%1\*.avs") Do echo avs2avi.exe "%%A%" -p 0 -o n -c null>>AVS_List.bat
@ECHO ON


and drop a folder with these files in it:

Wedding_23-1_3.avs
Wedding_23-1_4.avs
Wedding_23-2_1.avs
Wedding_23-2_2.avs
Wedding_23-2_3.avs
Backplates1.avs
Backplates10.avs
Backplates11.avs
Backplates12.avs
Backplates13.avs
Backplates14.avs
Backplates15.avs
Backplates16.avs
Backplates17.avs
Backplates18.avs
Backplates19.avs
Backplates2.avs
Backplates20.avs
Backplates21.avs
Backplates3.avs
Backplates4.avs
Backplates5.avs
Backplates6.avs
Backplates7.avs
Backplates8.avs
Backplates9.avs
Cematary grave_107-1_1.avs
Cematary grave_107-1_2.avs
foo.txt
Wedding_23-3_1.avs


(Yes all the spaces is for real. Whoever logged this footage was a nerfherder :) )

I get the following batch file output:


REM Misfit's batchfile :)
avs2avi.exe """H:\Blur\Scripts" -p 0 -o n -c null


So, that seems like maybe I need to drag and drop all the AviSynth scripts as a group to the original batch file generator? When I do that I get the following:


REM Misfit's batchfile :)
avs2avi.exe """H:\Blur\Scripts" -p 0 -o n -c null
avs2avi.exe "2\" -p 0 -o n -c null


Looks like problems parsing all those dreadful spaces or something along those lines.

What do you brilliant coders think? I'm too high level, thats for sure :D

~Misfit

jeffy
30th March 2007, 09:42
When I use the following file:

@ECHO OFF
echo REM Misfit's batchfile :)>AVS_List.bat

For %%A IN ("%1\*.avs") Do echo avs2avi.exe "%%A%" -p 0 -o n -c null>>AVS_List.bat
@ECHO ON

~Misfit

Your code when saved as misfit.bat works like mine: when I create a shortcut to it and drag & drop the folder H:\Scripts\Blur onto the shortcut, it does what it should.
@ECHO OFF
echo REM Misfit's batchfile :)>AVS_List.bat

For %%A IN ("%1\*.avs") Do echo avs2avi.exe "%%A%" -p 0 -o n -c null>>AVS_List.bat
@ECHO ON


I have partially recreated the folder structure:
Wedding_23-2_3.avs
Backplates9.avs
Cematary grave_107-1_1.avs
Cematary grave_107-1_2.avs
foo.txt
Wedding_23-3_1.avs

The result, AVS_List.bat:
REM Misfit's batchfile :)
avs2avi.exe "H:\Scripts\Blur\ Wedding_23-2_3.avs" -p 0 -o n -c null
avs2avi.exe "H:\Scripts\Blur\ Backplates9.avs" -p 0 -o n -c null
avs2avi.exe "H:\Scripts\Blur\Cematary grave_107-1_1.avs" -p 0 -o n -c null
avs2avi.exe "H:\Scripts\Blur\Cematary grave_107-1_2.avs" -p 0 -o n -c null
avs2avi.exe "H:\Scripts\Blur\Wedding_23-3_1.avs" -p 0 -o n -c null


I have Windows XP Professional SP2, what is your OS version?

Blue_MiSfit
30th March 2007, 10:23
Same... Windows XP Pro SP2..

Very strange. Any other ideas?

snherbst
30th March 2007, 11:40
Hi

Ive made a wsf file to create the output off the files you select as arguments.
Just put it in the sendto dir.
Select your files, right click the selected files, choose sendto, choose "avs2avi_convert.wsf" Thats it.
7022
Please submit bugs.

If you want to know more go here
http://www.microsoft.com/technet/scriptcenter/default.mspx

PS: it wont take the whole dir you choose just the files

Blue_MiSfit
30th March 2007, 22:28
Wow! Thanks!

Could you possibly post it somewhere like megaupload, in case the mod doesn't approve the attachment? One click hosters are the preferred method to reduce strain on doom9s servers.

Thanks again!!

~MiSfit

smok3
30th March 2007, 22:39
what i do is:
- do a batch that will work for 1 file (one.bat)
- do a batct that loops around the first one, based on acsii file list(one_tcm.bat)
- use total commander to select files and do the list and run the batch (at the end is just: select files then select the proper batch)

let me know if you need any details.

jeffy
30th March 2007, 22:56
Can someone please verify if my batch file works as described above with their system? I just drag & drop the folder onto the shortcut and it processes all the avs files in the dropped folder (does not include subdirectories) - the result was also posted above. Thank you.

snherbst
31st March 2007, 01:08
@Blue_MiSfit

Goto my tripod-lycos page (http://members.lycos.co.uk/snherbst/)

Blue_MiSfit
2nd April 2007, 06:44
I get an Object Required: WshShell error when I try to run the avs2avi_convert.wsf script.

What am I missing??

~MiSfit

snherbst
2nd April 2007, 08:14
@Blue_MiSfit

Please open the file in a text editor and correct the line 22
(right click and sendto notepad)
Cmdln = "C:\avs2avi.exe " + Arg + " -p 0 -o n -c null"
to
Cmdln = "C:\YURE_LOCATION\avs2avi.exe " + Arg + " -p 0 -o n -c null"
or just if you have the avs2avi.exe in your path
Cmdln = "avs2avi.exe " + Arg + " -p 0 -o n -c null"

Blue_MiSfit
4th April 2007, 04:44
Same problem. Object required: 'WshShell'.

It points to line 29:

Return = WshShell.Run(Cmdln, 1, true)


Ideas?

~MiSfit

foxyshadis
4th April 2007, 13:19
Add the line
WshShell = CreateObject("WScript.Shell")
above it somewhere.

jeffy
4th April 2007, 21:39
@foxyshadis:

May I please ask you for trying out my above mentioned batch file, does it work for you and if yes, do you have any idea, why does it create the weird output mentioned below my post for Misfit?

Thank you very much.

snherbst
9th April 2007, 00:33
@Blue_MiSfit

I just found a bug
remove the ' from line 24
So it is like this line(without the quotes)
" Set WshShell = WScript.CreateObject("WScript.Shell")"
I vil opload a new one without bugs to my lycos page
http://members.lycos.co.uk/snherbst/

Blue_MiSfit
9th April 2007, 07:32
nice :) I will check it out when I'm at my client location tomorrow.

Blue_MiSfit
13th May 2007, 05:24
It works! The one problem is that it doesn't work with a lot of input files. It complains about too many arguments.

Is it possible to have it take say... 50 inputs at once?

Also, I would like to adapt this process to running Mencoder (for MOV to AVI remux operations). I think I did things correctly but apparently not...

My code:

<package>
<job id="ver0-1">
<script language="VBScript">

Function Run_Prg(Arg)
Dim fso, f, Cmdln
Cmdln = "mencoder " + Arg + " -oac pcm -ovc copy -ffourcc dvsd -force-avi-aspect 16/9 -o " + Arg + ".avi"
Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run(Cmdln, 1, true)
Run_Prg = Return
End Function

'Main Program
Dim i
Set objArgs = WScript.Arguments
For i = 0 to objArgs.Count - 1
Run_Prg WScript.Arguments(i)
Next
</script>
</job>
</package>


It opens up a new CMD window, but it closes before I can read anything. Ideas?

Thanks!

~MiSfit

snherbst
13th May 2007, 21:16
The problem is in arguments i windows scripting that each argument will have the full filenames path. So you will have a limitation of 10-20 files depending of the path depth.
If you have a path C:\avs\ you will be able to have more arguments than if you have a path C:\Documents and settings\user\movies\avs.
Alternately I've made a script that takes the path as an input instead. The script only takes the .avs files in the folder so you can also have .txt files or other file types in the folder.
Pease go to my lycos site to download the new script.
http://members.lycos.co.uk/snherbst/

snherbst
13th May 2007, 21:21
Please insert those tow lines instead of the cmdln or you will get a "file.avs.avi" file

out_file = left(Arg, len(Arg) - 4)
Cmdln = "mencoder " + Arg + " -oac pcm -ovc copy -ffourcc dvsd -force-avi-aspect 16/9 -o " + out_file + ".avi"

Blue_MiSfit
14th May 2007, 01:20
Please insert those tow lines instead of the cmdln or you will get a "file.avs.avi" file

out_file = left(Arg, len(Arg) - 4)
Cmdln = "mencoder " + Arg + " -oac pcm -ovc copy -ffourcc dvsd -force-avi-aspect 16/9 -o " + out_file + ".avi"


In this script, my input files are QuickTime MOVs (just so you know).

I made the changes you suggest, but it still doesn't work, the CMD window closes before I can read anything, and no output file is created.

Thanks so much for your help! I cant tell you what a time-saver this is shaping up to be!!

~MiSfit

gzarkadas
19th May 2007, 22:15
May I contribute another approach to this thread, which does not suffer from limitations in number of files?

In short, download gawk (GnuWin32 (http://sourceforge.net/projects/gnuwin32) is a nice place to get win32 builds of GNU utilites), install it in your path and then:

1. Write the following script (say avsenc.awk) and put it somewhere in your path:

{ # for every record
error = system("c:\\avs2avi.exe \"" $0 "\" -p 0 -o n -c null")
if (error)
print "Error while encoding file: " $0
}


2. Run the following command from the command prompt:
dir /b *.avs | gawk -f avsenc.awk

The example takes the parameters of the very first post of this thread. Also note the extra backslash needed for characters like \ and ".

Of course this does not easily get packaged in a shell shortcut, but one can hack the "HKEY_CLASSES_ROOT\Directory\shell" registry key and add there say a "BatchEncode\command" set of keys, specifying there something like "Batch Encode" for the 1st key and c:\windows\system32\cmd.exe /k dir /b "%1\*.avs" | gawk -f avsenc.awk for the second. Then a right click in any folder's icon will allow to run the command from the context menu.