SoftTree Technologies SoftTree Technologies
Technical Support Forums
RegisterSearchFAQMemberlistUsergroupsLog in
How to use environment variable which contains backslashes?

 
Reply to topic    SoftTree Technologies Forum Index » 24x7 Scheduler, Event Server, Automation Suite View previous topic
View next topic
How to use environment variable which contains backslashes?
Author Message
barefootguru



Joined: 10 Aug 2007
Posts: 195

Post How to use environment variable which contains backslashes? Reply with quote
Hi, I'm running the multi-platform edition on Windows. In a JavaScript job I want to use a Windows environment variable which contains a path, which of course has backslashes in it.

The environment variable is HSProgs, it contains C:\Warehouse\Programs

If I run this code in a JavaScript job of course the backslashes from HSProgs are eaten, so this function:

StampFile("@V"env:HSProgs"\\sitecopy\\Logs\\sitecopy_hswebsite_@T"yyyy-mm-dd".log");

Just sees:

C:WarehousePrograms\sitecopy\Logs\sitecopy_hswebsite_2010-07-08.log

Is there a way to work around this? I realise I could move to a DOS batch file but would like to stay in JS.

Thanks
Wed Jul 07, 2010 6:31 pm View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7850

Post Reply with quote
I'm afraid there is no easy solution to preserve backslashes inside such JS job. I can suggest a slightly different solution, which is to create a batch script job to do the same job. In the code of a batch script job, use something like the following (this is just an idea, I don't know what your StampFile actually does)

Code:
FileTouch "%HSProgs%\sitecopy\Logs\sitecopy_hswebsite_@T"yyyy-mm-dd".log"


A copy of FileTouch can be downloaded here http://www.softtreetech.com/24x7/archive/47.htm
Wed Jul 07, 2010 7:33 pm View user's profile Send private message
barefootguru



Joined: 10 Aug 2007
Posts: 195

Post Reply with quote
I might just hardcode it :-)

Thanks for the prompt reply.

Can I put in a feature request for this please? A new macro variable for use in JavaScript which picks up a host environment variable and escapes it as necessary. i.e. double up backslashes and escape quotes.
Wed Jul 07, 2010 7:40 pm View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7850

Post Reply with quote
Another solution is to read the value from command line not using macros

Code:
var runInfo = Process.runAndWait("cmd /C echo %HSProgs%", 0);
//Scheduler.messageBox("output: " + runInfo.getOutput());
StampFile(runInfo.getOutput() + "\\sitecopy\\Logs\\sitecopy_hswebsite_@T"yyyy-mm-dd".log");

Wed Jul 07, 2010 9:32 pm View user's profile Send private message
barefootguru



Joined: 10 Aug 2007
Posts: 195

Post Reply with quote
Nice solution, thanks.
Wed Jul 07, 2010 9:39 pm View user's profile Send private message
barefootguru



Joined: 10 Aug 2007
Posts: 195

Post Reply with quote
I can't get this to work:

If I set HSProgs manually (second assignment) it works as expected--see first job run in log image below. If I pick up HSProgs from the environment the File.write fails--see second job run. In both cases the path written to the log looks exactly the same.

(Ignore the 2nd bunch of commented statements).

Code:
// Write timestamp to log files as sitecopy doesn't

var HSProgs = Process.runAndWait("cmd.exe /C echo %HSProgs%", 0).getOutput(); // 0=no timeout

//var HSProgs="C:\\Warehouse\\Programs";

Scheduler.logAddMessage('INFO', @V"job_id", '@V"job_name"', HSProgs);

StampFile(HSProgs+"\\sitecopy\\Logs\\sitecopy_hswebsitecbg00_@T"yyyy-mm-dd".log");

//for (i=0;i<=15;i++) {
//   var iz=''+i;
//   if (iz.length < 2) {var iz='0'+iz;};
//   StampFile("C:\\Warehouse\\Programs\\sitecopy\\Logs\\sitecopy_hswebsitecbg"+iz+"_@T"yyyy-mm-dd".log");
//}

function StampFile(Filename) {
   if (! File.exists(Filename)) {
      File.save(Filename,"");
   }
   
   var LogFile = File.open(Filename, "ReadWrite",true);  // true=append
   File.write(LogFile,"Log stamp @T"yyyy-mm-dd HH:mm:ss"\n");
   File.close(LogFile);
}



Thu Jul 08, 2010 1:00 am View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7850

Post Reply with quote
It points to line 19. Looks like File.save fails.

There is likely some leading or trailing space symbol which cannot be seen in the log and which is causing the file operation to fail.


I suggest to try trimming leading and trailing spaces from the output value returned by ECHO
Code:

// add trim() method to String class
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

StampFile(HSProgs.trim() + "\\sitecopy\\Logs\\sitecopy_hswebsitecbg00_@T"yyyy-mm-dd".log");

Thu Jul 08, 2010 8:51 am View user's profile Send private message
barefootguru



Joined: 10 Aug 2007
Posts: 195

Post Reply with quote
Brilliant, thanks. The echo command was adding a CRLF.

This is how I'm now picking up the DOS environment variable:

Code:
var HSProgs=Process.runAndWait("cmd.exe /C echo %HSProgs%", 0).getOutput(); // 0=no timeout
var HSProgs=HSProgs.replace("\r","").replace("\n",""); // Strip trailing CRLF

Thu Jul 08, 2010 5:26 pm View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7850

Post Reply with quote
Thanks for the status update. I'm glad you found an acceptable solution. Please let us know if we can help with anything else.
Thu Jul 08, 2010 7:47 pm View user's profile Send private message
Display posts from previous:    
Reply to topic    SoftTree Technologies Forum Index » 24x7 Scheduler, Event Server, Automation Suite All times are GMT - 4 Hours
Page 1 of 1

 
Jump to: 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


 

 

Powered by phpBB © 2001, 2005 phpBB Group
Design by Freestyle XL / Flowers Online.