The first thing we need to do is to create a folder object. One of the simplest ways of doing this is to use the “create” method and to place the new folder object into a variable.
var fldSource = new Folder(”c/indesign/projects/barkertruman”);
The above syntax can be used to create a folder objects that points to an existing folder or to one that has not yet been created. If the folder does not yet exist, we can create it using the create method.
var blnSuccess = fldSource.create();
If the folder is successfully created, our Boolean variable blnSuccess will contain true. If the attempt to create the folder fails, it will contain false.
If our folder object points to an existing folder, we might want to examine the contents of the folder. Let’s say, for example, we want to check for the existence of an InDesign file called “log.txt”. The getFiles method creates an array containing the names of all the files in the folder and, its optional argument (mask) allows you to specify the file extension of the files you want to return.
var arrFiles = fldSource.getFiles(”*.indd”);
We can then test, within a for loop whether the name of any of the files is “log.txt”.
if(arrSource[i].name == “march.txt”)
blnExists = true;
In this example, we are setting the value of a Boolean variable to true if the file is found.
Working with the file object is very similar to working with the folder object. Firstly, you need to create the object and assign a file to the object by specifying its path.
var myFile = new file(”c/indesign/projects/barkertruman/log.txt”);
The file object is often used to create and manipulate text files during such operations as keeping a log of activities and reading settings. The methods available with the file object include open, read, write and seek. The open method opens the file for read/write operations. The read method reads the contents of the file starting at the current position. The optional argument specifies the number of characters to read. Similarly, the write method can be used to write to the file starting at the current position. The seek method is used to reset the position within the file.