Deleting Files from Disk via scripting

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
pxlgirl
Posts: 4
Joined: March 7th, 2007, 1:14 pm

Hi,

I have written a script that automates render tasks on a specific job. It prompts the user to pick a render destination, input a version number, changes the comp name accordingly, queues the item and applies render settings and output module templates. Thank you all for the knowledge on this site already, I would not have been able to do this otherwise!

The one last thing I would like it to do is delete the contents of the folder that the user has chosen as their destination (doing away with previous renders). No way of knowing what these files are called or how many there are.

I'm at a loss. Any help is appreciated.

Thanks!

Mary.
User avatar
redefinery
Posts: 112
Joined: April 1st, 2005, 8:16 pm
Location: CA
Contact:

pxlgirl wrote:The one last thing I would like it to do is delete the contents of the folder that the user has chosen as their destination (doing away with previous renders).
hi mary,

please refer to the remove() methods for Folder and File objects, which you can find documented in the JavaScript Tools Guide (in CS3) or Bridge JavaScript Reference (in 7.0 and i think 6.5).

:jeff
pxlgirl
Posts: 4
Joined: March 7th, 2007, 1:14 pm

Thanks! I have looked in the javascript guide but am still lost as to exactly how to call the function. At first I thought it would be easier to just delete the whole folder and then recreate it, but the guide says you can only delete empty folders...so I guess I have to call the remove() function for each file in the folder? I tried this:


Code: Select all

var okaytoDelete = confirm ("Would you also like to empty the contents of your destination folder?"); 

					if (okaytoDelete) {

						var filestoDelete = HDDestination.getFiles();
						
								for (k = 1; k <= filestoDelete.length; ++k) { 
									var curFile = filestoDelete(k);
									curFile.remove(); //delete the current file
								}
						}
but when I run that it tells me my array is undefined.

I think I'm just not wrapping my head around how to do this, or my syntax is way off. Any ideas?
bradshaw1965
Posts: 98
Joined: March 14th, 2006, 2:16 pm
Location: Atlanta, GA
Contact:

I don't have the ref in front of me, but when you're removing items you generally want to work backwards down to 0, instead of incrementing up to a length, since length will change when you remove items.

Also, where are you setting the variable HDDestination? Is it defined in the debugger? If not, from the docs.
To create a Folder object, use the Folder function or the new operator. The constructor accepts full or
partial path names, and returns the new object.
Folder ([path]); //can return a File object
new Folder ([path]); //always returns a Folder object
so

Code: Select all

var HDDestination = Folder('path/to/folder')
or

Code: Select all

var HDDestination = new Folder('path/to/folder');
Dale
Dale Bradshaw
Technology Director | Primal Screen
creative-workflow-hacks.com
Atlanta, GA
pxlgirl
Posts: 4
Joined: March 7th, 2007, 1:14 pm

Found it!

The correct code for deleting the files needs to read:

Code: Select all

var okaytoDelete = confirm ("Would you also like to empty the contents of your destination folder?"); 

					if (okaytoDelete) {

						var filestoDelete = HDDestination.getFiles();
						
								for (k = filestoDelete.length; k >= 0; --k) { //run through all files in folder
									var curFile = File(filestoDelete[k]);
									curFile.remove(); //delete the current file
									
								}
						}
I was not defining the variable "curFile" as type File earlier, hence the "undefined" error.

I think counting up was also working against me as well.

THANKS SO MUCH!

-Mary.
nab
Posts: 203
Joined: November 29th, 2005, 3:00 am
Location: Royan
Contact:

some additional info...
. getFiles() returns an array of File (or Folder) objects, so no need to redeclare each element as a File.
. you can actually "iterate ++": the number of files in the folder is reduced after each removal but the length of the original array that stores the objects stays the same.

Code: Select all

function removeFiles(theFolder)
{
	var theFiles = theFolder.getFiles("*");
	for (var i = 0; i < theFiles.length; i++)
		theFiles[i].remove();	
}
Post Reply