Page 1 of 1
Deleting Files from Disk via scripting
Posted: September 27th, 2007, 9:01 pm
by pxlgirl
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.
Re: Deleting Files from Disk via scripting
Posted: September 27th, 2007, 10:57 pm
by redefinery
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
Posted: October 1st, 2007, 2:58 pm
by pxlgirl
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?
Posted: October 1st, 2007, 3:02 pm
by bradshaw1965
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
Posted: October 1st, 2007, 3:13 pm
by pxlgirl
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.
Posted: October 1st, 2007, 5:19 pm
by nab
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();
}