Community Forums Archive

Go Back

Subject:Generic Script to process files in a folder
Posted by: _TJ
Date:1/19/2006 1:38:20 PM

This is the second post in my ongoing series of generic examples. This script contains the code to process all of the files in a folder. See my previous post
<a href="http://www.sonymediasoftware.com/forums/ShowMessage.asp?MessageID=427441">Generic Script to process a bunch of files</a> for an example of how to ask the user to choose multiple files and then process the resulting list.

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 folder
2) shows how to open files from that folder one at a time and pass them to a function for processing. (the script only opens files matching *.wav, but you can easily change it to match some other pattern instead).
4) saves the file back to the original folder after processing if processing was successful.


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

public class EntryPoint {

public void Begin(IScriptableApp app) {

string strFolder = SfHelpers.ChooseDirectory("Choose a folder to process files from", @"c:\");

foreach (string strFilename in Directory.GetFiles(strFolder, "*.wav"))
{
SfStatus status = ProcessFile(app, strFilename);
if (status != SfStatus.Success)
{
DPF("{0} : {1}", strFilename, status.ToString());
// quit if processing or save failed.
break;
}
}
}

public bool bEnableProcessingLog = true;

public SfStatus ProcessFile(IScriptableApp app, string strFilename)
{
if (bEnableProcessingLog)
DPF("processing {0}", strFilename);

ISfFileHost file = app.OpenFile(strFilename, false, true);
if (null == file)
return SfStatus.Fail;


// TODO: do your file processing here.


// wait for processing to complete, and if it succeeds, save the file
SfStatus status = file.WaitForDoneOrCancel();
if (status == SfStatus.Success)
{
file.Save(SaveOptions.PromptIfNoFilename);
status = file.WaitForDoneOrCancel();
}

file.Close(CloseOptions.DiscardChanges);
return status;
}


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 on10/16/2007 2:36:38 PM by_TJ.
Subject:RE: Generic Script to process files in a folder
Reply by: kbruff
Date:5/1/2006 5:33:21 AM

To begin scripting what should I buy Visual C++, or Visual Basic I am not sure what is the best tool to buy.
Thanks,
Kevin
***

Subject:RE: Generic Script to process files in a fold
Reply by: _TJ
Date:5/3/2006 1:53:43 PM

You don't need anything other than Sound Forge.

However, you may find it more convenient to work in a dedicated code editor such as Visual Studio. If you do, then you need to make DLL projects and then run them using the "Script->Run..." menu item in Sound Forge.

I would personally recommend using Visual C#, but you should be able to use Visual Basic if you prefer. If you use Visual Basic, there are some gotchas mentioned by another user on this form. The default namespace for a Visual Basic project is incompatible with using that code with Sound Forge.

tj

Subject:RE: Generic Script to process files in a fold
Reply by: GPD
Date:10/16/2007 12:35:22 PM

If this:


foreach (string strFilename in Directory.GetFiles(strFolder, "*.wav"))


Is changed to:


foreach (string strFilename in Directory.GetFiles(strFolder, "*.aif"))


In effort to batch process a folder of AIFF files instead of Wav files, the script gives errors. I do not see anything else in the code that references .Wav. Do any other changes need to made in addition to this line to process something other than .Wav? Any ideas why this does not work?

Very great script BTW. Been using it with great sucess (on Wavs)!


Subject:RE: Generic Script to process files in a fold
Reply by: GPD
Date:10/22/2007 12:21:26 PM

Any idea why this fails with AIFF files?

Subject:RE: Generic Script to process files in a fold
Reply by: GPD
Date:10/22/2007 4:33:02 PM

I've figured out the problem:

Mac OS creates invisible files in the directory when saving over a network. (named: .filename.aif). the script tries to load these b/c they end in .aif, but it fails there b/c they are not audio files, thus stopping the processing.

Solution:

Manually deleete the invisible files prior to running, and the batch will run fine. ...including processing subfolders with the tip given in the other thread.

I guess I could even add this to the scipt maybe, but deleting files via script makes me a bit nervous...

Subject:RE: Generic Script to process files in a fold
Reply by: _TJ
Date:10/23/2007 3:46:52 PM

Alternatively, you could change the script so that when it fails to open a file, it just ignores the error and keeps going on.

just change this


ISfFileHost file = app.OpenFile(strFilename, false, true);
if (null == file)
return SfStatus.Fail;


to this


ISfFileHost file = app.OpenFile(strFilename, false, true);
if (null == file)
return SfStatus.Success;

Go Back