Subject:Close file issue
Posted by: RC_Intel
Date:2/1/2006 8:42:54 AM
I have a process where my customer records a file, cuts it into smaller files, reviews what they have done, then moves the file from a working directory to an active directory. I have scripted the moving of the files and everything works great as long as they have saved all changes before running the script. If they don't then they will be prompted to save the file but reguardless of whether they say yes or no the script will close file (which is what I want), and then error out with "The process can not access the file ... because it is being used by another application." (not what I want) Even though the file has been closed it seems to stay in memory until I close Sound Forge and reopen it. Any suggestions? Here is the main code: ... //close all open files SoundForge.ISfFileHostList fl = app.Files; foreach (SoundForge.ISfFileHost file in fl) { file.Close(SoundForge.CloseOptions.QuerySaveIfChanged); } app.WaitForDoneOrCancel(); //move working directory to active directory MoveDirectory(workingPath, activePath); ... private void MoveDirectory(string fromDir, string toDir) { //clear the destination directory string[] toArray = System.IO.Directory.GetFiles(toDir); foreach (string toFile in toArray) { System.IO.File.Delete(toFile); } if (fromDir != string.Empty) { //Move files to destination directory MoveFiles(fromDir, toDir, "*.au"); MoveFiles(fromDir, toDir, "*.html"); } } private void MoveFiles(string fromDir, string toDir, string filter) { string[] fromArray = System.IO.Directory.GetFiles(fromDir, filter); string fileName = string.Empty; foreach (string fromFile in fromArray) { fileName = fromFile.Substring(fromFile.LastIndexOf(@"\")); System.IO.File.Move(fromFile, toDir + fileName); } } |
Subject:RE: Close file issue
Reply by: _TJ
Date:2/1/2006 12:13:27 PM
Does it work if you let your script exit after closing the file (but before the move), then run another script to move the files? (I suspect a garbage collection issue on the file object that was closed. ) tj |
Subject:RE: Close file issue
Reply by: RC_Intel
Date:2/6/2006 5:34:50 AM
No. I split the script into 2 separate cs files, one to close the files (prompting to save) and the other to move the files. But unfortunately I still get the same error. |
Subject:RE: Close file issue
Reply by: _TJ
Date:2/6/2006 12:44:38 PM
No. I split the script into 2 separate cs files, one to close the files (prompting to save) and the other to move the files. But unfortunately I still get the same error. OK, probably not a garbage collection issue. Do you know the name of the file that is being used? |
Subject:RE: Close file issue
Reply by: RC_Intel
Date:2/7/2006 11:55:46 AM
I loop through the open files and do a file.Close(SoundForge.CloseOptions.QuerySaveIfChanged) to close them. While responding to your last post I began trying different combinations and found that I get the error if I follow these steps: 1. Record something 2. Save it 3. Highlight a section 4. Ctrl-X or Ctrl-C 5. Run the script. The key seems to be the Ctrl-C or Ctrl-X. If I delete a section or drag the section to a new window then the script runs just fine. But if I Copy a section or Cut it I get the error. Unfortunatly my customer uses Cut all the time. Is there a way for me to clear the clipboard in script? Or do you think it is something else? |
Subject:RE: Close file issue
Reply by: _TJ
Date:2/7/2006 12:52:41 PM
It might be the clipboard. try adding this line to your code IDataObject obj = System.Windows.Forms.Clipboard.GetDataObject(); |
Subject:RE: Close file issue
Reply by: RC_Intel
Date:2/28/2006 11:50:56 AM
It turned out that it was the clipboard but IDataObject obj = System.Windows.Forms.Clipboard.GetDataObject(); did not fix it. But, System.Windows.Forms.Clipboard.SetDataObject(""); did. Thanks for all the help! |
Subject:RE: Close file issue
Reply by: _TJ
Date:3/1/2006 12:29:06 PM
SetDataObject("") is emptying the clipboard, which you shouldn't do until you are certain that what is in the clipboard is audio from Sound Forge. Otherwise, you could have the text of an email that you spent an hour writing get thrown away because a script runs at an inopportune time. (Assuming that you were using the clipboard to move the text from one place to another). THE USER OWNS THE CLIPBOARD. That's the rule. You can look all you want, but never change its contents without asking the user first. Having a script that just willy-nilly deletes the clipboard contents is a severe violation of that rule. tj |