0

In my .bat file, I used the code:

setx /m path "%PATH%;%~dp0" %~dp0

to add the current directory of the .bat file to the system path.

But I can't use the cmd to run phantomjs.exe (in the same directory) until I go into my system settings and edit the path variable. I do not actually make any changes, just click edit then ok to apply the change.

What do I need to add to the script to allow me to run phantomjs.exe without the need of going into system settings?

EDIT: The laptop I'm using is a 32bit operating system and the change is not applied. I tested it on a 64bit operating system using the code setx /m path %PATH%%~dp0 and the change is applied straight away without having to open the path variable.

How can i make this work for a 32bit operating system?

  • 1
    so you have to manually run a script once to update PATH when one wishes to use phantomjs.exe? Hows that different than updating PATH manually once ? ( Just a usefulness question unless script gets auto kicked at system startup or so ) ....coming to your actual issue, was your CMD open when you ran script? Did you ran script as admin? – Sabir Khan Dec 30 '15 at 06:02
  • 1
    As noted already, you need to run as-admin for `setx /m` to work. And anyway, environment changes won't take effect within the same `cmd` session. If you want to run `phantomjs.exe (in the same directory)` from the same batch file, then just write `%~dp0phantomjs.exe`. – dxiv Dec 30 '15 at 06:45
  • @slashinsoulz Please read the answers on [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](http://stackoverflow.com/a/25919222/3074564) and [Setting path environment variable in batch file only once on Win7](http://stackoverflow.com/a/31889785/3074564) and [Batch script for set env. variable startup OC4J and remove env. variable](http://stackoverflow.com/a/34209984/3074564). The last one explains environment variable management by Windows on a simple step by step example to understand it. – Mofi Dec 30 '15 at 11:46
  • @SabirKhan you run the script which changes the path variable and closes itself. If you open new CMDs and type phantomjs it says its not recognised as if the path variable hasn't been changed. To make it take effect you have to edit the path variable manually but not actually change anything because the script has already made the change. After that if you type phantomjs into the cmd it will run. You shouldn't the have to do that because the path has already been changed. script isnt for me, it's for people who don't know how to edit the path variable. – slashinsoulz Dec 30 '15 at 12:09
  • @dxiv I have been running the script as admin and it won't take effect at all even if you start a new cmd andrestart your computer until you open the path variable in the settings and click ok for it to aplly the change the script made. After which you can run phantomjs at any time in any cmd. – slashinsoulz Dec 30 '15 at 12:14
  • @slashinsoulz Take a look on [setx](https://technet.microsoft.com/en-us/library/cc755104.aspx) documentation. `%~dp0` as fourth parameter is definitely wrong on your command line. Also is it really necessary to add batch file path to system PATH? Why do you not use `"%~dp0phantomjs.exe"` in your batch file instead of just `phantomjs.exe`? Take also a look on answer on [batch file to search and replace a string in environment variable](http://stackoverflow.com/a/24650324/3074564). – Mofi Dec 30 '15 at 12:54
  • @Mofi Just tested it and if i use `%~dp0phantomjs.exe`, when i run phantomjs in the cmd its not recognised at all, even after applying the changes. After looking at this answer: http://stackoverflow.com/a/5898184/5729228 I think the problem is the change has not been broadcasted. How do i broadcast WM_SETTINGCHANGE in the script? – slashinsoulz Dec 30 '15 at 13:19
  • Might not be that since even if i restart the change does not take effect. – slashinsoulz Dec 30 '15 at 13:31
  • `phantomjs.exe` is obviously not in directory of the batch file or it could not be found because you missed the double quotes in batch file and path contains 1 or more spaces. Please edit your question and add more relevant batch code. In which directory is `phantomjs.exe` installed? Use in batch file `echo %dp~0` and compare the output path with path of Phantom JS. Remove `@echo off` at top of your batch file and run it from within a command prompt window to see all commands also output making it possible to find errors in code. – Mofi Dec 30 '15 at 16:24
  • 1
    @Mofi For my test, I removed everything from the batch except that line of code and it worked. The phantomjs.exe is in the same directory as the batch file. If you read my edit it explains it works on 64bit. – slashinsoulz Dec 30 '15 at 23:02
  • 1
    I can't help further without much more details in question like all directory paths and real code of batch file. `setx /m path "%PATH%;%~dp0" %~dp0` is wrong because of a fourth parameter and `setx /m path %PATH%%~dp0` is also wrong if string of __PATH__ does not already end with a semicolon. And the method used to update system __PATH__ is not good at all as the referenced answers explain in detail and offering better batch code. – Mofi Dec 31 '15 at 13:38

1 Answers1

0

Use set and setx. Setx is for future windows and set for current window. Mix and match how you want.