1

I am using an Excel macro to call up UltraEdit in order to execute a script on some files.

In the Excel macro I write the path that I want the new file to be saved in by the UltraEdit script. Up until this point all work and if I do a write of the value I read in the temporary file I see that I have the correct path. But when I use the saveAs("^c") the file does not get saved to the path I specified, but instead gets saved to the current directory.

If I use the following code it saves the file properly. But I don't want to hard code the path:

  var sPath="H:\\IPEX\\DataFiles\\IPEX_Originals_Cleaned_Files\\"
      + sTransSet 
      +"_"
      + sDocNum
      + "_"
      + now.getFullYear()
      + month
      + day
      + "-"
      + hours
      + minutes
      + seconds
      + ".txt";          

   UltraEdit.saveAs(sPath);

This is the code I am trying to fix:

UltraEdit.selectClipboard(1); // switch to user clipboard #1   
var sPath=asParameterList[0]
      + sTransSet 
      +"_"
      + sDocNum
      + "_"
      + now.getFullYear()
      + month
      + day
      + "-"
      + hours
      + minutes
      + seconds
      + ".txt";

UltraEdit.clipboardContent=sPath;          

UltraEdit.outputWindow.write("sPath Value After assign= "+sPath);   
UltraEdit.saveAs("^c");

The write command shows me the following:

sPath Value After assign=  

H:\\IPEX\\DataFiles\\IPEX_Originals_Cleaned_Files\\856_IPEX-155630-2_20190607-152606.txt

Instead of saving the file to directory

H:\\IPEX\\DataFiles\\IPEX_Originals_Cleaned_Files\\ 

it saves the file to directory

H:\IPEX\DataFiles\Boomi_IPEX_Files

which is the directory of the original file.

Mofi
  • 46,139
  • 17
  • 80
  • 143
SNico
  • 11
  • 1
  • I wonder why it is so important to call `saveAs(^c)` instead of `saveAs(sPath)` – Alexander Pavlov Jun 08 '19 at 01:20
  • This question is most likely related to UltraEdit forum topic: [How to avoid invalid path error message on saving a new file with scripting command UltraEdit.saveAs?](http://forums.ultraedit.com/viewtopic.php?f=52&t=18181) – Mofi Jun 08 '19 at 11:48

2 Answers2

2

I agree with Alexander Pavlov and his analysis of the cause of the issue. The full qualified file name in clipboard is invalid because of having leading newline characters.

And the full qualified file name string contains additionally \\ instead of just \ as directory separator. \\ within a file path is no problem for Windows file system kernel functions in comparison to the newline character which is an invalid character for a file name or path according to Microsoft's documentation Naming Files, Paths, and Namespaces. But the full qualified file name should be nevertheless 100% correct.

These issues should be best fixed in Visual Basic macro in Excel file. In Visual Basic strings the backslash character is not an escape character like in other programming and scripting languages. Therefore the file path must be defined in Visual Basic macro with just \ and not with \\. And leading spaces/tabs/newline characters should be also removed from file path string in Excel macro before copying the full qualified file name to clipboard.

It is of course also possible to use in UltraEdit script following line before saving the new file with full qualified name in clipboard.

UltraEdit.clipboardContent = UltraEdit.clipboardContent.replace(/^\s+/,"").replace(/\\\\/g,"\\");

The first replace removes all leading whitespaces according to Unicode definition and the second replace modifies all occurrences of \\ to just \ in file name string before the fixed full qualified file name is copied back to clipboard.

But there is really no need to use a clipboard as also written by Alexander Pavlov. The invalid full qualified file name string is already stored in a JavaScript String object with the not really good name sPath. A better name for this string variable would be sFullFileName. The UltraEdit function UltraEdit.saveAs() expects a String object as parameter. So it is also possible to use in UltraEdit script:

var sFullFileName = sPath.replace(/^\s+/,"").replace(/\\\\/g,"\\");
UltraEdit.saveAs(sFullFileName);

Note: Replacing all occurrences of \\ by just \ would be wrong for full qualified file name having a UNC path which must start with two backslashes.

Mofi
  • 46,139
  • 17
  • 80
  • 143
1

Your debug output prints file name on a new line. It seems filename is preceded with a new line symbol. I think it is why it does not work. Try to trim all spaces and new lines from sPath up until the first character.

Alexander Pavlov
  • 2,264
  • 18
  • 25