Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Delete File and Create Folder

Featured Replies

Does anyone have any sample SM code for deleting files and creating folders?

Thanks,

Nick

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

  • Author

Fast and exactly what I wanted, thanks Jesse.

Cheers,

Nick

  • 1 month later...

Both work perfectly.

Thanks Jesse!

  • 4 weeks later...

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

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.