No internet connection
  1. Home
  2. How to

directoryGetFiles, more search patterns

By samuel henriques @samuel_henriques
    2020-12-01 22:10:51.770Z

    Hello,

    Is there a way to add more options to the searchPattern ?
    If I don't use the correct one, or don't use the line, I won't work, but I wouldn't like to have to change the script every time I get a different file format.

    thank you so much!

          var videoFileFolderPath = sf.file.directoryGetFiles({
                path: folderPath,
                searchPattern: '*.mov',
                isRecursive: true,
            }).path
    
    • 14 replies
    1. Kitch Membery @Kitch2020-12-01 22:58:43.801Z

      Hi Samuel,

      I've not tested it... but you could try using the the logical 'or' operator. Like this;

      var videoFileFolderPath = sf.file.directoryGetFiles({
          path: folderPath,
          searchPattern: '*.mov' || '*.mp4',
          isRecursive: true,
      }).paths
      

      Let me know if that works for you :-)

      1. samuel henriques @samuel_henriques
          2020-12-02 00:14:32.673Z

          nop sorry, && doesn't do it either. uuuh! but thank you.

          1. In reply toKitch:

            do regular expressions work?

            1. Chris Shaw @Chris_Shaw2020-12-02 01:00:22.189Z2020-12-02 01:06:38.487Z

              How about this? (it'll create an array of file paths with matching patterns):

              var videoFileFolderPath
              var patterns = ['*.mov', '*mp4']
              function searchFor(searchPattern) {
                  var searchResult = sf.file.directoryGetFiles({
                      path: folderPath,
                      searchPattern: searchPattern,
                      isRecursive: true,
                  }).paths.join().split(',').slice()
                  return searchResult
              }
              
              var results = patterns.map(item => searchFor(item))
              var matchingFiles = results.join().split(',')
              log (matchingFiles)
              

              Just fill the patterns array with whatever your searching for

          2. Hi Samuel,

            What exactly would you like the script to do?

            In your code, you're using "path" in singular and assigning it to a variable with the name videoFileFolderPath which indicates you're trying to find the path of a folder, not of a file.

            Please note, that the action directoryGetFiles returns an array of file paths. It doesn't return a single path. You're asking it to search for a list of files.

            If you want to search for a pattern of files inside a main folder path, there are several ways to do it.

            The simplest would be to not use a searchPattern, so that it returns all file paths, and then use a filter on the resulting array to use a regular expression to filter on the types of files you want.
            Like this, for example:

            var videoFilePaths = sf.file.directoryGetFiles({
                path: folderPath,
                isRecursive: true,
            }).paths.filter(p => p.match(/\.(mov|mp4|mxf)$/i));
            
            1. samuel henriques @samuel_henriques
                2020-12-02 11:36:09.572Z

                Hello guys, thank you, and @Chris_Shaw, and @Kitch ,
                Yes you are right it's weird and its more a matter of organisation and doing it the proper way, I managed to make it work and now, reviewing this part I find some mistakes, I'm finding the idea might be correct but I might have to do a bit of a clean up.

                The initial idea was that I get to the path with the files, by asking where they are or by default to session vide folder, then take the first in the array, strip the file names and use it later with the path + fileName, I want it to be isRecursive as well. I noticed If I paste the path + fileName to the finder go to folder, I don't need the extension and was hoping for the same behaviour in the pro tools import window.

                I can't try yours and Chris code now, but I will as soon as I can. But I have impression I need to do some housekeeping first instead of trying a quick fix that will give trouble later on.

                thank you so much.

                1. In reply tochrscheuer:
                  samuel henriques @samuel_henriques
                    2020-12-02 22:04:56.943Z

                    This works perfectly!!

                    Thank you so much guys

                    1. In reply tochrscheuer:

                      damn. takes my 13 lines of code and does in 4!
                      So much to learn…

                      1. In reply tochrscheuer:

                        Just out of curiosity, @chrscheuer , how would you do an inverse match? ie: all files that don't end with mov|mp4|mxf?

                        1. Nevermind I found it:
                          paths.filter(p => p.match(/^(.(?!(\.(ptx)$)))*$/i));

                          1. I would have just reversed the match result:

                            var videoFilePaths = sf.file.directoryGetFiles({
                                path: folderPath,
                                isRecursive: true,
                            }).paths.filter(p => !p.match(/\.(mov|mp4|mxf)$/i));
                            
                            1. Chris Shaw @Chris_Shaw2020-12-04 21:23:09.077Z2020-12-04 21:34:52.075Z

                              Argh! that's the only place I didn't think to put ! !!!!!!!!

                              1. Leave it to me to find the long way around…
                                🙃

                        2. In reply tochrscheuer:
                          Kitch Membery @Kitch2022-06-15 20:58:37.896Z

                          Hi @Owen_Granich_Young,

                          You were asking in the webinar today how to collect different video file types with a recursive search. See Christians post above on how to do that;

                          let videoFilePaths = sf.file.directoryGetFiles({
                              path: folderPath,
                              isRecursive: true,
                          }).paths.filter(p => p.match(/\.(mov|mp4|mxf)$/i));Ï
                          

                          T'was great to see you! Rock on!