How to insert or replace a header with file name, current date and user name into current file?

How to insert or replace a header with file name, current date and user name into current file?

3
NewbieNewbie
3

    Nov 25, 2020#1

    Hallo all

    I made two scripts using the GetFilePath function, one to insert a header with filepath. I got the insert to run correctly but for the update the backslashes are not written.
    For comparison here the lines, the first one for the insert and the second one for the update. 

    Code: Select all

    //Insert Filepath
    UltraEdit.activeDocument.write("-- = $Id:          " + GetFilePath() + GetNameOfFile() + "\ $\r\n");
    //UpdateFilepath
    UltraEdit.activeDocument.findReplace.replace("\\\$Id: .* \\\$","$Id:          " + GetFilePath() + GetNameOfFile() + "\ $");
    
    I did not find an answer where I made the error or how to escape the backslashes. 

    InsertHeader.js:

    Code: Select all

    //Insert Header Script
    var nCursorPos = UltraEdit.activeDocument.currentPos;
    UltraEdit.activeDocument.copyFilePath();  
    UltraEdit.activeDocument.write("-- =======================================================================\r\n");
    UltraEdit.activeDocument.write("-- = $Project:     OTMS $\r\n");
    UltraEdit.activeDocument.write("-- = $Description: Descriptiontext $\r\n");
    //Insert Author
    UltraEdit.activeDocument.write("-- = $Author:      USERNAME $\r\n");
    //Insert Date
    function todaysdate() {
    var currentDate = new Date();
    currentDate.setDate(currentDate.getDate());
    var d = currentDate.getDay()
    var dt = currentDate.getDate();
    dt = (dt < 10 ? '0' : '') + dt;
    var m = currentDate.getMonth() + 1;
    var year = currentDate.getFullYear();
    var mn = currentDate.getMinutes();
    mn = (mn < 10 ? '0' : '') + mn;
    var hr = currentDate.getHours();
    hr = (hr < 10 ? '0' : '') + hr;
    UltraEdit.activeDocument.write("-- = $Date:        " + year + "/" + m + "/" + dt  + "/" +  hr + ":" + mn + " $\r\n")}
    todaysdate();
    //Insert Id
    function GetFileExt (CompleteFileNameOrDocIndexNumber, bWithPoint)
    {
       var sFileExtension = "";
       var sNameOfFile = "";
       var sFullFileName = "";
       var nLastDirDelim = -1;
    
       var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 0;
    
       if (sFileExtension.length && (typeof(bWithPoint) == "boolean"))
       {
          if (bWithPoint) sFileExtension = "." + sFileExtension;
       }
    
       if (typeof(CompleteFileNameOrDocIndexNumber) == "string")
       {
          sFullFileName = CompleteFileNameOrDocIndexNumber;
    
          if (!sFullFileName.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("The input string is an empty string!","GetFileExt Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileExt: The input string is an empty string!");
             }
             return sFileExtension;
          }
    
          nLastDirDelim = sFullFileName.lastIndexOf("|");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0) sNameOfFile = sFullFileName;
          else
          {
             if (nLastDirDelim == (sFullFileName.length-1))
             {
                if (nOutputType == 2)
                {
                   UltraEdit.messageBox("The input string is a path string!","GetFileExt Error");
                }
                else if (nOutputType == 1)
                {
                   if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                   UltraEdit.outputWindow.write("GetFileExt: The input string is a path string!");
                }
                return sFileExtension;
             }
    
             nLastDirDelim++;
             sNameOfFile = sFullFileName.substring(nLastDirDelim);
          }
       }
       else
       {
          if (UltraEdit.document.length < 1)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("No document is open currently!","GetFileExt Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileExt: No document is open currently!");
             }
             return sFileExtension;
          }
    
          var nDocumentNumber = -1;
          if (typeof(CompleteFileNameOrDocIndexNumber) == "number")
          {
             nDocumentNumber = CompleteFileNameOrDocIndexNumber;
          }
    
          if (nDocumentNumber >= UltraEdit.document.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("A document with index number "+nDocumentNumber+" does not exist!","GetFileExt Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileExt: A document with index number "+nDocumentNumber+" does not exist!");
             }
             return sFileExtension;
          }
    
          if (nDocumentNumber < 0)
          {
             sFullFileName = UltraEdit.activeDocument.path;
             if (UltraEdit.activeDocument.isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          else
          {
             sFullFileName = UltraEdit.document[nDocumentNumber].path;
             if (UltraEdit.document[nDocumentNumber].isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File extension can't be determined from a new file!","GetFileExt Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileExt: File extension can't be determined from a new file!");
             }
             return sFileExtension;
          }
    
          nLastDirDelim++;
          sNameOfFile = sFullFileName.substring(nLastDirDelim);
    
       }
    
       var nLastPointPos = sNameOfFile.lastIndexOf(".");
       if ((nLastPointPos < 1) || (nLastPointPos == (sNameOfFile.length-1)))
       {
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("File \""+sNameOfFile+"\" has no file extension!","GetFileExt Warning");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFileExt: File \""+sNameOfFile+"\" has no file extension!");
          }
          if (typeof(bWithPoint) == "boolean")
          {
             if (bWithPoint) return ".";
          }
          return "";
       }
       if (typeof(bWithPoint) != "boolean") nLastPointPos++;
       else if (!bWithPoint) nLastPointPos++;
       sFileExtension = sNameOfFile.substring(nLastPointPos);
       return sFileExtension;
    }
    
    
    function GetFileName (CompleteFileNameOrDocIndexNumber, bWithPoint, bWithPath)
    {
       var sNameOfFile = "";
       var sFilePath = "";
       var sFullFileName = "";
       var nLastDirDelim = -1;
    
       var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 0;
    
       if (sNameOfFile.length)
       {
          if (typeof(bWithPoint) == "boolean")
          {
             if (bWithPoint) sNameOfFile += '.';
          }
          if (sFilePath.length && (typeof(bWithPath) == "boolean"))
          {
             if (bWithPath) sNameOfFile = sFilePath + sNameOfFile;
          }
       }
    
       if (typeof(CompleteFileNameOrDocIndexNumber) == "string")
       {
          sFullFileName = CompleteFileNameOrDocIndexNumber;
    
          if (!sFullFileName.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("The input string is an empty string!","GetFileName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileName: The input string is an empty string!");
             }
             return sNameOfFile;
          }
    
          nLastDirDelim = sFullFileName.lastIndexOf("|");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0) sNameOfFile = sFullFileName;
          else
          {
             if (nLastDirDelim == (sFullFileName.length-1))
             {
                if (nOutputType == 2)
                {
                   UltraEdit.messageBox("The input string is a path string!","GetFileName Error");
                }
                else if (nOutputType == 1)
                {
                   if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                   UltraEdit.outputWindow.write("GetFileName: The input string is a path string!");
                }
                return sNameOfFile;
             }
    
             nLastDirDelim++;
             sNameOfFile = sFullFileName.substring(nLastDirDelim);
             sFilePath = sFullFileName.substring(0,nLastDirDelim);
          }
       }
       else
       {
          if (UltraEdit.document.length < 1)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("No document is open currently!","GetFileName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileName: No document is open currently!");
             }
             return sNameOfFile;
          }
    
          var nDocumentNumber = -1;
          if (typeof(CompleteFileNameOrDocIndexNumber) == "number")
          {
             nDocumentNumber = CompleteFileNameOrDocIndexNumber;
          }
    
          if (nDocumentNumber >= UltraEdit.document.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("A document with index number "+nDocumentNumber+" does not exist!","GetFileName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileName: A document with index number "+nDocumentNumber+" does not exist!");
             }
             return sNameOfFile;
          }
    
          if (nDocumentNumber < 0)
          {
             sFullFileName = UltraEdit.activeDocument.path;
             if (UltraEdit.activeDocument.isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          else
          {
             sFullFileName = UltraEdit.document[nDocumentNumber].path;
             if (UltraEdit.document[nDocumentNumber].isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File name can't be determined from a new file!","GetFileName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileName: File name can't be determined from a new file!");
             }
             return sNameOfFile;
          }
    
          nLastDirDelim++;
          sNameOfFile = sFullFileName.substring(nLastDirDelim);
          sFilePath = sFullFileName.substring(0,nLastDirDelim);
    
       }
    
       var nLastPointPos = sNameOfFile.lastIndexOf(".");
       if (nLastPointPos <= 0)
       {
          if (!nLastPointPos)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File \""+sNameOfFile+"\" starts with a dot!","GetFileName Warning");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileName: File \""+sNameOfFile+"\" starts with a dot!");
             }
          }
          if (typeof(bWithPoint) == "boolean")
          {
             if (bWithPoint) sNameOfFile += '.';
          }
          if (typeof(bWithPath) != "boolean") return sNameOfFile;
          if (!bWithPath) return sNameOfFile;
          return sFilePath + sNameOfFile;
       }
       if (typeof(bWithPoint) == "boolean")
       {
          if (bWithPoint) nLastPointPos++;
       }
       sFullFileName = sNameOfFile.substring(0,nLastPointPos);
       if (typeof(bWithPath) != "boolean") return sFullFileName;
       if (!bWithPath) return sFullFileName;
       return sFilePath + sFullFileName;
    }
    
    
    function GetFilePath (CompleteFileNameOrDocIndexNumber)
    {
       var sFilePath = "";
       var sFullFileName = "";
       var nLastDirDelim = -1;
    
       var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 0;
    
       if (typeof(CompleteFileNameOrDocIndexNumber) == "string")
       {
          sFullFileName = CompleteFileNameOrDocIndexNumber;
    
          nLastDirDelim = sFullFileName.lastIndexOf("|");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File path can't be determined from \""+sFullFileName+"\"!","GetFilePath Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFilePath: File path can't be determined from \""+sFullFileName+"\"!");
             }
             return sFilePath;
          }
       }
       else
       {
          if (UltraEdit.document.length < 1)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("No document is open currently!","GetFilePath Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFilePath: No document is open currently!");
             }
             return sFilePath;
          }
    
          var nDocumentNumber = -1;
          if (typeof(CompleteFileNameOrDocIndexNumber) == "number")
          {
             nDocumentNumber = CompleteFileNameOrDocIndexNumber;
          }
    
          if (nDocumentNumber >= UltraEdit.document.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("A document with index number "+nDocumentNumber+" does not exist!","GetFilePath Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFilePath: A document with index number "+nDocumentNumber+" does not exist!");
             }
             return sFilePath;
          }
    
          if (nDocumentNumber < 0)
          {
             sFullFileName = UltraEdit.activeDocument.path;
             if (UltraEdit.activeDocument.isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          else
          {
             sFullFileName = UltraEdit.document[nDocumentNumber].path;
             if (UltraEdit.document[nDocumentNumber].isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File path can't be determined from a new file!","GetFilePath Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFilePath: File path can't be determined from a new file!");
             }
             return sFilePath;
          }
    
       }
    
       if (sFullFileName.charAt(nLastDirDelim) != '|')
       {
          nLastDirDelim++;
          sFilePath = sFullFileName.substring(0,nLastDirDelim);
       }
       else
       {
          sFilePath = sFullFileName.substring(0,nLastDirDelim) + "/";
       }
       return sFilePath;
    }
    
    
    function GetFolderName (CompleteFileNameOrDocIndexNumber)
    {
       var sFolderName = "";
       var sFullFileName = "";
       var nLastDirDelim = -1;
    
       var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 0;
    
       if (typeof(CompleteFileNameOrDocIndexNumber) == "string")
       {
          sFullFileName = CompleteFileNameOrDocIndexNumber;
    
          nLastDirDelim = sFullFileName.lastIndexOf("|");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("Folder name can't be determined from \""+sFullFileName+"\"!","GetFolderName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFolderName: Folder name can't be determined from \""+sFullFileName+"\"!");
             }
             return sFolderName;
          }
       }
       else
       {
          if (UltraEdit.document.length < 1)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("No document is open currently!","GetFolderName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFolderName: No document is open currently!");
             }
             return sFolderName;
          }
    
          var nDocumentNumber = -1;
          if (typeof(CompleteFileNameOrDocIndexNumber) == "number")
          {
             nDocumentNumber = CompleteFileNameOrDocIndexNumber;
          }
    
          if (nDocumentNumber >= UltraEdit.document.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("A document with index number "+nDocumentNumber+" does not exist!","GetFolderName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFolderName: A document with index number "+nDocumentNumber+" does not exist!");
             }
             return sFolderName;
          }
    
          if (nDocumentNumber < 0)
          {
             sFullFileName = UltraEdit.activeDocument.path;
             if (UltraEdit.activeDocument.isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          else
          {
             sFullFileName = UltraEdit.document[nDocumentNumber].path;
             if (UltraEdit.document[nDocumentNumber].isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("Folder name can't be determined from a new file!","GetFolderName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFolderName: Folder name can't be determined from a new file!");
             }
             return sFolderName;
          }
    
       }
    
       if (!nLastDirDelim)
       {
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("The file is stored in root of a drive!","GetFolderName Error");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFolderName: The file is stored in root of a drive!");
          }
          return sFolderName;
       }
    
       var sFilePath = sFullFileName.substring(0,nLastDirDelim);
    
       if (!sFilePath.search(/^[A-Za-z]:$/))
       {
          var sDriveLetter = sFilePath.charAt(0).toUpperCase();
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("The file is stored in root of drive "+sDriveLetter+":","GetFolderName Error");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFolderName: The file is stored in root of drive "+sDriveLetter+":");
          }
          return sFolderName;
       }
    
       if (!sFilePath.search(/^[\/\\]{2}.+?[\/\\][^\\]+$/))
       {
          sFilePath += sFullFileName.charAt(nLastDirDelim);
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("The file is stored in shared folder \""+sFilePath+"\"!","GetFolderName Error");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFolderName: The file is stored in shared folder \""+sFilePath+"\"!");
          }
          return sFolderName;
       }
    
       if (!sFilePath.search(/^(?:s?ftp:\/\/|S?FTP::)[^\/\\]+[\/\\]*$/))
       {
          var sServerName = sFilePath.replace(/^(?:s?ftp:\/\/|S?FTP::)([^\/\\]+)[\/\\]*$/,"$1");
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("The file is stored in root of FTP server \""+sServerName+"\"!","GetFolderName Error");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFolderName: The file is stored in root of FTP server \""+sServerName+"\"!");
          }
          return sFolderName;
       }
    
       var nDirDelim = sFilePath.lastIndexOf("/");
       if (nDirDelim < 0)
       {
          nDirDelim = sFilePath.lastIndexOf("\\");
       }
       nDirDelim++;
       var sLastFolderName = sFilePath.substring(nDirDelim);
    
       if (!sLastFolderName.search(/^[.\/\\]+$/))
       {
          sFilePath += sFullFileName.charAt(nLastDirDelim);
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("The folder name can't be determined from relative file path \""+sFilePath+"\"!","GetFolderName Error");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFolderName: The folder name can't be determined from relative file path \""+sFilePath+"\"!");
          }
          return sFolderName;
       }
    
       return sLastFolderName;
    }
    
    
    function GetNameOfFile (CompleteFileNameOrDocIndexNumber)
    {
       var sNameOfFile = "";
       var sFullFileName = "";
       var nLastDirDelim = -1;
    
       var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 0;
    
       if (typeof(CompleteFileNameOrDocIndexNumber) == "string")
       {
          sFullFileName = CompleteFileNameOrDocIndexNumber;
    
          if (!sFullFileName.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("The input string is an empty string!","GetNameOfFile Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetNameOfFile: The input string is an empty string!");
             }
             return sNameOfFile;
          }
    
          nLastDirDelim = sFullFileName.lastIndexOf("|");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0) sNameOfFile = sFullFileName;
          else
          {
             if (nLastDirDelim == (sFullFileName.length-1))
             {
                if (nOutputType == 2)
                {
                   UltraEdit.messageBox("The input string is a path string!","GetNameOfFile Error");
                }
                else if (nOutputType == 1)
                {
                   if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                   UltraEdit.outputWindow.write("GetNameOfFile: The input string is a path string!");
                }
                return sNameOfFile;
             }
    
             nLastDirDelim++;
             sNameOfFile = sFullFileName.substring(nLastDirDelim);
          }
       }
       else
       {
          if (UltraEdit.document.length < 1)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("No document is open currently!","GetNameOfFile Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetNameOfFile: No document is open currently!");
             }
             return sNameOfFile;
          }
    
          var nDocumentNumber = -1;
          if (typeof(CompleteFileNameOrDocIndexNumber) == "number")
          {
             nDocumentNumber = CompleteFileNameOrDocIndexNumber;
          }
    
          if (nDocumentNumber >= UltraEdit.document.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("A document with index number "+nDocumentNumber+" does not exist!","GetNameOfFile Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetNameOfFile: A document with index number "+nDocumentNumber+" does not exist!");
             }
             return sNameOfFile;
          }
    
          if (nDocumentNumber < 0)
          {
             sFullFileName = UltraEdit.activeDocument.path;
             if (UltraEdit.activeDocument.isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          else
          {
             sFullFileName = UltraEdit.document[nDocumentNumber].path;
             if (UltraEdit.document[nDocumentNumber].isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File name can't be determined from a new file!","GetNameOfFile Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetNameOfFile: File name can't be determined from a new file!");
             }
             return sNameOfFile;
          }
    
          nLastDirDelim++;
          sNameOfFile = sFullFileName.substring(nLastDirDelim);
    
       }
    
       return sNameOfFile;
    }
    var g_nDebugMessage = 2;
    var sDemoName = "";
    UltraEdit.activeDocument.write("-- = $Id:          " + GetFilePath() + GetNameOfFile() + "\ $\r\n");
    UltraEdit.activeDocument.write("-- =======================================================================\r\n");
    UltraEdit.clearClipboard();
    UltraEdit.activeDocument.gotoPos(nCursorPos);
    UltraEdit.outputWindow.showOutput = true;
    UltraEdit.outputWindow.showWindow(true);
    UltraEdit.outputWindow.write("Unsaved Changes, please save before running script.");
    
    
    UpdateHeader.js

    Code: Select all

    //Update Header Script
    var nCursorPos = UltraEdit.activeDocument.currentPos;
    UltraEdit.activeDocument.copyFilePath();
    UltraEdit.perlReOn();
    UltraEdit.activeDocument.top();
    UltraEdit.activeDocument.findReplace.replaceAll=true;
    UltraEdit.activeDocument.findReplace.regExp = true;
    //Update Author
    UltraEdit.activeDocument.findReplace.replace("\\\$Author: .* \\\$","$Author:      USERNAME $");
    //Update Date
    function todaysdate() {
    var currentDate = new Date();
    currentDate.setDate(currentDate.getDate());
    var d = currentDate.getDay()
    var dt = currentDate.getDate();
    dt = (dt < 10 ? '0' : '') + dt;
    var m = currentDate.getMonth() + 1;
    var year = currentDate.getFullYear();
    var mn = currentDate.getMinutes();
    mn = (mn < 10 ? '0' : '') + mn;
    var hr = currentDate.getHours();
    hr = (hr < 10 ? '0' : '') + hr;
    UltraEdit.activeDocument.findReplace.replace("\\\$Date: .* \\\$","$Date:        " + year + "/" + m + "/" + dt  + "/" +  hr + ":" + mn + " $")}
    todaysdate();
    //Update Id
    function GetFileExt (CompleteFileNameOrDocIndexNumber, bWithPoint)
    {
       var sFileExtension = "";
       var sNameOfFile = "";
       var sFullFileName = "";
       var nLastDirDelim = -1;
    
       var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 0;
    
       if (sFileExtension.length && (typeof(bWithPoint) == "boolean"))
       {
          if (bWithPoint) sFileExtension = "." + sFileExtension;
       }
    
       if (typeof(CompleteFileNameOrDocIndexNumber) == "string")
       {
          sFullFileName = CompleteFileNameOrDocIndexNumber;
    
          if (!sFullFileName.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("The input string is an empty string!","GetFileExt Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileExt: The input string is an empty string!");
             }
             return sFileExtension;
          }
    
          nLastDirDelim = sFullFileName.lastIndexOf("|");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0) sNameOfFile = sFullFileName;
          else
          {
             if (nLastDirDelim == (sFullFileName.length-1))
             {
                if (nOutputType == 2)
                {
                   UltraEdit.messageBox("The input string is a path string!","GetFileExt Error");
                }
                else if (nOutputType == 1)
                {
                   if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                   UltraEdit.outputWindow.write("GetFileExt: The input string is a path string!");
                }
                return sFileExtension;
             }
    
             nLastDirDelim++;
             sNameOfFile = sFullFileName.substring(nLastDirDelim);
          }
       }
       else
       {
          if (UltraEdit.document.length < 1)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("No document is open currently!","GetFileExt Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileExt: No document is open currently!");
             }
             return sFileExtension;
          }
    
          var nDocumentNumber = -1;
          if (typeof(CompleteFileNameOrDocIndexNumber) == "number")
          {
             nDocumentNumber = CompleteFileNameOrDocIndexNumber;
          }
    
          if (nDocumentNumber >= UltraEdit.document.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("A document with index number "+nDocumentNumber+" does not exist!","GetFileExt Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileExt: A document with index number "+nDocumentNumber+" does not exist!");
             }
             return sFileExtension;
          }
    
          if (nDocumentNumber < 0)
          {
             sFullFileName = UltraEdit.activeDocument.path;
             if (UltraEdit.activeDocument.isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          else
          {
             sFullFileName = UltraEdit.document[nDocumentNumber].path;
             if (UltraEdit.document[nDocumentNumber].isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File extension can't be determined from a new file!","GetFileExt Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileExt: File extension can't be determined from a new file!");
             }
             return sFileExtension;
          }
    
          nLastDirDelim++;
          sNameOfFile = sFullFileName.substring(nLastDirDelim);
    
       }
    
       var nLastPointPos = sNameOfFile.lastIndexOf(".");
       if ((nLastPointPos < 1) || (nLastPointPos == (sNameOfFile.length-1)))
       {
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("File \""+sNameOfFile+"\" has no file extension!","GetFileExt Warning");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFileExt: File \""+sNameOfFile+"\" has no file extension!");
          }
          if (typeof(bWithPoint) == "boolean")
          {
             if (bWithPoint) return ".";
          }
          return "";
       }
       if (typeof(bWithPoint) != "boolean") nLastPointPos++;
       else if (!bWithPoint) nLastPointPos++;
       sFileExtension = sNameOfFile.substring(nLastPointPos);
       return sFileExtension;
    }
    
    
    function GetFileName (CompleteFileNameOrDocIndexNumber, bWithPoint, bWithPath)
    {
       var sNameOfFile = "";
       var sFilePath = "";
       var sFullFileName = "";
       var nLastDirDelim = -1;
    
       var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 0;
    
       if (sNameOfFile.length)
       {
          if (typeof(bWithPoint) == "boolean")
          {
             if (bWithPoint) sNameOfFile += '.';
          }
          if (sFilePath.length && (typeof(bWithPath) == "boolean"))
          {
             if (bWithPath) sNameOfFile = sFilePath + sNameOfFile;
          }
       }
    
       if (typeof(CompleteFileNameOrDocIndexNumber) == "string")
       {
          sFullFileName = CompleteFileNameOrDocIndexNumber;
    
          if (!sFullFileName.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("The input string is an empty string!","GetFileName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileName: The input string is an empty string!");
             }
             return sNameOfFile;
          }
    
          nLastDirDelim = sFullFileName.lastIndexOf("|");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0) sNameOfFile = sFullFileName;
          else
          {
             if (nLastDirDelim == (sFullFileName.length-1))
             {
                if (nOutputType == 2)
                {
                   UltraEdit.messageBox("The input string is a path string!","GetFileName Error");
                }
                else if (nOutputType == 1)
                {
                   if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                   UltraEdit.outputWindow.write("GetFileName: The input string is a path string!");
                }
                return sNameOfFile;
             }
    
             nLastDirDelim++;
             sNameOfFile = sFullFileName.substring(nLastDirDelim);
             sFilePath = sFullFileName.substring(0,nLastDirDelim);
          }
       }
       else
       {
          if (UltraEdit.document.length < 1)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("No document is open currently!","GetFileName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileName: No document is open currently!");
             }
             return sNameOfFile;
          }
    
          var nDocumentNumber = -1;
          if (typeof(CompleteFileNameOrDocIndexNumber) == "number")
          {
             nDocumentNumber = CompleteFileNameOrDocIndexNumber;
          }
    
          if (nDocumentNumber >= UltraEdit.document.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("A document with index number "+nDocumentNumber+" does not exist!","GetFileName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileName: A document with index number "+nDocumentNumber+" does not exist!");
             }
             return sNameOfFile;
          }
    
          if (nDocumentNumber < 0)
          {
             sFullFileName = UltraEdit.activeDocument.path;
             if (UltraEdit.activeDocument.isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          else
          {
             sFullFileName = UltraEdit.document[nDocumentNumber].path;
             if (UltraEdit.document[nDocumentNumber].isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File name can't be determined from a new file!","GetFileName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileName: File name can't be determined from a new file!");
             }
             return sNameOfFile;
          }
    
          nLastDirDelim++;
          sNameOfFile = sFullFileName.substring(nLastDirDelim);
          sFilePath = sFullFileName.substring(0,nLastDirDelim);
    
       }
    
       var nLastPointPos = sNameOfFile.lastIndexOf(".");
       if (nLastPointPos <= 0)
       {
          if (!nLastPointPos)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File \""+sNameOfFile+"\" starts with a dot!","GetFileName Warning");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFileName: File \""+sNameOfFile+"\" starts with a dot!");
             }
          }
          if (typeof(bWithPoint) == "boolean")
          {
             if (bWithPoint) sNameOfFile += '.';
          }
          if (typeof(bWithPath) != "boolean") return sNameOfFile;
          if (!bWithPath) return sNameOfFile;
          return sFilePath + sNameOfFile;
       }
       if (typeof(bWithPoint) == "boolean")
       {
          if (bWithPoint) nLastPointPos++;
       }
       sFullFileName = sNameOfFile.substring(0,nLastPointPos);
       if (typeof(bWithPath) != "boolean") return sFullFileName;
       if (!bWithPath) return sFullFileName;
       return sFilePath + sFullFileName;
    }
    
    
    function GetFilePath (CompleteFileNameOrDocIndexNumber)
    {
       var sFilePath = "";
       var sFullFileName = "";
       var nLastDirDelim = -1;
    
       var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 0;
    
       if (typeof(CompleteFileNameOrDocIndexNumber) == "string")
       {
          sFullFileName = CompleteFileNameOrDocIndexNumber;
    
          nLastDirDelim = sFullFileName.lastIndexOf("|");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File path can't be determined from \""+sFullFileName+"\"!","GetFilePath Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFilePath: File path can't be determined from \""+sFullFileName+"\"!");
             }
             return sFilePath;
          }
       }
       else
       {
          if (UltraEdit.document.length < 1)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("No document is open currently!","GetFilePath Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFilePath: No document is open currently!");
             }
             return sFilePath;
          }
    
          var nDocumentNumber = -1;
          if (typeof(CompleteFileNameOrDocIndexNumber) == "number")
          {
             nDocumentNumber = CompleteFileNameOrDocIndexNumber;
          }
    
          if (nDocumentNumber >= UltraEdit.document.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("A document with index number "+nDocumentNumber+" does not exist!","GetFilePath Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFilePath: A document with index number "+nDocumentNumber+" does not exist!");
             }
             return sFilePath;
          }
    
          if (nDocumentNumber < 0)
          {
             sFullFileName = UltraEdit.activeDocument.path;
             if (UltraEdit.activeDocument.isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          else
          {
             sFullFileName = UltraEdit.document[nDocumentNumber].path;
             if (UltraEdit.document[nDocumentNumber].isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File path can't be determined from a new file!","GetFilePath Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFilePath: File path can't be determined from a new file!");
             }
             return sFilePath;
          }
    
       }
    
       if (sFullFileName.charAt(nLastDirDelim) != '|')
       {
          nLastDirDelim++;
          sFilePath = sFullFileName.substring(0,nLastDirDelim);
       }
       else
       {
          sFilePath = sFullFileName.substring(0,nLastDirDelim) + "/";
       }
       return sFilePath;
    }
    
    
    function GetFolderName (CompleteFileNameOrDocIndexNumber)
    {
       var sFolderName = "";
       var sFullFileName = "";
       var nLastDirDelim = -1;
    
       var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 0;
    
       if (typeof(CompleteFileNameOrDocIndexNumber) == "string")
       {
          sFullFileName = CompleteFileNameOrDocIndexNumber;
    
          nLastDirDelim = sFullFileName.lastIndexOf("|");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("Folder name can't be determined from \""+sFullFileName+"\"!","GetFolderName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFolderName: Folder name can't be determined from \""+sFullFileName+"\"!");
             }
             return sFolderName;
          }
       }
       else
       {
          if (UltraEdit.document.length < 1)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("No document is open currently!","GetFolderName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFolderName: No document is open currently!");
             }
             return sFolderName;
          }
    
          var nDocumentNumber = -1;
          if (typeof(CompleteFileNameOrDocIndexNumber) == "number")
          {
             nDocumentNumber = CompleteFileNameOrDocIndexNumber;
          }
    
          if (nDocumentNumber >= UltraEdit.document.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("A document with index number "+nDocumentNumber+" does not exist!","GetFolderName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFolderName: A document with index number "+nDocumentNumber+" does not exist!");
             }
             return sFolderName;
          }
    
          if (nDocumentNumber < 0)
          {
             sFullFileName = UltraEdit.activeDocument.path;
             if (UltraEdit.activeDocument.isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          else
          {
             sFullFileName = UltraEdit.document[nDocumentNumber].path;
             if (UltraEdit.document[nDocumentNumber].isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("Folder name can't be determined from a new file!","GetFolderName Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetFolderName: Folder name can't be determined from a new file!");
             }
             return sFolderName;
          }
    
       }
    
       if (!nLastDirDelim)
       {
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("The file is stored in root of a drive!","GetFolderName Error");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFolderName: The file is stored in root of a drive!");
          }
          return sFolderName;
       }
    
       var sFilePath = sFullFileName.substring(0,nLastDirDelim);
    
       if (!sFilePath.search(/^[A-Za-z]:$/))
       {
          var sDriveLetter = sFilePath.charAt(0).toUpperCase();
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("The file is stored in root of drive "+sDriveLetter+":","GetFolderName Error");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFolderName: The file is stored in root of drive "+sDriveLetter+":");
          }
          return sFolderName;
       }
    
       if (!sFilePath.search(/^[\/\\]{2}.+?[\/\\][^\\]+$/))
       {
          sFilePath += sFullFileName.charAt(nLastDirDelim);
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("The file is stored in shared folder \""+sFilePath+"\"!","GetFolderName Error");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFolderName: The file is stored in shared folder \""+sFilePath+"\"!");
          }
          return sFolderName;
       }
    
       if (!sFilePath.search(/^(?:s?ftp:\/\/|S?FTP::)[^\/\\]+[\/\\]*$/))
       {
          var sServerName = sFilePath.replace(/^(?:s?ftp:\/\/|S?FTP::)([^\/\\]+)[\/\\]*$/,"$1");
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("The file is stored in root of FTP server \""+sServerName+"\"!","GetFolderName Error");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFolderName: The file is stored in root of FTP server \""+sServerName+"\"!");
          }
          return sFolderName;
       }
    
       var nDirDelim = sFilePath.lastIndexOf("/");
       if (nDirDelim < 0)
       {
          nDirDelim = sFilePath.lastIndexOf("\\");
       }
       nDirDelim++;
       var sLastFolderName = sFilePath.substring(nDirDelim);
    
       if (!sLastFolderName.search(/^[.\/\\]+$/))
       {
          sFilePath += sFullFileName.charAt(nLastDirDelim);
          if (nOutputType == 2)
          {
             UltraEdit.messageBox("The folder name can't be determined from relative file path \""+sFilePath+"\"!","GetFolderName Error");
          }
          else if (nOutputType == 1)
          {
             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
             UltraEdit.outputWindow.write("GetFolderName: The folder name can't be determined from relative file path \""+sFilePath+"\"!");
          }
          return sFolderName;
       }
    
       return sLastFolderName;
    }
    
    
    function GetNameOfFile (CompleteFileNameOrDocIndexNumber)
    {
       var sNameOfFile = "";
       var sFullFileName = "";
       var nLastDirDelim = -1;
    
       var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 0;
    
       if (typeof(CompleteFileNameOrDocIndexNumber) == "string")
       {
          sFullFileName = CompleteFileNameOrDocIndexNumber;
    
          if (!sFullFileName.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("The input string is an empty string!","GetNameOfFile Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetNameOfFile: The input string is an empty string!");
             }
             return sNameOfFile;
          }
    
          nLastDirDelim = sFullFileName.lastIndexOf("|");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
    
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0) sNameOfFile = sFullFileName;
          else
          {
             if (nLastDirDelim == (sFullFileName.length-1))
             {
                if (nOutputType == 2)
                {
                   UltraEdit.messageBox("The input string is a path string!","GetNameOfFile Error");
                }
                else if (nOutputType == 1)
                {
                   if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                   UltraEdit.outputWindow.write("GetNameOfFile: The input string is a path string!");
                }
                return sNameOfFile;
             }
    
             nLastDirDelim++;
             sNameOfFile = sFullFileName.substring(nLastDirDelim);
          }
       }
       else
       {
          if (UltraEdit.document.length < 1)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("No document is open currently!","GetNameOfFile Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetNameOfFile: No document is open currently!");
             }
             return sNameOfFile;
          }
    
          var nDocumentNumber = -1;
          if (typeof(CompleteFileNameOrDocIndexNumber) == "number")
          {
             nDocumentNumber = CompleteFileNameOrDocIndexNumber;
          }
    
          if (nDocumentNumber >= UltraEdit.document.length)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("A document with index number "+nDocumentNumber+" does not exist!","GetNameOfFile Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetNameOfFile: A document with index number "+nDocumentNumber+" does not exist!");
             }
             return sNameOfFile;
          }
    
          if (nDocumentNumber < 0)
          {
             sFullFileName = UltraEdit.activeDocument.path;
             if (UltraEdit.activeDocument.isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          else
          {
             sFullFileName = UltraEdit.document[nDocumentNumber].path;
             if (UltraEdit.document[nDocumentNumber].isFTP())
             {
                nLastDirDelim = sFullFileName.lastIndexOf("|");
                if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
             }
          }
          if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
    
          if (nLastDirDelim < 0)
          {
             if (nOutputType == 2)
             {
                UltraEdit.messageBox("File name can't be determined from a new file!","GetNameOfFile Error");
             }
             else if (nOutputType == 1)
             {
                if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
                UltraEdit.outputWindow.write("GetNameOfFile: File name can't be determined from a new file!");
             }
             return sNameOfFile;
          }
    
          nLastDirDelim++;
          sNameOfFile = sFullFileName.substring(nLastDirDelim);
    
       }
    
       return sNameOfFile;
    }
    var g_nDebugMessage = 2;
    var sDemoName = "";
    UltraEdit.activeDocument.findReplace.replace("\\\$Id: .* \\\$","$Id:          " + GetFilePath() + GetNameOfFile() + "\ $");
    UltraEdit.clearClipboard();
    UltraEdit.activeDocument.gotoPos(nCursorPos);
    
    

    6,686585
    Grand MasterGrand Master
    6,686585

      Nov 25, 2020#2

      The backslashes in the full qualified file name used in a Perl regular expression replace string must be all escaped with one more backslash to be interpreted as backslash character by the Perl regular expression function. There is no need for the file name functions GetFilePath and GetNameOfFile on full qualified file name of active file should be inserted into active file. The file name functions written by me are for splitting up the full qualified file name of a file into its parts (path, name, extension, name + extension).

      The insert header script optimized for the task:

      Code: Select all

      // Insert Header Script
      
      function todaysdate() {
         var currentDate = new Date();
         currentDate.setDate(currentDate.getDate());
         var d = currentDate.getDay()
         var dt = currentDate.getDate();
         dt = (dt < 10 ? '0' : '') + dt;
         var m = currentDate.getMonth() + 1;
         var year = currentDate.getFullYear();
         var mn = currentDate.getMinutes();
         mn = (mn < 10 ? '0' : '') + mn;
         var hr = currentDate.getHours();
         hr = (hr < 10 ? '0' : '') + hr;
         UltraEdit.activeDocument.write("-- = $Date:        " + year + "/" + m + "/" + dt  + "/" +  hr + ":" + mn + " $\r\n")
      }
      
      if (UltraEdit.document.length > 0) {  // Is any file opened?
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         var nCursorPos = UltraEdit.activeDocument.currentPos;
         UltraEdit.activeDocument.write("-- =======================================================================\r\n");
         UltraEdit.activeDocument.write("-- = $Project:     OTMS $\r\n");
         UltraEdit.activeDocument.write("-- = $Description: Descriptiontext $\r\n");
         // Insert Author
         UltraEdit.activeDocument.write("-- = $Author:      USERNAME $\r\n");
         // Insert Date
         todaysdate();
         // Insert Id
         UltraEdit.activeDocument.write("-- = $Id:          " + UltraEdit.activeDocument.path + " $\r\n");
         UltraEdit.activeDocument.write("-- =======================================================================\r\n");
         UltraEdit.activeDocument.gotoPos(nCursorPos);
         UltraEdit.outputWindow.showOutput = true;
         UltraEdit.outputWindow.showWindow(true);
         UltraEdit.outputWindow.write("Unsaved Changes, please save before running script.");
      }
      
      The update header script optimized for the task:

      Code: Select all

      // Update Header Script
      
      function todaysdate() {
         var currentDate = new Date();
         currentDate.setDate(currentDate.getDate());
         var d = currentDate.getDay()
         var dt = currentDate.getDate();
         dt = (dt < 10 ? '0' : '') + dt;
         var m = currentDate.getMonth() + 1;
         var year = currentDate.getFullYear();
         var mn = currentDate.getMinutes();
         mn = (mn < 10 ? '0' : '') + mn;
         var hr = currentDate.getHours();
         hr = (hr < 10 ? '0' : '') + hr;
         UltraEdit.activeDocument.findReplace.replace("\\$Date: .* \\$","$Date:        " + year + "/" + m + "/" + dt  + "/" +  hr + ":" + mn + " $")
      }
      
      if (UltraEdit.document.length > 0) {  // Is any file opened?
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         var nCursorPos = UltraEdit.activeDocument.currentPos;
         UltraEdit.perlReOn();
         UltraEdit.activeDocument.top();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         // Update Author
         UltraEdit.activeDocument.findReplace.replace("\\$Author: .* \\$","$Author:      USERNAME $");
         // Update Date
         todaysdate();
         // Update Id
         UltraEdit.activeDocument.findReplace.replace("\\$Id: .* \\$","$Id:          " + UltraEdit.activeDocument.path.replace(/\\/g,"\\\\") + " $");
         UltraEdit.activeDocument.gotoPos(nCursorPos);
      }
      
      A script to insert or update the header if the header always exists only once in a file:

      Code: Select all

      // Insert or Update Header Script
      
      function todaysdate() {
         var currentDate = new Date();
         currentDate.setDate(currentDate.getDate());
         var d = currentDate.getDay()
         var dt = currentDate.getDate();
         dt = (dt < 10 ? '0' : '') + dt;
         var m = currentDate.getMonth() + 1;
         var year = currentDate.getFullYear();
         var mn = currentDate.getMinutes();
         mn = (mn < 10 ? '0' : '') + mn;
         var hr = currentDate.getHours();
         hr = (hr < 10 ? '0' : '') + hr;
         return "-- = $Date:        " + year + "/" + m + "/" + dt  + "/" +  hr + ":" + mn + " $\r\n";
      }
      
      if (UltraEdit.document.length > 0) {  // Is any file opened?
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         var nCursorPos = UltraEdit.activeDocument.currentPos;
         var sHeaderBlock;
         UltraEdit.perlReOn();
         UltraEdit.activeDocument.top();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         if (UltraEdit.activeDocument.findReplace.find("\\$Author:.*\\r\\n.*\\$Date:.*\\r\\n.*\\$Id:.*$"))
         {
            sHeaderBlock = "$Author:      USERNAME $\r\n" + todaysdate() +
                           "-- = $Id:          " + UltraEdit.activeDocument.path + " $";
         }
         else
         {
            UltraEdit.activeDocument.gotoPos(nCursorPos);
            sHeaderBlock = "-- =======================================================================\r\n" +
                           "-- = $Project:     OTMS $\r\n" + 
                           "-- = $Description: Descriptiontext $\r\n" +
                           "-- = $Author:      USERNAME $\r\n" + todaysdate() +
                           "-- = $Id:          " + UltraEdit.activeDocument.path + " $\r\n" +
                           "-- =======================================================================\r\n";
         }
         UltraEdit.activeDocument.write(sHeaderBlock);
         UltraEdit.outputWindow.showOutput = true;
         UltraEdit.outputWindow.showWindow(true);
         UltraEdit.outputWindow.write("Unsaved Changes, please save before running script.");
         UltraEdit.activeDocument.gotoPos(nCursorPos);
      }
      
      A script to insert or update the header if the header could exist more than once in a file:

      Code: Select all

      // Insert or Update Header Script
      
      function todaysdate() {
         var currentDate = new Date();
         currentDate.setDate(currentDate.getDate());
         var d = currentDate.getDay()
         var dt = currentDate.getDate();
         dt = (dt < 10 ? '0' : '') + dt;
         var m = currentDate.getMonth() + 1;
         var year = currentDate.getFullYear();
         var mn = currentDate.getMinutes();
         mn = (mn < 10 ? '0' : '') + mn;
         var hr = currentDate.getHours();
         hr = (hr < 10 ? '0' : '') + hr;
         return "-- = $Date:        " + year + "/" + m + "/" + dt  + "/" +  hr + ":" + mn + " $\r\n";
      }
      
      if (UltraEdit.document.length > 0) {  // Is any file opened?
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         var nCursorPos = UltraEdit.activeDocument.currentPos;
         var sHeaderBlock = "$Author:      USERNAME $\\r\\n" + todaysdate() +
                            "-- = $Id:          " + UltraEdit.activeDocument.path.replace(/\\/g,"\\\\") + " $";
         UltraEdit.perlReOn();
         UltraEdit.activeDocument.top();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
      
         if (!UltraEdit.activeDocument.findReplace.replace("\\$Author:.*\\r\\n.*\\$Date:.*\\r\\n.*\\$Id:.*$",sHeaderBlock))
         {
            UltraEdit.activeDocument.gotoPos(nCursorPos);
            sHeaderBlock = "-- =======================================================================\r\n" +
                           "-- = $Project:     OTMS $\r\n" + 
                           "-- = $Description: Descriptiontext $\r\n" +
                           "-- = $Author:      USERNAME $\r\n" + todaysdate() +
                           "-- = $Id:          " + UltraEdit.activeDocument.path + " $\r\n" +
                           "-- =======================================================================\r\n";
            UltraEdit.activeDocument.write(sHeaderBlock);
         }
         UltraEdit.outputWindow.showOutput = true;
         UltraEdit.outputWindow.showWindow(true);
         UltraEdit.outputWindow.write("Unsaved Changes, please save before running script.");
         UltraEdit.activeDocument.gotoPos(nCursorPos);
      }
      
      The second and the fourth script use the replace method of the JavaScript String object to escape each backslash in full qualified file name of active file by one more backslash before concatenating this string with the fixed strings to form the Perl regular expression replace string.
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Nov 25, 2020#3

        Thank you very much Sir.
        A follow-up question: Is there a possibility to use the environment variable  USERNAME instead of a hardcoded username?

        6,686585
        Grand MasterGrand Master
        6,686585

          Nov 25, 2020#4

          There is no direct access to resources of operating system. But it is possible to get the user name string from the environment variable USERNAME on having configured a user tool for this task.

          One solution is configuring a user tool with following settings.

          Tab Command:

          Menu item name: GetUserName (or whatever you like, can contain also spaces)
          Command line: C:\Windows\System32\cmd.exe /C echo %USERNAME%| C:\Windows\System32\clip.exe
          Working directory: let it empty or use C:\Windows\System32
          Toolbar bitmap/icon (file path): let it empty or if you want a custom icon for this user tool, select an image file with an appropriate icon

          Tab Options:

          Program type: DOS program
          Save active file: unchecked
          Save all files first: unchecked

          Tab Output:

          Command output (DOS commands): Append to existing
          Show DOS box: unchecked
          Capture output: unchecked
          Clear output before run: unchecked
          Replace selected text with: No replace
          Handle output as: ANSI (or UTF-16)

          UTF-16 could be necessary instead of ANSI if there is assigned to the environment variable USERNAME a string containing Unicode characters.

          In the script use following command lines to run the configured user tool, select Windows clipboard and get the user name from clipboard assigned to a string variable with removing all leading and trailing whitespaces as command echo outputs the user name string with a carriage return and a line-feed.

          Code: Select all

          UltraEdit.runTool("GetUserName");
          UltraEdit.selectClipboard(0);
          var UserName = UltraEdit.clipboardContent.trim();
          It would be also possible to just output the user name by Windows command processor, let UltraEdit capture the output to the output window (output to list box) and copy the captured output window content to an UltraEdit clipboard from which the user name is assigned to a string variable. That would have the advantage that the Windows clipboard content is not modified by the script. Please let me know if you would like the alternative solution which does not modify the Windows clipboard content.
          Best regards from an UC/UE/UES for Windows user from Austria

          3
          NewbieNewbie
          3

            Nov 26, 2020#5

            Once again thank you very much. I've not only learned to know features of UltraEdit, but also on how to write a comprehensive instruction by your posts. For the time being I think I stick to hard coded username, since either one of the solutions needs more "work" for the initial setup than to replace USERNAME in the script.