No internet connection
  1. Home
  2. How to

How to use sf.file to get list of files in a known Directory

By Iain Anderson @Iain_Anderson
    2021-09-29 16:46:19.082Z

    Hi there, just recently stumbled on the code sf.file. Have looked at the list of possible functions but not sure how to use sf.file correctly.

    I'm looking to read the contents of a folder on my computer and add the file names to a list.

    Are either of these the correct starting point?

    sf.file.directoryGetEntries():
    
    sf.file.directoryGetFiles();
    

    Any light you can shed @chrscheuer would be amazing!

    Many thanks,

    Iain

    • 7 replies
    1. directoryGetEntries will get you both files and directories in the path you're looking under. directoryGetFiles will only get you files.

      Example use case:

      
      let textFilePaths = sf.file.directoryGetFiles({
          path: `~/Desktop`,
          searchPattern: '*.txt',
      }).paths;
      
      for(let path of Array.from(textFilePaths))
      {
          log(`Processing file "${path}"...`);
      }
      
      log('Done');
      
      1. Set the isRecursive property to true to search through all child folders as well

        
        let textFilePaths = sf.file.directoryGetFiles({
            path: `~/Desktop`,
            searchPattern: '*.txt',
            isRecursive: true,
        }).paths;
        
        for (let path of Array.from(textFilePaths)) {
            log(`Processing file "${path}"...`);
        }
        
        log('Done');
        
        1. Another example – getting the newest file in a folder:

          
          let newestPathOnDesktop = sf.file.directoryGetFiles({
              path: `~/Desktop`,
              searchPattern: '*.txt',
              sortOrder: 'LastWriteTimeDesc',
          }).paths[0];
          
          log(newestPathOnDesktop);
          
          1. AAlex Oldroyd @Alex_Oldroyd8
              2023-10-22 10:22:50.802Z

              hi christian. When i run this script, it just logs 'Done'. I tried adding

              log(textFilePaths)
              

              but this doesn't seem to work either

              1. Hi Alex

                That script looks for txt files on your Desktop. If you need help with a script doing something else, please use Script Help so we have a full view of what you're trying to do.

                https://soundflow.org/docs/help#script-help

        2. In reply toIain_Anderson:
          Randy Brown @Randy_Brown
            2024-12-08 23:00:14.348Z

            Hi guys! Any way to amend that function to take in the “selected” files on the finder? I’d like to generate an array of these file names.

            Thanks!
            Randy

            1. Yes, you can do that like so:

              
              var selectedFilePaths = Array.from(sf.ui.finder.selectedPaths);
              
              for(let filePath of selectedFilePaths)
              {
                  log(filePath);
              
                  //Do something with this file here...
              }