Community Forums Archive

Go Back

Subject:Generic Script to process a bunch of files
Posted by: _TJ
Date:12/14/2005 12:30:59 AM

There have been several recent requests to 'batch' process a bunch of files with operations that the Sound Forge Batch Converter can't do.

In answering these qestions, I have provided sample scripts that process a single file, but it occurs to me that it might be useful to provide a generic script showing how to allow the user to choose a bunch of files, and then have the script open, process, and save them one at a time.

This is intended to be a script that you can use as a starting point for your own specialized batch processors. it

1) prompts the user to select a bunch of files
2) prompts the user to choose an output format (just change .mp3 to whatever you want to change the format type)
3) shows how to open files from the list one a time and pass them to a function for processing.
4) saves the file using the given output format and then closes it.




using System;
using System.IO;
using System.Windows.Forms;
using SoundForge;

public class EntryPoint {
public void Begin(IScriptableApp app) {

// pick a set of files.
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Choose files to process.";
ofd.FileName = @"E:\*.*";
ofd.Filter = "Wave (*.wav)|*.wav|Perfect Clarity Audio (*.pca)|*.pca|Mpeg Layer 3 (*.mp3)|*.mp3|All files (*.*)|*.*";
ofd.DefaultExt = "*.*";
ofd.Multiselect = true;
if (DialogResult.OK != ofd.ShowDialog(app.Win32Window))
return;

// choose an output format
ISfRenderer rend = app.FindRenderer(null, ".mp3");
ISfGenericPreset tpl = rend.ChooseTemplate(IntPtr.Zero, 0);
if (null == tpl)
return;

// now process them until we are done or the user hits cancel
//
foreach (string strFilename in ofd.FileNames)
{
// make an output filename using the input filename with _x appended.
string strDir = Path.GetDirectoryName(strFilename);
string strBase = Path.GetFileNameWithoutExtension(strFilename);
string strExt = Path.GetExtension(strFilename);
string strOutfile = Path.Combine(strDir, strBase + "_x" + "." + rend.Extension);

SfStatus result = ProcessAFile(app, strFilename, strOutfile, tpl);
if (result == SfStatus.Cancel)
break;
}
}

public SfStatus ProcessAFile(IScriptableApp app, string strFilename, string strOutfile, ISfGenericPreset tpl)
{
SfStatus result = SfStatus.Success;

// open the file
ISfFileHost file = app.OpenFile(strFilename, false, true);


/* add code here to process the file */


// wait for processing to finish
result = file.WaitForDoneOrCancel();
if (result != SfStatus.Success)
return result;

// save the file under the new name and given format
file.RenderAs(strOutfile, null, tpl, null, RenderOptions.RenderOnly);
result = file.WaitForDoneOrCancel();

file.Close(CloseOptions.DiscardChanges);

return result;
}

public void FromSoundForge(IScriptableApp app) {
ForgeApp = app; //execution begins here
app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));
Begin(app);
app.SetStatusText(String.Format("Script '{0}' is done.", Script.Name));
}
public static IScriptableApp ForgeApp = null;
public static void DPF(string sz) { ForgeApp.OutputText(sz); }
public static void DPF(string fmt, object o) { ForgeApp.OutputText(String.Format(fmt,o)); }
public static void DPF(string fmt, object o, object o2) { ForgeApp.OutputText(String.Format(fmt,o,o2)); }
public static void DPF(string fmt, object o, object o2, object o3) { ForgeApp.OutputText(String.Format(fmt,o,o2,o3)); }
} //EntryPoint


Message last edited on12/14/2005 12:32:55 AM by_TJ.
Subject:RE: Generic Script to process a bunch of files
Reply by: Keeba
Date:12/14/2005 8:55:15 AM

Is there a way to set this to select directories....? Just FYI - it is me.....the fella who is talking with you about the 'other' batch script....Letm e use this moment to explain my task.

I have 600 folders, all with one simple wav file in them. In another directory, I have 60 folders, each with another simple wav file in them. Note that these files are very short - 3 to 6 seconds. My process is as follows....I take one of hte 600 files (file inside the folder) and edit it together, individually, with each one of the 60 files.

As you can see, it gets a little confusing and arbitrary....well, more confusing, but the thing is that there are SO MANY FOLDERS (files) to choose, and to have to do this over and over again, it KILLS me.

So - to that end, I am tooling around with the script that you left me over at the 'other' post. This one as well, I have copied, and as I get comfortable with this stuff, I will hopefully be able to modify this a bit.

In the meantime, I am just wondering if this script can me adjusted to select directories rather than just files?

Subject:RE: Generic Script to process a bunch of file
Reply by: _TJ
Date:12/14/2005 6:41:02 PM

Well, there is no standard .net class that will allow you to select more than one folder.

But if all 600 of your folders are children of a single folder. i.e.


c:\stuff\one
c:\stuff\two
c:\stuff\three


There isn't any way to put up a dialog that can select multiple folders.

But you could select stuff and then modify this script to process all of the files in all of the folders underneath stuff. That's pretty easy - so long as you want to processing everything in a folder. It gets more complicated if you want to process only some of the files or folders in a folder.

tj

Message last edited on12/14/2005 7:26:24 PM by_TJ.
Subject:RE: Generic Script to process a bunch of file
Reply by: _TJ
Date:12/14/2005 7:27:42 PM

Is it necessary that you put all of your files one to a folder? that does complicate the scripting quite a bit to do it that way.

How about one folder with the 60 files and another with the 600?

tj

Subject:RE: Generic Script to process a bunch of file
Reply by: Keeba
Date:12/14/2005 8:38:52 PM

Well - the matrix is what becomes a problem. See, if I were to do that, then all files would be sent to one location, and then I would have the Widget Companies files mixed in with the MarcoPolo companies files. Then, I would have to go through and manually separate them into their own folders. See, I have this folder structure setup as it makes organizzation a little easier for this builk operation.

Now to that end, I CAN put all the 60 'meat' files into one folder and use that one folder as the resource folder. Then, run each of the 600 folders against the one folder and putting them into some other location. That would work.......then, though I would have a bit of a mess in terms of the 60 files, and THEIR organization, it would be FAR less a headache than the alternative.

let me know what you think about this thought.....

Subject:RE: Generic Script to process a bunch of file
Reply by: _TJ
Date:12/14/2005 9:54:48 PM

I can understand you wanting to be vague about your clients, but really. This isn't geting anywhere. Make up client names if you must, but please tell me USING ACTUAL FILENAMES what you need to do.

For the sake of brevity lets assume that you have 2 clients and 3 prompts.

clients---
c:\Megatron\Megatron.wav
c:\Janeco\Janeco.wav

prompts---
c:\Prompts\Stop.wav
c:\Prompts\Look.wav
c:\Prompts\Listen.wav

So Clients x prompts would generate 6 output files.

Megatron_Stop.wav
Megatron_Look.wav
Megatron_Listen.wav
Janeco_Stop.wav
Janeco_Look.wav
Janeco_Listen.wav

So the question is WHAT FOLDERS do the output files go into?
is it

c:\Megatron\Megatron_Stop.wav
..
c:\Janeco\Janeco_Stop.wav
..
etc

or is it
c:\Megatron\Stop\Megatron_Stop.wav
c:\Megatron\Look\Megatron_Look.wav
etc.

or is it
c:\Prompts\Megatron\Megatron_Stop.wav
..etc.

or is it
c:\Outputs\Megatron_Stop.wav
..etc

The possible variations here are endless, but without knowing what is needed,
it's nearly impossible to design the outer loop. walking files in a folder is pretty easy, but its not the same code as walking folders in a folder.

One more thing. working on your desktop generates HUGE filenames and it's gonna drive you crazy. You should make a folder at the root of C: or D: to work in that.






Subject:RE: Generic Script to process a bunch of file
Reply by: Keeba
Date:12/15/2005 3:14:10 PM

Ok - Ok - I get it. You're right, and I know it, I am just a little protective of the process and the property, BUT, as you say, we have to get to the bottom of this. Ok......so now, here it is....

first and foremost - ONE FOLDER. I have a 'shuttle' of sorts, a bat file that does an xcopy function for me that reads a list of file names and locations and moves the files to where they are supposed to go. For that reason, we can dump everything into ONE folder. That is NOT a problem anymore. I wont have to manually move the files.

Now, about the desktop issue - the truth is all of htis work will be done on an external drive. I have a drive setup exclusively for these files, so the actual location is E:\tones\tempates\...
Much simpler. The other one was a sample file I was testing the sciprt on....not the actual folders I will be using this process on. I will go and build some test folders on the E drive, and run the script against them there..........be back soon.

Oh - and again...thanks

Go Back