|
SoftTree Technologies
Technical Support Forums
|
|
Author |
Message |
barefootguru
Joined: 10 Aug 2007 Posts: 195
|
|
How to use environment variable which contains backslashes? |
|
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 |
|
|
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7850
|
|
|
|
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)
|
|
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 |
|
|
barefootguru
Joined: 10 Aug 2007 Posts: 195
|
|
|
|
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 |
|
|
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7850
|
|
|
|
Another solution is to read the value from command line not using macros
|
|
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 |
|
|
barefootguru
Joined: 10 Aug 2007 Posts: 195
|
|
|
|
Nice solution, thanks.
|
|
Wed Jul 07, 2010 9:39 pm |
|
|
barefootguru
Joined: 10 Aug 2007 Posts: 195
|
|
|
|
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).
|
|
// 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 |
|
|
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7850
|
|
|
|
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
|
|
// 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 |
|
|
barefootguru
Joined: 10 Aug 2007 Posts: 195
|
|
|
|
Brilliant, thanks. The echo command was adding a CRLF.
This is how I'm now picking up the DOS environment variable:
|
|
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 |
|
|
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7850
|
|
|
|
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 |
|
|
|
|
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
|
|
|