Community Forums Archive

Go Back

Subject:Problems creating folder
Posted by: Kit
Date:6/4/2011 12:11:14 AM

The following code saves named regions to a folder selected by the user. The problem is if the user makes a new folder it creates the folder but doesn't save the files. When the flder exists the files are saved. What is wrong? Thanks.

Kit


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

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

// Save file first
app.DoMenuAndWait("File.Save", false);

//**************************************************************************
//** Check regionPath.txt exists, create it if it is missing
if (!File.Exists("T:\\SoundForge\\savePath.txt"))
{
TextWriter fix = new StreamWriter("T:\\SoundForge\\savePath.txt");

// write a line of text to the file
fix.WriteLine("L:\\Sets");
fix.Close();
}
//**************************************************************************

// Open regionPath.txt
StreamReader current = new StreamReader("T:\\SoundForge\\savePath.txt");

// read last used path
string cPath = current.ReadLine().Trim();

current.Close();


ISfFileHost file = app.CurrentFile;
ISfDataWnd wnd = app.ActiveWindow;

SfAudioSelection asel = wnd.Selection;
if (asel.ccLength == 0)
{
DialogResult result = MessageBox.Show("Export All Regions?","Error: no regions found!", MessageBoxButtons.YesNo);

switch (result)
{
case DialogResult.Yes:
{
app.DoMenuAndWait("Edit.SelectAll", false);
break;
}
case DialogResult.No:
{
return "Does the file contain regions?";;
}
}

}

if (null == file)
return "Open a file containing regions before running this script. Script stopped.";
if (null == file.Markers || file.Markers.Count <= 0)
return "The file does not have any markers.";

// Choose a directory to save the rendered regions
string szDir = SfHelpers.ChooseDirectory("Select the target folder for saved files:", @cPath);

if (szDir == null || szDir.Length <= 0)
return "no output directory";

//make sure the directory exists
if(!Path.IsPathRooted(szDir))
{
string szBase2 = Path.GetDirectoryName(file.Filename);
szDir = Path.Combine(szBase2, szDir);
}

// Create new directory if one chosen in dialogue
Directory.CreateDirectory(szDir);

// wait for directory to be created
Wait:
if(!Path.IsPathRooted(szDir))
goto Wait;

string szBase = file.Window.Title;
string szType = szBase.Substring(szBase.Length - 4, 4);

ISfRenderer render = file.SaveFormat;
ISfRenderer rend = app.FindRenderer(null, szType);

foreach (SfAudioMarker mk in file.Markers)
{
if (mk.Start >= asel.Start && asel.Start + asel.Length >= mk.Start + mk.Length )
{
string szName = String.Format(mk.Name, rend.Extension);
szName = SfHelpers.CleanForFilename(szName);
string szFullName = Path.Combine(szDir, szName + szType);

// Overwrite existing files
if (File.Exists(szFullName))
File.Delete(szFullName);

SfAudioSelection range = new SfAudioSelection(mk.Start, mk.Length);
file.RenderAs(szFullName, rend.Guid, "Default Template", range, RenderOptions.RenderOnly);
}
}

//open savePath.txt
TextWriter tw = new StreamWriter("T:\\SoundForge\\savePath.txt");

// write new path to the file
tw.WriteLine(szDir);
tw.Close();

return null;
}


public void FromSoundForge(IScriptableApp app) {
ForgeApp = app; //execution begins here
app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));
string msg = Begin(app);
app.SetStatusText((msg != null) ? msg : 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)); }
public static string GETARG(string k, string d) { string val = Script.Args.ValueOf(k); if (val == null || val.Length == 0) val = d; return val; }
public static int GETARG(string k, int d) { string s = Script.Args.ValueOf(k); if (s == null || s.Length == 0) return d; else return Script.Args.AsInt(k); }
public static bool GETARG(string k, bool d) { string s = Script.Args.ValueOf(k); if (s == null || s.Length == 0) return d; else return Script.Args.AsBool(k); }
} //EntryPoint

Subject:RE: Problems creating folder
Reply by: roblesinge
Date:6/5/2011 11:40:29 AM

This seems like a really complicated way to do what you're describing. Let me make sure I understand what you're trying to accomplish. You want to take only named regions in a file (meaning there are unnamed regions you want to skip) and have them saved as separate files in a folder selected by the user? Or are all the regions named and you want them saved by those region names as separate files in a selected folder?

Rob.

Subject:RE: Problems creating folder
Reply by: Kit
Date:6/5/2011 5:41:28 PM

Thanks for the reply. I want to be able to save any regions highlighted by the user. I want the user to have the option to save to any chosen folder. If no region is highlighted I want to offer the user the option to save all regions to a chosen folder. I want to be able to save the path to the last used folder so that it becomes the default folder for next time the macro is run. My problem is that the code won't save the names if the regions if a new folder is created meaning that the user has to run the macro twice if a new folder is created.

Kit

Subject:RE: Problems creating folder
Reply by: roblesinge
Date:6/7/2011 3:30:56 PM

I think the ChooseDirectory method will allow the user to create a new folder. I don't see why you need to have the code that follows that call. The directory is either there or it isn't. If you're worried about a null result from that call, you can put it in a loop until the user gives you a valid file path.

Try commenting out that section and see if this solves your problem.

Rob.

Subject:RE: Problems creating folder
Reply by: Kit
Date:6/8/2011 5:42:55 PM

Not sure what you mean - I tried commenting out some lines but still had problems. I decided to rebuild the script using the example SaveRegionsAsFiles.cs from the SDK. I think I have what I need now. Thanks for your help.

Kit

Go Back