Question on writing a file to a directory

Question on writing a file to a directory

7
NewbieNewbie
7

    Jun 04, 2007#1

    I'm new to UE and am trying to figure out the best approach to take to getting the following to work.

    I have a text file (SAS program source code) that I have residing in my editor. I want to have some easy form of automation where I press a command key and it writes the SAS program to disk in a predefined directory on a server (almost acting as a SAVE), yet still be able to retain my original source that I'm editing on my local hard drive. Basically, I'm trying to copy everything in my editor to a directory on the server, yet keep editing the same source from the local desktop.

    What is actually going on is that I have a "filewatcher" program that monitors the directory on the server and when it sees a new or changed SAS file, it submits the program to SAS. I really want my users to keep their source code that they are editing on their own desktop machines and only write their source to the server when they want to execute the program.

    Does anyone have any suggestions? Can this be accomplished in the "Advanced Tools Configuration" or is this something that requires a macro to accomplish?

    Thanks.

    262
    MasterMaster
    262

      Jun 04, 2007#2

      You don't say which UE version you use, but if you are a UE13 user, then maybe you'll find inspiration in the script I posted in the FTP Transfer from PC to MVS with no filetype-ending. It for a cobol program but it could easily be modified for SAS programs.

      7
      NewbieNewbie
      7

        Jun 04, 2007#3

        Thank you. I'll take a close look at it. I'm pretty overwhelmed with UE (and I am using UE13) at the moment and with all the options that it offers. There's a lot to learn here!

          Jun 05, 2007#4

          I finally got a chance to put your code to work and it does what I had hoped. Thanks for you help.

          I do have one question though, and was hoping you could explain this behavior. As you can tell, I borrowed heavily from your code. The last 18 lines of the program are included. Why does the closeFile() statement, not close the newly created file? If I move it up before the newFile() statement, it will close the original file.

          I was hoping that after creating and saving the new file, I could close the window.

          Code: Select all

             /* Construct full path + filename for submittal */
          
                SASFilePath = myMQBatchPath+SASFileName; 
          
          
             UltraEdit.selectClipboard(8);  /* choose user clipboard 8 */
             UltraEdit.clearClipboard(); 
             
          
             /* sourceDoc.save(); */ /* save local file */
          
              UltraEdit.activeDocument.selectAll();
              UltraEdit.activeDocument.copy();
             
              UltraEdit.newFile();
              UltraEdit.activeDocument.paste();
              UltraEdit.saveAs(SASFilePath); 
          
              UltraEdit.closeFile(UltraEdit.activeDocument.path,2);  
             
              UltraEdit.clearClipboard(8);
              UltraEdit.selectClipboard(0); /* restore to windows clipboard */

          262
          MasterMaster
          262

            Jun 05, 2007#5

            It does close the new file when I test it. Although it takes a few seconds. (A FTP upload dialog shown behind the UE window). When the dialog closes ( UltraEdit.saveAs finishes) the new file is closed.

            Maybe your FTP upload takes more than a couple of seconds ?

            Or maybe a backslash in a file path is not "escaped with \" and is teasing you. For example:

            var SASFilePath = "FTP::myserver.dk\\/web/news|news1820.php";

            The green part is an escaped single backslash which is written as \\.

            7
            NewbieNewbie
            7

              Jun 05, 2007#6

              Hi Jørgen,

              I'm not writing to an FTP server. I'm simply savingAs to a directory on a Windows 2000 server. Once that directory has a new file in it with an extension of .SAS, then the SAS program kicks off. This all works quite well actually. But for some reason, the new file that was created on the server with a filename of "//mq-sas2/mqbatch/phil/test.sas" instantly pops-up in UE and never gets deleted. I'm sure you will cringe when you see how much I've mangled your original code but I'm going to provide it below in hopes that you see something very obvious.


              // Save local *.cob-file to FTP location without cob-extension in name
              // Created: Jørgen Rasmussen, 2007-05-10. Please use and modify freely.


              var myMQBatchFolder = "//mq-sas2/MqBatch/Phil/";


              saveToMyMQBatchFolder();

              function saveToMyMQBatchFolder()
              {

              /* construct a path to the sas server */

              var myMQBatchPath = myMQBatchFolder;


              /* Get name path of file with the extension */


              var path = UltraEdit.activeDocument.path;

              var SASFileName = path.substr(path.lastIndexOf("\\"));


              /* Construct full path + filename for submittal */

              SASFilePath = myMQBatchPath+SASFileName;

              UltraEdit.selectClipboard(8); /* choose user clipboard 8 */
              UltraEdit.clearClipboard();


              /* sourceDoc.save(); */ /* save local file */

              UltraEdit.activeDocument.selectAll();
              UltraEdit.activeDocument.copy();

              UltraEdit.newFile();
              UltraEdit.activeDocument.paste();
              UltraEdit.saveAs(SASFilePath);

              UltraEdit.closeFile(UltraEdit.activeDocument.path,2);

              UltraEdit.clearClipboard(8);
              UltraEdit.selectClipboard(0); /* restore to windows clipboard */

              }

              262
              MasterMaster
              262

                Jun 05, 2007#7

                First: Check out the bbcode tags:[­code][­/code] and put those around code. It makes it more readable. Thanks.

                It's past midnight here and I must admit, I simply can't see why it wont work for you. I have only access to FTP and not servers accessible as //server/path/file at home. Must try it at work tomorrow.

                I'm pretty sure the focus should lie on:

                Code: Select all

                UltraEdit.saveAs(SASFilePath);
                UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
                
                I would expect SASFilePath and UltraEdit.activeDocument.path to be the same.

                I'll get back to you - if not anybody else beat me to it ;-)

                7
                NewbieNewbie
                7

                  Jun 05, 2007#8

                  Thanks for all your help. FWIW, I get the same behavior writing the file to a local PC's directory or to a server.

                  6,686585
                  Grand MasterGrand Master
                  6,686585

                    Jun 05, 2007#9

                    The string variable SASFilePath hopefully contains backslashes instead of slashes as directory delimiter because you save the new file to a network drive not using FTP. Windows accepts also slashes as directory separator, but it is not Windows standard.

                    Have you tried: UltraEdit.closeFile(SASFilePath,2);
                    Best regards from an UC/UE/UES for Windows user from Austria

                    262
                    MasterMaster
                    262

                      Jun 05, 2007#10

                      I think Mofi nailed the problem. When using forward slashes in a Windows environment, the UltraEdit javascript API still is able to interpret and save correctly:

                      Code: Select all

                      SASFilePath = "c:/temp/temp2223.txt";
                      UltraEdit.activeDocument.selectAll();
                      UltraEdit.activeDocument.copy();
                      UltraEdit.newFile();
                      UltraEdit.activeDocument.paste();
                      
                      If we stop here and do a "Edit -> Copy file path/name" the full path is of course c:\temp\temp2223.txt

                      But when you add either

                      Code: Select all

                      UltraEdit.closeFile(SASFilePath,2);
                      
                      or

                      Code: Select all

                      UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
                      
                      Neither of them will close the active file.

                      However this will work without problems:

                      Code: Select all

                      SASFilePath = "c:\\temp\\temp2223.txt";
                      UltraEdit.activeDocument.selectAll();
                      UltraEdit.activeDocument.copy();
                      UltraEdit.newFile();
                      UltraEdit.activeDocument.paste();
                      UltraEdit.saveAs(SASFilePath);
                      UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
                      
                      (Notice the escaped backslashes \\).

                      So the conclusion must be, that the UltraEdit API gets confused when you use forward slashes for Windows files (i.e. non FTP-files).

                      So in PhilRack's case the path should be defined:

                      Code: Select all

                       var myMQBatchFolder = "\\\\mq-sas2\\MqBatch\\Phil\\";
                      So is it an error in the UltraEdit scripting API ? Well, yes and no. But a minor one. If SaveAs is able to figure it out, then CloseFile might as well. I will send an e-mail to the IDM supporters for consideration.

                      7
                      NewbieNewbie
                      7

                        Jun 05, 2007#11

                        Thank you very much! I appreciate the effort in helping me get this to work.

                        262
                        MasterMaster
                        262

                          Jun 05, 2007#12

                          Message from IDM Support: They consider it a bug and the developers will look into it.