Skip navigation
jtcdesigns1
Currently Being Moderated

Finding file within directory

Jun 8, 2012 5:51 AM

I'm currently using InDesign CS5.5

 

I have an automation script I wrote for datamerge in javascript but I've had trouble finding any references to searching for files with javascript. I'm well aware you can't in javascript for the web but there is server side scripting but can't find where any tutorials are online.

 

What im trying to acheive is this line here... but in javascript.

 

 

var rootName = app.doScript('set theFile to (do shell script "ls /Path/To/Folder")', ScriptLanguage.APPLESCRIPT_LANGUAGE);

 

now to explain why im using this incase anyone wants this or wants to know... this will give me a list of filenames in a directory..

I then apply..

 

var filename = rootName.split('.').reverse().slice(1).reverse().join('.');

 

which strips everything but the name of  the file without the extension.

 

Part of my program is.. each csv file that comes in will not have the same name so I use this to get the name and then later apply it to my datasource and for saving out the PDF.

 

I'd appreciate if anyone can help me acheive looking for a file within javascript or if they could send me a link to where some documentation is.

 

I have tried using getFiles("*.csv") but I get errors.. when I slap an alert after getting the file it shows the file with the entire path and I can't use slice because it doesn't do anything. literally it doesn't slice. This is why I was using applescript. I'm not sure if its more efficient or not to use Javascript for this or Applescript just would like to keep it all one language.

 

Eventually I'd like to figure out how to set up a for function or some sort of count to grab only 1 file at a time in the case that 2 files make it to the directory.

 

Also if anyone wants the code to do a datamerge and save out to pdf I'll gladly post.

 

Thanks

 
Replies
  • Currently Being Moderated
    Jun 8, 2012 6:08 AM   in reply to jtcdesigns1

    jtcdesigns1 wrote:

     

    I have tried using getFiles("*.csv") but I get errors.. when I slap an alert after getting the file it shows the file with the entire path and I can't use slice because it doesn't do anything. literally it doesn't slice

     

    I think the problem lies in what getFiles is returning. This is not a single file, but it's always an array -- even when there are no files found (in which case the length of the array is 0). Your slicing-and-dicing does not work on an array, hence the error. Your alert, on the other hand, does know how to display an array -- it shows all members, comma separated, but if there is only one member, you cannot tell from the alert text if it was an array or not.

     

    Eventually I'd like to figure out how to set up a for function or some sort of count to grab only 1 file at a time in the case that 2 files make it to the directory.

     

    Yep, doable. Use getFiles with your file mask and check the length of the returned array. If it's zero, there are no files. If it's 1, you can use item[0] of the returned array for your file name; if it's more than one, you can show your select-a-file dialog.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 7:19 AM   in reply to jtcdesigns1

    jtcdesigns1 wrote:

     

    I'm better at modifying than creating

     

    Modify this to taste

     

    filename = fetch_me_a_file ("*.csv");
    if (filename == null)
    alert ("No file found, or user canceled");
    else
    alert (filename);
    function fetch_me_a_file (mask)
    {
    var fileList = Folder.myDocuments.getFiles(mask);
    if (fileList.length == 0)
    {
      alert ("No files found");
      return null;
    }
    if (fileList.length == 1)
      return fileList[0].name;
    var selectedFile = Folder.myDocuments.openDlg("Fetch me a file", mask);
    // Canceled?
    if (selectedFile == null)
      return null;
      
    return selectedFile.name;
    }
    

     

    I was wondering if and how your Split Off Filename line worked, but decided to use this RegExp instead:

     

    name = filename.replace(/^.+\//, '');
    

     

    (replacing everything before and including the final forward slash with nothing) but then I thought, wait a minute. The 'object' returned by both getFiles and by openDlg is not actually a file name -- even though JS conveniently translates it to a fully qualified path when asked --, it's a file object. And a regular File object has this useful property 'name': that's just the file name.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points