5

Here is the commands in my batch file setpath.bat

set workdir="%cd%"

timeout 30

set path="C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;"

timeout 30

Each time I run this batch in cmd.exe on my windows 10, I get this error

E:\>setpath.bat

E:\>set workdir="E:\"

E:\>timeout 30

Waiting for 29 seconds, press a key to continue ...

E:\>set path="C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;"

E:\>timeout 30
'timeout' is not recognized as an internal or external command,
operable program or batch file.

I'm aware that set modifies the current shell's (the window's) environment values, and the change is available immediately, but it is temporary; while setx modifies the value permanently, which affects all future shells, but does not modify the environment of the shells already running.

I'd just like to know whether I can assign value to environment value %path% on Windows 10, so that I can run some command inside the shell immediately.

Here is another experiment with these commands

echo %path%

set workdir="%cd%"

timeout 30

set path="C:\Windows\system32;"

echo %path%

timeout 30

C:\Windows\system32\timeout 30

and this output

E:\>setpath.bat

E:\>echo E:\Miniconda3\condabin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;E:\Miniconda3;E:\Miniconda3\Scripts;E:\Miniconda3\Library\bin;C:\Users\AlbertD\AppData\Local\Microsoft\WindowsApps;e:\Program Files\JetBrains\PyCharm Community Edition 2020.3\bin
E:\Miniconda3\condabin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;E:\Miniconda3;E:\Miniconda3\Scripts;E:\Miniconda3\Library\bin;C:\Users\AlbertD\AppData\Local\Microsoft\WindowsApps;e:\Program Files\JetBrains\PyCharm Community Edition 2020.3\bin

E:\>set workdir="E:\"

E:\>timeout 30

Waiting for 30 seconds, press a key to continue ...

E:\>set path="C:\Windows\system32;"

E:\>echo "C:\Windows\system32;"
"C:\Windows\system32;"

E:\>timeout 30
'timeout' is not recognized as an internal or external command,
operable program or batch file.

E:\>C:\Windows\system32\timeout 30

Waiting for 29 seconds, press a key to continue ...

E:\>

which means the current shell can't find the timeout.exe even there is only one value for %path% variable though, calling it by full path works well.

I've done lots of other experiments, including set path="%workdir%;%path%", none of them works.

4
  • @Ramhound What I'm trying to set is exactly the same value as the original one in the system. – JJJohn Jan 7 at 11:31
  • @Ramhound nah, its the quotes. They should not be there. Path treats it literally, so it sets the first entry to "C:\windows\system32, which of couse doesn't exists. Since timeout.exe lives in c:\windows\system32, that path is excluded and thus the command isn't found. – LPChip Jan 7 at 12:47
  • @LPChip - My next suggestion would have been to use where timeout, this would have failed for the reasons you described, which would have made it clear it was the quotes. I knew something was wrong just couldn't identify it. – Ramhound Jan 7 at 14:11
  • @Ramhound yeah... I copied his script in my command prompt, and entered where timeout to come to that conclusion myself. :) – LPChip Jan 7 at 14:13
5

You have "" in your path. They should not be added.

Try

set path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;

Also, why not change path like so?

set path=C:\mydir;%path% 

that ensures that your path keeps all original path settings and only adds whatever you want to add...

0
3

You may be considering also use setlocal and endlocal

Starts localization of environment variables in a batch file. Localization continues until a matching endlocal command is encountered or the end of the batch file is reached.*

Remarks

If you use setlocal outside of a script or batch file, it has no effect. Use setlocal to change environment variables when you run a batch file. Environment changes made after you run setlocal are local to the batch file. The Cmd.exe program restores previous settings when it encounters an endlocal command or reaches the end of the batch file.

You can have more than one setlocal or endlocal command in a batch program (that is, nested commands).


  • You can also use system variables to define your "alternative" path:
 rem :: %~DP0%                     == > Drive + Path from where your bat file is
 rem :: %__CD__%
 rem :: %Windir%
 rem :: %__AppDir__%
 rem :: %ProgramFiles%
 rem :: %ProgramFiles(x86)%
 rem :: %AppData:Roaming=Local%    == > Replace Roaming to Local in: %Userprofile%\AppData\Roaming to %Userprofile%\AppData\Local

for %%i in ("%~DP0%","%__CD__%","%Windir%","%__AppDir__%","%ProgramFiles%","%ProgramFiles(x86)%","%AppData:Roaming=Local%")do echo\%%~i
  • Output/Results:
F:\Q1615715\
F:\Q1615715\
C:\WINDOWS
C:\Windows\System32\
C:\Program Files
C:\Program Files (x86)
C:\Users\ecker\AppData\Local

  • Obs.: Use "" in your set "path=....strings;strings;..." only:
                                                                  
 set "path=%__AppDir__%;%Windir%;%__AppDir__%Wbem;....\WindowsApps;"

@echo off

setlocal

set "workdir=%__CD__%"

timeout 30

set "path=%__AppDir__%;%Windir%;%__AppDir__%Wbem;%__AppDir__%WindowsPowerShell\v1.0\;%ProgramFiles(x86)%\NVIDIA Corporation\PhysX\Common;%ProgramFiles%\NVIDIA Corporation\NVIDIA NvDLISR;%AppData:Roaming=Local%\Microsoft\WindowsApps;"

echo.your alternative and current %%path%%

echo\%path:;=&echo\%

timeout 30

endlocal

echo.your system and current %%path%%

echo\%path:;=&echo\%
  • Outputs:
Waiting for  0 seconds, press a key to continue ...

your alternative and current %path%

C:\Windows\System32\
C:\WINDOWS
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR
C:\Users\ecker\AppData\Local\Microsoft\WindowsApps


Waiting for  0 seconds, press a key to continue ...

your system and current %path%

C:\Program Files\Python39\Scripts\
C:\Program Files\Python39\
C:\Python38\Scripts\
C:\Python38\
C:\Program Files\ImageMagick-7.0.10-Q16-HDRI
C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64_win\mpirt
C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\ia32_win\mpirt
C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64_win\compiler
C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\ia32_win\compiler
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0\
C:\WINDOWS\System32\OpenSSH\
C:\Program Files (x86)\QuickTime\QTSystem\
C:\Program Files\dotnet\
C:\Program Files\Microsoft SQL Server\130\Tools\Binn\
C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\
C:\Program Files\nodejs\
C:\ProgramData\chocolatey\bin
C:\Program Files\Microsoft VS Code\bin
C:\Program Files (x86)\Gpg4win\..\GnuPG\bin
C:\Program Files (x86)\Sennheiser\SenncomSDK\
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR
C:\Program Files (x86)\HP\IdrsOCR_15.2.10.1114\
C:\Program Files\Git\cmd
C:\Program Files (x86)\PDFtk\bin\
C:\Ruby30-x64\bin
C:\Users\ecker\AppData\Local\Microsoft\WindowsApps
C:\Users\ecker\AppData\Roaming\npm
C:\Users\ecker\.dotnet\tools

3
  • The path for ..\AppData\Local is stored in %localappdata% – LPChip Jan 7 at 14:12
  • @LPChip Yes, but, this part of the code intends to show the OP that can also use some substring substitution in the set command, , even if inside or outside a loop – It Wasn't Me Jan 7 at 14:17
  • @PChip It does not aim to inform all accessible variables in the system... – It Wasn't Me Jan 7 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.