Solved

how to add path entry in variable using javascript?

Posted on 2003-11-26
21
1,038 Views
Last Modified: 2010-05-18
How do I add to the path variable with javascript. I have this javascript:

<script Language=Javascript>
function blabla()
{
oShell = new ActiveXObject("wscript.shell");
var oFso = new ActiveXObject("Scripting.FileSystemObject");
if(oFso.FileExists("C:\\blabla\\blabla.exe"))
     {
       oShell.Run("C:\\blabla\\blabla.exe");
       window.location.href='http://somethingelse';
     }
else
     {
     alert(blabla is not installed on this computer.')
     }
}
</script>

which starts an application, but this app turns out that it needs a certain path to be added to the path variable. the problem is, I wrote an NT script at startup that adds this path, but it is adding it at every startup. I would like to add it DYNAMICALLY, ie to write it in the memory, not in the variable itself, meaning that everytime the user clicks on the link that launches this script, it gets the variable in the memory only, not in the system.
0
Comment
Question by:tonikaram
  • 9
  • 5
  • 3
  • +3
21 Comments
 
LVL 8

Expert Comment

by:xabi
ID: 9823511
Try this:

oShell.Run("set PATH = %PATH%;c:\\my_new_path");

xabi

PS: %PATH% is the actual value of PATH, so this line should add "c:\my_new_path" to the existing path.

0
 

Author Comment

by:tonikaram
ID: 9823609
it gives me the error The system cannot find the file specified
eventhough I double checked the path is correct
0
 
LVL 10

Expert Comment

by:Nushi
ID: 9823647
do you use any server side script?
0
 

Author Comment

by:tonikaram
ID: 9823672
you mean asp?

I don't use vbscript because entering vbscript in the page ruined other javascript present for drop down menus'n stuff. so I was obliged to use javascript. why, it doesn't work using javascript?
0
 
LVL 10

Expert Comment

by:Nushi
ID: 9823687
i meant asp/php/jsp .

>> why, it doesn't work using javascript?
no. you cant do it in JS.

ActiveX is not javascript.
ActiveX is a pre coded application that is embeded in the Operating system.
you can use it with JS as you do in your sample but its not JavaScript.
>>wscript.shell
this allow you to run shell commands.

if a user is blocking ActiveX you cant run your script.

regarding your question i would have try what was suggested above:
oShell.Run("set PATH = %PATH%;c:\\my_new_path");


Nushi.
0
 

Author Comment

by:tonikaram
ID: 9823700
I agree with you, but this is the intranet and I have enabled activex for all my users. That linke of code is giving an error "The system cannot find the file specified"
why??

here is the complete script. wherever I put that line it is causing me the error:

<script Language=Javascript>
function blabla()
{
oShell = new ActiveXObject("wscript.shell");
var oFso = new ActiveXObject("Scripting.FileSystemObject");
if(oFso.FileExists("C:\\blabla\\blabla.exe"))
     {
      oShell.Run("set PATH = %PATH%;c:\\my_new_path");
      oShell.Run("C:\\blabla\\blabla.exe");
      window.location.href='http://somethingelse';
     }
else
     {
     alert(blabla is not installed on this computer.')
     }
}
</script>

0
 
LVL 10

Expert Comment

by:Nushi
ID: 9823705
in first look this is my impression ( imight be wrong since i havent checked it throuly)
i guess its due to security reasons and not a problem with the file path.

but i will check it throw later on today and i iwll try to solve your problem.

Nushi.

0
 
LVL 8

Expert Comment

by:xabi
ID: 9824954
My be this way:

oShell.Run("cmd /c set PATH = %PATH%;c:\\my_new_path");


xabi
0
 
LVL 10

Expert Comment

by:Nushi
ID: 9825092
xabi
your suggestion is good.
the only problem with that is the scope of the path will be only for the cmd
once its closed you los ethe path.

i couldnt do any better then that.
its a security issue that as fas as i can tell i couldnt found any "workaround" to solve it.
if someone was able to do it he can plant a virus in someone computer ans whenever this persone
viewing HTML page (on the writer side) he can do some nustry things on the victim computer.

so i am sorry to disapoint you but your question is not posible
:-((
Nushi.
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9825544
hi,

  You can use the BuildPath method

 newpath = fso.BuildPath(path, "New   Folder");
 
example & other methods links here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsmthbuildpath.asp

Vinny
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:tonikaram
ID: 9830416
below is the script with all the methods mentionned above tested. the three commented ("//") lines below are the three methods.
- the first line gives nothing, as if the path is still not there (you can see the function definition at the end of the script).
- the second line does nothing, the cmd does run, I see a black window for a flash of a second, but the path is still not there (if I could run my app in the same black window then the path will be there I am sure)
- finally, the third line gives the error "The system cannot find the file specified".

I feel getbuildpath method is my solution, how do I call/use that funciton properly? I know I haven't in the first line commented below.

function blabla()
{
oShell = new ActiveXObject("wscript.shell");
var oFso = new ActiveXObject("Scripting.FileSystemObject");
if(oFso.FileExists("c:\\blalba\\blabla.exe"))
     {
//       GetBuildPath("C:\\mynewpath")
//       oShell.Run("cmd /c set PATH = %PATH%;C:\\mynewpath");
//       oShell.Run("set PATH = %PATH%;C:\\mynewpath");
     oShell.Run("c:\\blabla\\blabla.exe");
     }
else
     {
     alert('blabla is not installed on your computer.')
     }
}

function GetBuildPath(path)
{
   var fso, newpath;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   newpath = fso.BuildPath(path, "C:\\mynewpath");
   return(newpath);
}  




0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9832074
Hi tonikaram,

   May I ask a few questions first?  
 1) Is this going on a server or simply on the client's desktop?
 2) Why do you need to 'build a path'?  Is it that you do not know the path, or that the path changes from machine to machine?
 3) what is the final path you are looking for?
 4) Are you intent on 'finding a path', or simply locating and running an application?

Vinny
 
0
 

Author Comment

by:tonikaram
ID: 9832328
1) it is a webpage I am writing that simply launches an application. this application needs one folder to be added to the path.
2) I don't need to build a path, I just need that for this session ( the line: oShell.Run("c:\\blabla\\blabla.exe") ) the path gets one more folder added. as in a "local environment".\
3) the final path should look like: %path%;c:\pathtolocalenv (meaning the old path + the new folder)
4) I don't need to know the original path, just add
0
 

Author Comment

by:tonikaram
ID: 9835522
so, nobody has a solution? :(
0
 
LVL 10

Expert Comment

by:Nushi
ID: 9837125
>> so, nobody has a solution? :(

there isnt any soltion since it will be a security problem if there was.
imagine web site which can set any class path he need and then read/add/delete file ?
ActiveX does allow you some "workaround" in some cases but you cant be too gridy...

you should eigther ask to delete this question
or spare the points to those who spent their time for you.

:-))
Nushi.
0
 

Author Comment

by:tonikaram
ID: 9837149
ok

I have two questions though:

- what does the buildpath function do? because I am sure I didn't use/call it properly.
- the command oShell.Run("c:\\blabla\\blabla.exe") works like a charm, wonder why the oShell.Run("set PATH = %PATH%;C:\\mynewpath") doesn't? you would agree that both of them are as unsafe. don't forget that this the intranet, added to the "Trusted sites" of internet explorer for all the users, and the setting "Initialize and script ActiveX controls not marked as safe" is set to "enabled" for all the users. this is not standard web browsing, but a boosted-up intranet.

kay
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9838334
Hi kay,

  After rereading your original question, I think I have a better idea of what you are asking for -- though I am still not certain.  I think you want to do one of 2 things: either append a 'working' directory for the executable, or change the path to the executable.

  Any rate if you wish to change the path, this is what you would use (simply change somePathVar to the desired string or pass it as a var):

if(oFso.FileExists("C:\\WINDOWS/NOTEPAD.EXE"))
     {
         oldPath = "c:\\windows/notepad.exe"
         fileName = oldPath.substring(oldPath.lastIndexOf("/"))
         newPath = oFso.BuildPath(somePathVar, fileName)
         alert(newPath)

If, on the other hand, you wish to append a 'working directory', you actually want to be able to change the environment of the executable -- which should be possible through oShell.Run command.

Vinny


0
 

Author Comment

by:tonikaram
ID: 9840304
"If, on the other hand, you wish to append a 'working directory', you actually want to be able to change the environment of the executable -- which should be possible through oShell.Run command."

what I wish to do is append a directory to the path, exactly as if I am loading a local environment for that executable. how is it possible through the oShell.Run command?

I want to do this:
1- set path = c:\blabla
2- run the executable (which will find its dll's in the path above, without the path above it will not find its dll's)

kay
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10383783
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0
 

Author Comment

by:tonikaram
ID: 10412066
question may be deleted..
today, I have still not found the solution.
0
 

Accepted Solution

by:
SpazMODic earned 0 total points
ID: 10420420
PAQed, with points refunded (250)

SpazMODic
EE Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now