Sage-ST ä

Fileutil

Documentation

Global Declarations (Constants, Types, Variables)
CheckFileModifyStatus ConvertToFullPath FilenameExtensionIs
FilesBinaryEqual FilesTextEqual GetRunInfo GetUniqueFilename
JoinPath LocateFile PrinterName ReplaceExtension
ReplacePrefix ReplacePrefixAndExtension SetToExePath SplitPath
TypeOfFileName





  TYPE
    FileNameType = (invalidName,
                    singleName,
                    wildName);




  PROCEDURE TypeOfFileName
             (CONST FileName : ARRAY OF CHAR) : FileNameType;

  (**
      TypeOfFileName - Checks a file name and returns general type info about it.


      This procedure checks the file name that is passed in.  It returns one of
      three values based on what it finds.  It can return invalidName which would
      indicate that invalid elements were found.  It can return wildName indicating
      that wild card characters are present in the name.  The last value it can return
      is singleName which would indicate a valid single file name is contained in the
      string.

      CALLING SEQUENCE -

        TypeOfFileName (FileName);

      ENTRY -

        FileName : ARRAY OF CHAR
          The file name to be checked.

      EXIT -

        FileNameType
          The type that this name appears to be.  Can be invalidName, singleName or
          wildName.
  *)




  PROCEDURE PrinterName
             (CONST TestName : ARRAY OF CHAR) : BOOLEAN;

  (**
      PrinterName - Returns boolean as to whether this appears to be a port name.


      This procedure checks the name that was passed in and returns a boolean that
      shows if this appears to be a printer port name such as LPT1, LPT2, etc.

      CALLING SEQUENCE -

        PrinterName (TestName);

      ENTRY -

        TestName : ARRAY OF CHAR
          The name to be checked.

      EXIT -

        BOOLEAN
          TRUE - The name does appear to be a port name such as LPT1, LPT2, etc.
          FALSE - The name is not a printer port name.
  *)




  PROCEDURE ConvertToFullPath
             (CONST CurrentName  : ARRAY OF CHAR;
              VAR   FullPathName : ARRAY OF CHAR);

  (**
      ConvertToFullPath - Converts a given path and/or filename to its full path.


      This procedure converts an input path and/or file name into the full path
      equivalent based on the current directory.  Uses for this routine include
      converting relative names (begins with .\ or ..\ or \ or nextdir\myfile.doc)
      into the full name.  For instance if the current directory is E:\Dir1\Dir2
      and you pass in ..\Testdir\Myfile.txt the return value would be
      E:\Dir1\Testdir\Myfile.txt.  Or given the same current directory and input
      of nextdir\Myfile.out the output value would be E:\Dir1\Dir2\nextdir\Myfile.out.

      The output value is always based on the current directory of the application.

      CALLING SEQUENCE -

        ConvertToFullPath (CurrentName, FullPathName);

      ENTRY -

        CurrentName : ARRAY OF CHAR
          The current value for a file and/or path.  Can include relative references.

      EXIT -

        FullPathName : ARRAY OF CHAR
          The full path including the drive, directory and filename if included in the
          input value.  All relative directory references at the start of the CurrentName
          parameter will have been replaced with their explicit equivalents.
  *)




  PROCEDURE JoinPath
             (VAR   Full      : ARRAY OF CHAR;
              CONST Drive     : ARRAY OF CHAR;
              CONST Directory : ARRAY OF CHAR;
              CONST FileName  : ARRAY OF CHAR;
              CONST Extension : ARRAY OF CHAR);

  (**
      JoinPath - Build a complete path from subcomponents.


      This will build a complete path + filename identifier from a Drive,
      Directory, Filename AND File Extension.

      CALLING SEQUENCE -

        JoinPath (MyFile, "D:", "\TEST\TEMP\", "TEST", ".TXT");

      ENTRY -

        Drive : DriveType
          The drive designator i.e. "D:"

        Directory : DirectoryType
          The directory name

        FileName : NameType
          The file name (up to eight CHARs)

        Extension : NameType
          The file name extension including the . i.e. .TXT

      EXIT -

        Full : FullType
          The concatenated full file name IMPORT path AND drive
  *)




  PROCEDURE SplitPath
             (CONST Full      : ARRAY OF CHAR;
              VAR   Drive     : ARRAY OF CHAR;
              VAR   Directory : ARRAY OF CHAR;
              VAR   FileName  : ARRAY OF CHAR;
              VAR   Extension : ARRAY OF CHAR);

  (**
      SplitPath - Split a complete path into subcomponents.


      This will split a complete path + filename identifier into a Drive,
      Directory, Filename AND File Extension.

      CALLING SEQUENCE -

        SplitPath ("D:\TEST\TEMP\TEST.TXT", Drive, Directory, Name,
                   Extension);

      ENTRY -

        Full : FullType
          The concatenated full file name IMPORT path AND drive

      EXIT -

        Drive : DriveType
          The drive designator i.e. "D:"

        Directory : DirectoryType
          The directory name

        FileName : NameType
          The file name (up to eight CHARs)

        Extension : NameType
          The file name extension including the . i.e. .TXT
  *)




  PROCEDURE GetRunInfo
             (VAR   FullCommandLine  : ARRAY OF CHAR;
              VAR   AppRunPathOnly   : ARRAY OF CHAR;
              VAR   AppNameOnly      : ARRAY OF CHAR;
              VAR   AppFullRunName   : ARRAY OF CHAR;
              VAR   ParamsOnly       : ARRAY OF CHAR;
              VAR   CurrentDirectory : ARRAY OF CHAR;
              VAR   Success          : BOOLEAN);

  (**
      GetRunInfo - Gets the application run information such as start directory, etc.


      This procedure gets the run information for the current application.  This
      includes such things as the directory that the .EXE is in, the commmand line
      parameters and the current directory.  This is very usefull when trying to find
      other files associated with the .EXE that you know should be in that same directory.

      NOTE - If relative values were used to start the application and the current
      directory has been changed since that time this routine will not correctly interpret
      the relative path.  Consequently the run path will be wrong.  This routine should be
      called before the current directory is changed if that may occur.

      CALLING SEQUENCE -

        GetRunInfo (FullCommandLine, AppRunPathOnly, AppNameOnly, AppFullRunName,
                    ParamsOnly, CurrentDirectory, Success);

      ENTRY -

        None
          N/A

      EXIT -

        FullCommandLine : ARRAY OF CHAR
          The complete command line for the app including the full path to .EXE, the .EXE
          name and any commmand line arguments.

        AppRunPathOnly : ARRAY OF CHAR
          The run path of the app only, ending in a backslash.

        AppNameOnly : ARRAY OF CHAR
          The .EXE name only in the form of APPNAME.EXE.

        AppFullRunName : ARRAY OF CHAR
          The path and name of the application.  This is basically the concatination of
          AppRunPathOnly and AppNameOnly.

        ParamsOnly : ARRAY OF CHAR
          Any parameters to the app that were sent on the command line.

        CurrentDirectory : ARRAY OF CHAR
          The current directory that the application is using.  In other words this is
          the current directory as far as reading/writing files is concerned if no path
          is used as part of their name.

        Success : BOOLEAN
          TRUE - The information was successfully obtained.
          FALSE - Some error prevented the information to be collected.

      EXAMPLE -

        Assume that 1) the .EXE file is located in D:\MyApp\ExeDir 2) while running
        the current directory has been changed to C:\Dir1\Dir2 and 3) the application
        was started with the command "D:\MyApp\ExeDir\MyApp.EXE VAL1 VAL2" the call
        would return the following values:

        GetRunInfo (FullCommandLine, AppRunPathOnly, AppNameOnly, AppFullRunName,
                    ParamsOnly, CurrentDirectory, Success);

        FullCommandLine  = "D:\MyApp\ExeDir\MyApp.EXE VAL1 VAL2"
        AppRunPathOnly   = "D:\MyApp\ExeDir\"
        AppNameOnly      = "MyApp.EXE"
        AppFullRunName   = "D:\MyApp\ExeDir\MyApp.EXE"
        ParamsOnly       = "VAL1 VAL2"
        CurrentDirectory = "C:\Dir1\Dir2\"

        If relative paths are used to start the app they are interpreted before returning
        the values.  However read the NOTE above for more information on that.  In the
        second example assume that the command line was "ExeDir\MyApp.EXE VAL1 VAL2" and
        that the current directory has NOT been changed (current directory starts as
        "D:\MyApp").  In this case the routine can correctly interpret the relative path
        and will return the correct information.  The following values will be returned:

        GetRunInfo (FullCommandLine, AppRunPathOnly, AppNameOnly, AppFullRunName,
                    ParamsOnly, CurrentDirectory, Success);

        FullCommandLine  = "D:\MyApp\ExeDir\MyApp.EXE VAL1 VAL2"
        AppRunPathOnly   = "D:\MyApp\ExeDir\"
        AppNameOnly      = "MyApp.EXE"
        AppFullRunName   = "D:\MyApp\ExeDir\MyApp.EXE"
        ParamsOnly       = "VAL1 VAL2"
        CurrentDirectory = "D:\MyApp\"
  *)




  PROCEDURE ReplaceExtension
             (CONST OldName      : ARRAY OF CHAR;
              CONST NewExtension : ARRAY OF CHAR;
              VAR   NewName      : ARRAY OF CHAR);

  (**
      ReplaceExtension - Replaces the current extension on the filename with a new one.


      This procedure allows for quickly replacing the current file extension on the
      passed in file name with a new one.

      CALLING SEQUENCE -

        ReplaceExtension (OldName, NewExtension, NewName);

      ENTRY -

        OldName : ARRAY OF CHAR
          The file name before the it is replaced.  May include a path.

        NewExtension : ARRAY OF CHAR
          The new extension to put on.

      EXIT -

        NewName : ARRAY OF CHAR
          The file name with any path info intact but the extension replaced.

      EXAMPLE -

        FullName := "C:\Dir1\Dir2\Test.EXE";

        ReplaceExtension (FullName,
                          ".DFL",
                          FullName);

        FullName returns as "C:\Dir1\Dir2\Test.DFL"
  *)




  PROCEDURE ReplacePrefix
             (CONST OldName   : ARRAY OF CHAR;
              CONST NewPrefix : ARRAY OF CHAR;
              VAR   NewName   : ARRAY OF CHAR);

  (**
      ReplacePrefix - Replaces the current filename prefix with a new one with same extension.


      This procedure allows for quickly replacing the current filename on the
      passed in name with a new one without disturbing the current path or
      extension.

      CALLING SEQUENCE -

        ReplacePrefix (OldName, NewPrefix, NewName);

      ENTRY -

        OldName : ARRAY OF CHAR
          The file name before the it is replaced.  May include a path and extension.

        NewPrefix : ARRAY OF CHAR
          The new file name prefix to put on.  For convenience any path on this name
          may be included and will be put on the new name.

      EXIT -

        NewName : ARRAY OF CHAR
          The file name with any path and extension info intact but the name replaced.

      EXAMPLE -

        FullName := "C:\Dir1\Dir2\Test.EXE";

        ReplacePrefix (FullName,
                       "MyTest",
                       FullName);

        FullName returns as "C:\Dir1\Dir2\MyTest.EXE"
  *)




  PROCEDURE ReplacePrefixAndExtension
             (CONST OldName         : ARRAY OF CHAR;
              CONST ReplacementName : ARRAY OF CHAR;
              VAR   NewName         : ARRAY OF CHAR);

  (**
      ReplacePrefixAndExtension - Replaces the name and extension with or without path.


      This procedure allows for quickly replacing the current filename and extension
      with a new name.  The new name may or may not include an extension.  If no
      extension is give there will not be one on the resulting name.  The main
      purpose of this routine is to handle names that may or may not include the
      path.  This way you do not have to  be concerned with finding just the
      filename part before replacing.

      CALLING SEQUENCE -

        ReplacePrefixAndExtension (OldName, ReplacementName, NewName);

      ENTRY -

        OldName : ARRAY OF CHAR
          The file name before the extension is replaced.

        ReplacementName : ARRAY OF CHAR
          The new file name with or without extension that will replace the old one.

      EXIT -

        NewName : ARRAY OF CHAR
          The file name with any path info intact but the name replaced.

      EXAMPLE -

        FullName := "C:\Dir1\Dir2\Test.EXE";

        ReplaceNameAndExtension (FullName,
                                 "MyNewTest.EXE",
                                 FullName);

        FullName returns as "C:\Dir1\Dir2\MyNewTest.EXE"
  *)




  PROCEDURE LocateFile
             (CONST SearchName   : ARRAY OF CHAR;
              VAR   FullFileName : ARRAY OF CHAR;
              VAR   Success      : BOOLEAN);

  (**
      LocateFile - Searchs for a file and returns the full path if found.


      This procedure searches for a file when its location may not be known
      for certain.  First it searches the current directory, then if the
      file is not found it checks the directory from where the application
      executable was started.  In addition if the application is Win32 it will
      continue on and check the search path.

      CALLING SEQUENCE -

        LocateFile (SearchName, FullFileName, Success);

      ENTRY -

        SearchName : ARRAY OF CHAR
          The file name to search for.

      EXIT -

        FullFileName : ARRAY OF CHAR
          The full path and name of the file if it was located.

        Success : BOOLEAN
          TRUE - the file was located.
          FALSE - the file was not found in the areas checked.

      EXAMPLE -

        LocateFile ("TEST.OUT",
                    FullFileName,
                    Success);
  *)




  PROCEDURE CheckFileModifyStatus
             (CONST FileName  : ARRAY OF CHAR;
              VAR   Found     : BOOLEAN;
              VAR   CanModify : BOOLEAN);

  (**
      CheckFileModifyStatus - Checks a file to see if it is read/write or read/only.


      This procedure checks a file and if found returns a value to indicate
      if the file can be modified.

      CALLING SEQUENCE -

        CheckFileModifyStatus (FileName, Found, CanModify);

      ENTRY -

        FileName : ARRAY OF CHAR
          The file name to search for.

      EXIT -

        Found : BOOLEAN
          Indicates if the file was located.

        CanModify : BOOLEAN
          TRUE - the file can be changed (attribute is NOT read only).
          FALSE - the file CANNOT be changed (attibute is read only).

      EXAMPLE -

        CheckFileModifyStatus ("TEST.OUT",
                               Found,
                               CanModify);

        IF Found THEN
          Files.Open (FileHandle,
                      "TEST.OUT",
                      Files.binMode,
                      Files.readOnly,
                      State);
  *)




  PROCEDURE FilenameExtensionIs
             (CONST FileName  : ARRAY OF CHAR;
              CONST Extension : ARRAY OF CHAR) : BOOLEAN;

  (**
      FilenameExtensionIs - Do NON case sensitive compare of the file extension.


      This procedure does a non case sensitive comparison of the extension on the
      file name and the given extension and returns a boolean result of the
      comparison.

      CALLING SEQUENCE -

        FilenameExtensionIs (FileName, Extension);

      ENTRY -

        FileName : ARRAY OF CHAR
          The file name whose extension will be checked.

        Extension : ARRAY OF CHAR
          The extension to search for.  May or may not include the '.' character.

      EXIT -

        BOOLEAN
          TRUE - The file name does have that extension.
          FALSE - The file name does NOT have that extension.

      EXAMPLE -

        IF (FilenameExtensionIs ("C:\MyDir\TEST.res",
                                 ".RES")) THEN
          Will return TRUE

        or

        IF (FilenameExtensionIs ("C:\MyDir\TEST.res",
                                 "RES")) THEN
          Will return TRUE
  *)




  PROCEDURE FilesTextEqual
             (CONST SourceName1   : ARRAY OF CHAR;
              CONST SourceName2   : ARRAY OF CHAR;
              VAR   SameText      : BOOLEAN;
              VAR   FirstDiffLine : CARDINAL);

  (**
      FilesTextEqual - Text comparison of files for equality.


      This procedure does a case sensitive text comparison of two files.
      This is a faster way to compare but only works for text.  If either file
      does not exist or the files are different size or any part of the text
      differs from the other file the SameText value returns as False.  Even though
      this is doing a text comparison the files must be of the same size or the
      routine will return False that the files are not the same.

      CALLING SEQUENCE -

        FileTextEqual (SourceName1, SourceName2, SameText, FirstDiffLine);

      ENTRY -

        SourceName1 : ARRAY OF CHAR
          The file name of one of the files to compare.

        SourceName2 : ARRAY OF CHAR
          The file name of other file to compare.

      EXIT -

        SameText : BOOLEAN
          TRUE - The files are identical.
          FALSE - The files differ in size or content.

        FirstDiffLine : CARDINAL
          The line number where the first difference was detected. A return
          value of 0 can indicate that one of the files did not exist or
          there was a problem opening one of the files.

      EXAMPLE -

        FileUtil.FilesTextEqual ("FirstFile.txt",
                                 "SecondFile.txt",
                                 TextEqual,
                                 FirstDiffLine);
  *)




  PROCEDURE FilesBinaryEqual
             (CONST SourceName1   : ARRAY OF CHAR;
              CONST SourceName2   : ARRAY OF CHAR;
              VAR   SameBinary    : BOOLEAN;
              VAR   FirstDiffByte : CARDINAL);

  (**
      FilesBinaryEqual - Binary comparison of files for equality.


      This procedure does a binary (byte by byte) comparison of two files.
      This is a slower way to compare but will work for any type of file.
      If either file does not exist or the files are different size or any
      byte in the file differs from the other file the SameBinary value
      returns as False.

      CALLING SEQUENCE -

        FileBinaryEqual (SourceName1, SourceName2, SameBinary, FirstDiffByte);

      ENTRY -

        SourceName1 : ARRAY OF CHAR
          The file name of one of the files to compare.

        SourceName2 : ARRAY OF CHAR
          The file name of other file to compare.

      EXIT -

        SameBinary : BOOLEAN
          TRUE - The files are identical.
          FALSE - The files differ in size or content.

        FirstDiffByte : CARDINAL
          The byte number where the first difference was detected. A return
          value of 0 can indicate that one of the files did not exist or
          there was a problem opening one of the files.

      EXAMPLE -

        FileUtil.FilesBinaryEqual ("FirstFile.txt",
                                   "SecondFile.txt",
                                   TextBinary,
                                   FirstDiffByte);
  *)




  PROCEDURE GetUniqueFilename
             (CONST baseName          : ARRAY OF CHAR;
              CONST AppendToPrefix    : BOOLEAN;
              CONST PrefixDigits      : CARDINAL;
              CONST AppendToExtension : BOOLEAN;
              CONST ExtensionDigits   : CARDINAL;
              VAR   UniqueName        : ARRAY OF CHAR);

  (**
      GetUniqueFilename - Get a unique (unused) filename.


      This procedure generates a unique name that is currently not used.
      You have the option of appending a numeric value onto the prefix,
      the extension or both parts in order to make a unique name.
      The unique name will be created using numbers for uniqueness.  Note
      that the return name will be fully qualified with a path.

      CALLING SEQUENCE -

        GetUniqueFilename (BaseName, AppendToPrefix, AppendToExtension,
                           UniqueName);

      ENTRY -

        BaseName : ARRAY OF CHAR
          The base file name to use.  This string may be null in which case the
          UniqueName returned will be completely composed of the generated name.

        AppendToPrefix : BOOLEAN
          Append onto the current prefix portion of the name to attain uniqueness.

        PrefixDigits : CARDINAL
          Number of digits to add, will zero fill if needed.

        AppendToExtension : BOOLEAN
          Replace the extension portion of the name to attain uniqueness.

        ExtensionDigits : CARDINAL
          Number of digits to add, will zero fill if needed.

      EXIT -

        UniqueName : ARRAY OF CHAR
          The name which currently is not used by any file.  This name will have
          numbers appended to its prefix or extension or both depending on the flags
          which were passed in.  Also this name will be fully qualified with the path
          to where the file would be.

      EXAMPLE -

        FullName := "C:\Dir1\Dir2\Test";

        GetUniqueFilename (FullName,
                           TRUE,
                           4,
                           FALSE,
                           0,
                           FullName);

        FullName returns as "C:\Dir1\Dir2\Test0001"

        FullName := "C:\Dir1\Dir2\Test";

        GetUniqueFilename (FullName,
                           FALSE,
                           0,
                           TRUE,
                           3,
                           FullName);

        FullName returns as "C:\Dir1\Dir2\Test.001"

        FullName := "C:\Dir1\Dir2\Test.EXE";

        GetUniqueFilename (FullName,
                           TRUE,
                           4,
                           FALSE,
                           0,
                           FullName);

        FullName returns as "C:\Dir1\Dir2\Test0001.EXE"

        FullName := "C:\Dir1\Dir2\Test.EXE";

        GetUniqueFilename (FullName,
                           FALSE,
                           0,
                           TRUE,
                           2,
                           FullName);

        FullName returns as "C:\Dir1\Dir2\Test.EXE01"

        FullName := "C:\Dir1\Dir2\Test.EXE";

        GetUniqueFilename (FullName,
                           TRUE,
                           4,
                           TRUE,
                           4,
                           FullName);

        FullName returns as "C:\Dir1\Dir2\Test0001.EXE0001"
  *)




  PROCEDURE SetToExePath
             (CONST OldFileName : ARRAY OF CHAR;
              VAR   NewFileName : ARRAY OF CHAR);

  (**
      SetToExePath - Set this file to the same folder as the exe.


      This procedure takes in a filename with or without path, modifies
      it so that the path is the same as the .exe folder and returns the
      result.  This is ideal for applications that may let the user browse
      around the drives but when certain files are accessed want to store
      them in the same location as the executable.  This is an easy way to
      figure out what that path should be.

      CALLING SEQUENCE -

        SetToExePath (OldFileName, NewFileName)

      ENTRY -

        OldFileName : ARRAY OF CHAR
          The filename with or without a path.

      EXIT -

        NewFileName : ARRAY OF CHAR
          A fully qualified filename including path that now points to the
          same folder where the currently running executable is located.

  *)




Send mail to   warren.merrill@inl.gov with questions or comments about this web site.
Copyright © 1989-2006 Battelle Energy Alliance