The mistake in your script code is that you created a file name with double quotes. The double quote character is not allowed in a file name. Double quotes must be used only on command line if full file name contains a space character. The double quotes determine where the string of the file name starts and where it ends, but do not belong to the file name itself.
I offer two solutions. The first one is very quick on execution, but does not work for all possibilities that could occur. However, it works very well if you execute the script always on a file which has already a file extension or is a new file not yet saved.
- Code: Select all
var sActFileName = UltraEdit.activeDocument.path;
var nLastDot = sActFileName.lastIndexOf('.');
if (nLastDot >= 0) {
// Insert "_new" left to last dot which is hopefully the dot separating the
// file extension from the file name and not a dot anywhere in file path.
var sNewFileName = sActFileName.substr(0,nLastDot) + "_new" + sActFileName.substr(nLastDot);
} else {
// The file name has no dot (new file) and therefore append ".txt".
var sNewFileName = sActFileName + ".txt";
}
UltraEdit.saveAs(sNewFileName);
The ultimate solution working in all possible cases would require using the
general file name evaluating functions. You would need to copy and paste the code of the functions
GetFilePath,
GetFileName and
GetFileExt into your script and append at end of the script below the functions following code:
- Code: Select all
// Insert ".new" left to ".extension" or in case the active document is a
// new file not yet saved, append ".txt" to name displayed in the file tab.
var sNewFileName = GetFilePath();
if(sNewFileName != "") sNewFileName += GetFileName() + "_new" + GetFileExt(-1,true);
else sNewFileName = UltraEdit.activeDocument.path + ".txt";
UltraEdit.saveAs(sNewFileName);