I want my winbatch script to add a new directory to the path

Started by Garry, September 22, 2023, 03:50:05 PM

Previous topic - Next topic


I want my Winbatch script to add a new directory to the (enviroment) path upon first launch.
Is it possible to do this in Windows 10 using Winbatch 2022D?

Also, is it possible for Winbatch to make a copy of the existing path for backup?

I'm confused.


The Consolidated WIL Help file and corresponding Website are your friends. Check out the following links or pursue the CWH on your system.



Read the information carefully to avoid common mistakes when dealing with environment variables.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade


A while back, I wrote a WinBatch script to check the PATH for errors;  duplicate
and missing folders.  They could slow the system down, especially on those older,
slower machines. Neither DOS nor Windows will give you an error message if this
kind of error exists.  And you'd be surprised how often an error like this does
exist!  I'd say %80 of all machines have one.

Anyway, I never finished the script, but it was no skin off my back to copy
and paste this code for you.  This is not exactly a gift horse, but its a valuable
skill to be able to take someone else's code and modify it to your needs.


; Read the PATH into a Winbatch variable
Pause("Hit OK if Path is below:",Path)

;Join, or conCATenate NewFolder with Path

;Now set the new value for the path



There's another, or second, way to do this.  Accessing the registry is more
complicated, but a bit more versatile. For instance, you should be able to access
the SYSTEM path as well the USER path. You use a different constant as a first
parameter in RegOpenKey().  I can't, however, remember what that is.


; To access the registry you have to get a "key", which is sort of
; like a handle except that you specify the target location (in the
; registry) relative to that key.

      if RegKey==0 then goto ErrorLabel

; Now fetch the User path from the registry
; Join, or conCATenate NewFolder with Path

;Now set the new value for the path

; Just like a handle, you have to CLOSE the key or your changes won't take.


When you're done testing either of these routines open a Command Prompt
and type SET to see all the Environment variables to check PATH=.

Best O'Luck,