Author Topic: I want my winbatch script to add a new directory to the path  (Read 418 times)

Garry

  • Newbie
  • *
  • Posts: 9
I want my winbatch script to add a new directory to the path
« on: September 22, 2023, 03:50:05 pm »
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.

td

  • Tech Support
  • *****
  • Posts: 4444
    • WinBatch
Re: I want my winbatch script to add a new directory to the path
« Reply #1 on: September 23, 2023, 09:05:00 am »
The Consolidated WIL Help file and corresponding Website are your friends. Check out the following links or pursue the CWH on your system.

Environment:
https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILAK_E__002.htm

EnvironSet:
https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILAK_E__003.htm

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

Staudenmaier

  • Newbie
  • *
  • Posts: 12
Re: I want my winbatch script to add a new directory to the path
« Reply #2 on: October 07, 2023, 02:11:27 am »

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.

NewFolder=""

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

;Join, or conCATenate NewFolder with Path
NewPath=Strcat(Path,";",NewFolder)

;Now set the new value for the path
EnvironSet("Path",NewPath)

exit

===============================================================

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.

NewFolder=""

; 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.

      RegKey=RegOpenKey(@REGCURRENT,"Environment")
      if RegKey==0 then goto ErrorLabel

; Now fetch the User path from the registry
      UserPath=RegQueryValue(RegKey,"[Path]")
     
; Join, or conCATenate NewFolder with Path
      NewPath=StrCat(UserPath,";",NewFolder)

;Now set the new value for the path
      RegSetValue(RegKey,"[Path]",NewPath)

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

      Exit

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,
Robert