Nicholas Orr Posted July 9, 2009 Posted July 9, 2009 Does anyone have any sample SM code for deleting files and creating folders? Thanks, Nick
Jesse Barnum Posted July 9, 2009 Posted July 9, 2009 Hey Nick - here are two modules I just wrote up. I've added them to the ScriptMaster.fp7 file, so they'll be in the next release. === CreateFolder === This takes an operating system path as a parameter (for example, /Users/Shared/newFolder or C:Documents and SettingsMyAccountnewFolder) and creates that folder. It returns a 1 if the folder was created, 0 if it already exists, and an error if the folder does not exist and could not be created. It will create nested subdirectories, so if /Users/Shared existed and you use /Users/Shared/a/b/c as the path, it will create folders a, b, and c. This takes one parameter, 'pathToCreate': File dir = new File(pathToCreate); if( dir.exists() ) return false; if( dir.mkdirs() ) { return true; } else { File realParent = dir; while( realParent != null && ! realParent.exists() ) { realParent = realParent.getParentFile(); } if( realParent != null && !realParent.canWrite() ) { throw new FileNotFoundException("Directory " + dir.getAbsolutePath() + " could not be created because the parent directory " + realParent.getAbsolutePath() + " is not writeable" ); } else { throw new FileNotFoundException("Directory + " + dir.getAbsolutePath() + " could not be created because of an unknown error." ); } } ===DeleteFileOrFolder=== This takes an operating system path as a parameter (for example, /Users/Shared/newFolder or C:Documents and SettingsMyAccountnewFolder) and deletes that folder or file. It returns a 1 if the folder or file was deleted, 0 if it does not exist, and an error if the folder or any of its contents could not be deleted. It will recursively delete subdirectories, so be careful! this takes one parameter, 'pathToDelete': File toDelete = new File(pathToDelete); if( ! toDelete.exists() ) { return false; } else { deleteRecursive( toDelete ); } void deleteRecursive( File fileToDelete ) throws IOException { if( fileToDelete.isDirectory() ) { File[] children = fileToDelete.listFiles(); for( int n=0; n
Nicholas Orr Posted July 10, 2009 Author Posted July 10, 2009 Fast and exactly what I wanted, thanks Jesse. Cheers, Nick
sprynmd Posted September 22, 2009 Posted September 22, 2009 I'm using DeleteFileOrFolder() on both Mac & PC. It seems to work for me but with unexpected returned results. I found that when the file is successfully deleted, the return result is EMPTY instead of 1. I do get a 0 if the file does not exist. ALSO, DeleteFileOrFolder() deletes the file even if it is open by another application. (MSWord gets really confused when that happens.) I was originally checking for an error code so I could warn the user. ...Mike
Recommended Posts
This topic is 5552 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now