Get partial file path from an array of file names

Get partial file path from an array of file names

15
Basic UserBasic User
15

    May 06, 2021#1

    I have a script that contains an array of file names with the full UNC path.  For the first file only, I need to get a partial path out of it.  The paths will have all a similar format:
    ...\CCN 6241\Install\Back_End...
    I need to get the UNC path up through the "Install" folder, if it exists, otherwise just up through the "CCN 9999" folder (the number part of the folder name will vary, but will always be a 4-digit number).

    I can get the value either in the loop as the filenames are added to the array, or after the loop when I start writing to a new file window.

    Any help you can provide would be greatly appreciated!

    Thanks,
    Dan

    6,676579
    Grand MasterGrand Master
    6,676579

      May 07, 2021#2

      Look on this simple example script:

      Code: Select all

      var asFullFileNames = [
         "\\\\server\\share\\folder\\CCN 6241\\Install\\Back_End\\FileName1.txt",
         "\\\\server\\share\\folder\\CCN 9999\\Back_End\\FileName2.txt",
         "\\\\server\\share\\folder\\another folder\\FileName3.txt"
      ];
      
      var sFilePath = asFullFileNames[0].replace(/^(.*\\(?:Install|CCN \d\d\d\d)\\).*$/i,"$1");
      var sCCN = sFilePath.replace(/^.*CCN (\d+).*$/i,"$1");
      UltraEdit.outputWindow.write("Path of first file: " + sFilePath);
      UltraEdit.outputWindow.write("CCN number of file: " + sCCN);
      
      
      var nFileNameIndex = 0;
      while (nFileNameIndex < asFullFileNames.length)
      {
         sFilePath = asFullFileNames[nFileNameIndex].replace(/^(.*\\(?:Install|CCN \d\d\d\d)\\).*$/i,"$1");
         nFileNameIndex++;
         UltraEdit.outputWindow.write("Path of file " + nFileNameIndex + ": " + sFilePath);
      }
      
      if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
      
      The output into the output window is:

      Code: Select all

      Path of first file: \\server\share\folder\CCN 6241\Install\
      CCN number of file: 6241
      Path of file 1: \\server\share\folder\CCN 6241\Install\
      Path of file 2: \\server\share\folder\CCN 9999\
      Path of file 3: \\server\share\folder\another folder\FileName3.txt
      
      It can be seen on last file name that the replace method of the JavaScript String object returns the string as is if the regular expression pattern does not match a string.
      Best regards from an UC/UE/UES for Windows user from Austria

      15
      Basic UserBasic User
      15

        May 07, 2021#3

        Perfect, thanks!

        One more thing: how would you also extract out the CCN number (just the number part) from the path into a separate variable?

        Thanks so much.

          May 07, 2021#4

          With enough searching, I eventually found my own answer:

          "asFilePath" contains, as you might expect, the file path.  To get the CCN number, I did this:

          Code: Select all

           asCCN = asFilePath.match(/\b\d{4}\b/g);
          Thanks!

          6,676579
          Grand MasterGrand Master
          6,676579

            May 08, 2021#5

            The match method could be used as demonstrated by you, but I think it is again better to use the replace method as wanted is only the CCN number and not an array of numbers with four digits found anywhere in the file path. The replace is more safe and a bit faster than the match method as used by you. I updated my script code to get and output also the CCN number of first file name.
            Best regards from an UC/UE/UES for Windows user from Austria

            15
            Basic UserBasic User
            15

              May 10, 2021#6

              Thanks, Mofi.  That works great, and I have updated my script to use replace rather than match.