General file name evaluating functions

Help with writing and running scripts

General file name evaluating functions

Postby Mofi » Sat Oct 25, 2008 12:13 pm

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.
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 scripts section of the Extra Downloads page.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: General file name evaluating functions

Postby Mofi » Wed Feb 11, 2009 11:15 am

The script file with the 4 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:

  1. The type of a variable is always visible which is a great help.
  2. 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.
  3. 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.
  4. 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
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: General file name evaluating functions

Postby Mofi » Tue Jan 19, 2010 11:38 am

The script file with the 4 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 funtions now also with these new optional parameters.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts