Tapatalk

General file name evaluating functions

General file name evaluating functions

6,685587
Grand MasterGrand Master
6,685587

    Oct 25, 2008#1

    Hello script writers!

    I have written a script with some general functions working with the full name of a file for the usage in other scripts:

    GetFileExt ........... get file extension from full name of a file.
    GetFileName ....... get file name without extension from full name of a file.
    GetFilePath ......... get file path from full name of a file.
    GetFolderName ... get name of folder a file is stored from full file name.
    GetNameOfFile .... get file name with extension from full name of a file.

    For all functions either the index number of an opened document or the full name of a file can be specified as an input parameter. The functions can be even used without any parameter to get the appropriate part of the full file name of the active document. The functions use only core string and UltraEdit functions. So nothing of the environment (running mode, find parameters, ...) is changed by these functions.

    The script file contains also a few examples at bottom of the file. So the script file can be also simply executed to see the functions in action.

    If you want to report mistakes or have suggestions for further enhancements or other useful file name functions post a message here.

    The script file FileNameFunctions.js with the code can be viewed or downloaded from the Macros & Scripts page.

    The line and block comments can be removed from the five functions by running a replace all (from top of file) searching with Perl regular expression for ^ *//.+[\r\n]+|^ */\*[\s\S]+?\*/[\r\n]+| +//.+$ and using an empty replace string. The first part in this OR expression with three arguments matches entire lines containing only a line comment, the second part matches block comments, and third part matches line comments right to code. Removal of the comments makes the usage of these functions in other scripts more efficient because of JavaScript interpreter has to interpret less characters and lines.

      Feb 11, 2009#2

      The script file with the file name functions was updated on 2009-02-10.

      There are no real changes on the code. I have just changed slightly all variable names by adding a prefix letter for the type of the variable. Advantages of using a type prefix letter:
      • The type of a variable is always visible which is a great help.
      • No problem anymore with variable names similar common words used in comments or strings, especially for searching/replacing such variables. For example sDirectory as variable name is much better than just Directory because word Directory can also exist in comments and strings.
      • It is easy to search for all string, number or boolean variables if using a type prefix letter (always lowercase) and the variable name starts with an uppercase character. For example the case sensitive regular expression search string s[A-Z][A-Za-z]+ with option Match Whole Word Only finds all string variables in the script file.
      • Using type prefix letters makes the selection of an existing variable of type string, number or boolean easier in the auto-complete dialog.
      Additionally the global variable used is now named g_nDebugMessage. g_ as additional prefix defines that this variable is a global variable not defined inside the function. Variables with a non standard type don't have a prefix, but have a very special name like CompleteFileNameOrDocIndexNumber.

      The standard prefixes I use are:

      an ... array of numbers (doesn't exist in FileNameFunctions.js)
      as ... array of strings (doesn't exist in FileNameFunctions.js)
      b ... boolean
      n ... number
      s ... string

        Jan 19, 2010#3

        The script file with the file name functions was updated on 2010-01-16.

        Function GetFileExt has now a second parameter named bWithPoint which is optional. The second parameter is of type boolean as prefix b already indicates. It can have the values false or true or is simply not used (=false). If GetFileExt is called with value true for the second parameter the function returns the file extension with a point, i.e. it returns for example ".htm" instead of just "htm" for file "C:\Temp\index.htm".

        Function GetFileName has now also a second parameter named bWithPoint which is also optional and of type boolean. If GetFileName is called with value true for the second parameter the function returns the file name with a point appended, i.e. it returns for example "index." instead of just "index" for file "C:\Temp\index.htm".

        And function GetFileName has now additionally a third parameter named bWithPath which is also optional and of type boolean. If this third parameter is used and has the value true the function returns the file name with the path, i.e. it returns for example "C:\Temp\index" instead of just "index" for file "C:\Temp\index.htm" with second parameter has value false or "C:\Temp\index." with second parameter has value true.

        These modifications are 100% downwards compatible. Nothing must be changed in existing scripts when replacing existing GetFileExt or GetFileName functions by the updated versions.

        GetFilePath and GetNameOfFile are not modified.

        Furthermore I updated also the demonstration code at bottom of the script file to show the usage of the functions now also with these new optional parameters.

        691
        Advanced UserAdvanced User
        691

          Graphical layout

          Nov 03, 2019#4

          I had a question about the layout of these scripts which I think I have answered by drawing up this screenshot below.
          It seemed a waste once I had drawn it up so have posted.
          If it is incorrect please let me know and I will correct accordingly.
          Anyway the 5 functions are shown and the "Demonstration code" appears thereafter.
          I presume this is correct?
          filename_functions_graphical_layout.png (157.99KiB)
          Graphical layout of the file name evaluating functions with demonstration code below the functions.

          6,685587
          Grand MasterGrand Master
          6,685587

            Nov 03, 2019#5

            The graphic is correct of course. You have found out by using code folding and command to collapse all folds why I wrote the published UltraEdit scripts with the format as it can be seen on looking on code of the script files. A well-formatted and commented source code file is wonderful to get an overview, isn't it.
            Best regards from an UC/UE/UES for Windows user from Austria

            691
            Advanced UserAdvanced User
            691

              Nov 03, 2019#6

              Wonderful indeed Mofi
              It allowed me to answer my own question then share my pretty picture   :)

              6,685587
              Grand MasterGrand Master
              6,685587

                Jun 22, 2022#7

                The script file with the file name functions was updated on 2022-05-29 by slightly improving the code of all functions to handle better a string like: C:\Test\Folder Name\/\

                Such a folder path is in real an invalid folder path on Windows, but is handled nevertheless correct on Windows because of the automatic corrections applied by the Windows file I/O functions as described on the Microsoft documentation page about Naming Files, Paths, and Namespaces. The folder path is corrected to C:\Test\Folder Name before passing it to the file system of drive C: by replacing / by \ and then replacing a series of \ by a single \ except at beginning of a fie/folder string starting with two backslashes (UNC path).

                The improved functions do not correct such a string. They just interpret the string as invalid file name string and return all an empty string as it was done before the improvement for a string like C:\Test\Folder Name\\ too. The functions are not coded intentionally to handle all possible invalid file/folder strings and correct them as done by the Windows file I/O functions to keep them efficient on execution. A script must have appropriate code to correct such file/folder strings before making use of the file name functions if the script should support such syntactically not really valid file/folder strings.

                The demonstration code and some copyright information were updated on 2022-06-22 in the script file, too.
                Best regards from an UC/UE/UES for Windows user from Austria