当前位置:网站首页>[batch dos-cmd command - summary and summary] - environment variables, path variables, search file location related instructions - set, path, where, what if there are spaces in the path parameters of

[batch dos-cmd command - summary and summary] - environment variables, path variables, search file location related instructions - set, path, where, what if there are spaces in the path parameters of

2022-06-27 07:51:00 dssgresadfsrgre

One 、 Print help related information ——set /?

C:\Users\Administrator>set /?
 Show 、 Set or delete  cmd.exe  environment variable .

SET [variable=[string]]

  variable   Specify the environment variable name .
  string     Specifies a series of strings to assign to the variable .

 To display the current environment variables , Type... Without parameters  SET.

 If command extension is enabled ,SET  Will change as follows :

 You can activate... With only one variable  SET  command , The equal sign or value does not show all prefix matches 
SET  The value of all variables of the name used by the command . for example :

    SET P

 Will display all in letters  P  Leading variables 

 If the variable name cannot be found in the current environment ,SET  The order will put  ERRORLEVEL
 Set to  1.

SET  The command does not allow variable names to contain an equal sign .

 stay  SET  Two new command line switches have been added to the command :

    SET /A expression
    SET /P variable=[promptString]

 Please press any key to continue . . .

Two 、 Show environment variables

2.1 Show all environment variables ——set

2.2 Display the specified environment variable ——set %name%

According to specified Windows environment variable :set %name% or echo %name%. Be careful name A percent sign should be added before and after the %.

C:\Users\Administrator>echo %PATH%
C:\CUDA_win10\CUDA11.1\cuda1\bin;C:\CUDA_win10\CUDA11.1\cuda1\libnvvp;D:\Program Files\ffmpeg-4.4-essentials_build\ffmpeg-4.4-essentials_build\bin;D:\ProgramData\Anaconda3;D:\ProgramData\Anaconda3\Library\mingw-w64\bin;D:\ProgramData\Anaconda3\Library\usr\bin;D:\ProgramData\Anaconda3\Library\bin;D:\ProgramData\Anaconda3\Scripts;D:\Program Files\Python\Python38\Scripts\;D:\Program Files\Python\Python38\;D:\Program Files\python\venv38\py-cv-learning\Scripts;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;D:\Program Files\MATLAB2017b\runtime\win64;D:\Program Files\MATLAB2017b\bin;C:\Program Files\dotnet\;D:\texlive\2021\bin\win32;D:\Users\Administrator\AppData\Local\SumatraPDF;D:\Program Files\Git\cmd;C:\Program Files\NVIDIA Corporation\NVSMI;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\NVIDIA Corporation\Nsight Compute 2022.1.1\;D:\Program Files\python\python37\Scripts\;D:\Program Files\python\python37\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;;D:\Program Files\JetBrains\PyCharm Community Edition 2021.3.1\bin;;D:\Users\Administrator\AppData\Local\Programs\Microsoft VS Code\bin

C:\Users\Administrator>set %PATH%
 environment variable  C:\CUDA_win10\CUDA11.1\cuda1\bin;C:\CUDA_win10\CUDA11.1\cuda1\libnvvp;D:\Program Files\ffmpeg-4.4-essentials_build\ffmpeg-4.4-essentials_build\bin;D:\ProgramData\Anaconda3;D:\ProgramData\Anaconda3\Library\mingw-w64\bin;D:\ProgramData\Anaconda3\Library\usr\bin;D:\ProgramData\Anaconda3\Library\bin;D:\ProgramData\Anaconda3\Scripts;D:\Program Files\Python\Python38\Scripts\;D:\Program Files\Python\Python38\;D:\Program Files\python\venv38\py-cv-learning\Scripts;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;D:\Program Files\MATLAB2017b\runtime\win64;D:\Program Files\MATLAB2017b\bin;C:\Program Files\dotnet\;D:\texlive\2021\bin\win32;D:\Users\Administrator\AppData\Local\SumatraPDF;D:\Program Files\Git\cmd;C:\Program Files\NVIDIA Corporation\NVSMI;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\NVIDIA Corporation\Nsight Compute 2022.1.1\;D:\Program Files\python\python37\Scripts\;D:\Program Files\python\python37\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;;D:\Program Files\JetBrains\PyCharm Community Edition 2021.3.1\bin;;D:\Users\Administrator\AppData\Local\Programs\Microsoft VS Code\bin  No definition 

  stay Linux In the system , To display all or specify variables , Need to use export、env、echo Instructions .

  • Show all linux environment variable :export or env
  • Show / View specified linux environment variable :export name or echo name

3、 ... and 、 New environment variable ——set name=xxx

3.1 newly build Windows environment variable ——set name=constant

newly build Windows environment variable ( Valid only in the current window ):set name=constant. stay cmd Use... In the window set The variables created by the directive , Can only be used in the current window , That is to reopen another cmd This variable does not exist in the window .

3.2 Expression assignment environment variable —— Parameters /a

The result of the expression is used as the value of the environment variable :【set /a name=expression】, That is, add a parameter to the above /a.

The reason for adding parameters /a, Because if you don't add , Will be value Part of the input variable is recognized as a pure string name.

  Added parameters /a after , As shown in the figure below , Expressions are then evaluated to variables .

3.3 Manually enter environment variables ——set /p name=hint

The manually entered value is passed into the environment variable :【set /p name= Prompt information 】 , ditto , If you don't add parameters /p, This will cause the prompt message to be passed into the variable as a pure string name.

The picture above , I misused the redirection symbol >( If you don't understand , Look back 2.10 You know the ), The following file is generated .

Four 、 Clear the specified environment variable ——set %name%=

Clear the specified environment variable :【set %name%=】, There is no value after the equals sign .

5、 ... and 、path Search path management commands ——Path

5.1 Print path Command help information ——path /?

C:\Users\Administrator>path /?
 Display or set a search path for the executable .

PATH [[drive:]path[;...][;%PATH%]
PATH ;

 type  PATH ;  Clear all search path settings and indicate  cmd.exe  Only in the present 
 Search the directory for .
 type  PATH  But without parameters , Show current path .
 take  %PATH%  Including in the new path settings appends the old path to the new settings .

C:\Users\Administrator>

5.2 Print all path Search path ——path

5.3 Clean all path Search path ——path ;

perform 【path ;】 after , Then print all search paths , Found empty (null).

Empathy , This is only at present cmd Clear the window , Does not represent... Stored on disk path The search path has been changed .

Once empty ,cmd When searching for a program or command , Only the current directory will be searched .

therefore , The clear command can manipulate the search order . 

5.4 Add path Search path

stay 5.3 In this section, we deleted all search paths , Now let's try to add one .

Can execute orders 【Path new_path %PATH%】,new_path Is the absolute pathname that contains the complete drive and folder , and %PATH% Indicates that the new path is attached to the original path .

Be careful %Path% Case sensitive , Out-of-service %path%.

Empathy , The added path is also temporarily valid . 

6、 ... and 、 Search program / File location or search priority ——where

6.1 Print Where Command help information ——where /?

Directly in cmd Command line window input 【where】 Command or input 【where /?】 And then go back , It will show all and where Help information about .

6.2 Basic usage without parameters ——where pattern

Explain where Before the various parameters of the command , Let's explain without parameters , Progressive learning + Explain profound theories in simple language .

6.2.1 Single without wildcards —— File name only

where Command can be used to display the path of an executable program , Such as input where cmd enter , Will be output C:\Windows\System32\cmd.exe, Another example is input where help enter , Will be output help.exe The path of .

Output path on , Higher priority , That is, when executing instructions, the files in the top path have priority .

I used to be in pycharm Of terminal Open in powershell Environmental Science , Then enter several times where No new messages pop up , Later it was estimated that shell There is no such order in the , So make sure it's in cmd The command is running .

6.3.2 Support for wildcards ——*、?

(1) wildcard * Used to replace multiple arbitrary characters

For example, execute command 【where py*】, Not only will python.exe I found out , Will also put pylupdate、pycc This kind of document was found .

(2) wildcard ? Used in place of an arbitrary character

For example, execute command 【where pyth?n】, It can also be found python.exe Program out .

6.3.3 Support batch search ——where pattern1 pattern2

Carry out orders 【where *bat *exe】, The output result is shown in the figure below , A lot of bat Document and exe The documents were searched out .

On the left side of the command pattern Higher search priority , So the output is first bat file 、 after exe file . 

It only uses 2 individual pattern, It can actually be n individual , Ha ha ha, isn't it rich ? 

6.3 Do not display the search file list —— Parameters /Q

This parameter is the most useless parameter I have ever seen , Because after taking it , You can't output anything , The only information that can be reflected is errorlevel, I'll use it in the back echo Print out .

Carry out orders 【where /Q python】, I found that at this time where The program is “ Gao Leng pie ”( Quiet mode is turned on ,quiet), Talking to him doesn't bother you , A kind of !

not reconciled to , I'll see if I can redirect to the log file , So I followed my orders  【where /Q python>log.txt】. The situation is as follows

Last , Just when I was in complete despair , I suddenly remembered the past in my mind ——where Commands also have error feedback mechanisms , This was mentioned in the help message .

If the search is successful , This tool returns the error level 0; If it doesn't work , return 1; If it fails or an error occurs , return 2.

When I deliberately execute orders 【where /Q pytho123141】 when , I know it can't be found , That is, the error level of the unsuccessful situation 1.

 

When I deliberately execute orders 【where /Q python】 when , I know it can find , That is, the error level of success 0.

The higher the error level value , Of course, more and more wrong 、 The more “ wrong ” More brave .

therefore , This /Q Parameters are not all useless .

When in bat Script file dos On command , Maybe I will use this .

Because when you don't need to output search results , Just need to know if you can find it , Then, the execution direction of the follow-up program is determined according to whether the search can be performed , You can use this parameter .

6.4 Recursively search the files in the specified directory —— Parameters /R or /r

Because the path space problem is involved , So here are some small mistakes , Difficult to solve .

But when there is no space on the path , The implementation was very smooth !

6.4.1 There are no spaces in the path , everything “ Safe and sound ”

Carry out orders 【where /r C:\Users\Administrator\ python】

6.4.2 There are spaces on the path , Relative paths are also quoted “ good ”

The relative path is normal , Whether the quotation marks are in the part of the path or in the whole of the path .

Carry out orders 【where /r "Program Files"\python\Python38 python】

  Carry out orders 【where /r "Program Files\python\Python38" python】

6.4.3 There are spaces in the path , Absolute path with local quotation marks “ Not good. ”

Carry out orders  【where /r D:\"Program Files"\python\Python38 python】

6.4.4 There are spaces in the path , Absolute path with global Quotes “ very good ”

Carry out orders  【where /r "D:\Program Files\python\Python38" python】


 

6.4.5 There are spaces in the path , Folders are mapped to disks “ very good ”

Folders are mapped to virtual disks , Carry out orders 【subst w: "D:\Program Files"】, Created a virtual disk w.

Then execute the order 【where /r W:\python\Python38 python】 , Search successful .

6.4.6 There are spaces in the path , The blank part of the directory is substituted with variables “ Not good. ”

Set the directory variable first ( Carry out orders set dir1=Program Files), And then substitute the variables into the path ( Carry out orders where /r D:\%dir1%\python\Python38 python), And I failed .

6.4.7  There are spaces in the path , Add numbers with wavy lines “ Not good. ”

Carry out orders 【where /r d:\Progra~1\python\Python38 python】

To tell you the truth, I still don't understand the meaning of this wavy line plus numbers , But there are a lot of them on the Internet article It is suggested to use this method to avoid the error of path space problem .

7、 ... and 、 How to adjust search path priority ?where Command output path order

The order of path output represents the priority order , On behalf of cmd Command line window search instructions / Sequence of procedures , So just learn cmd What is the default search order of the command line window , And how to change these settings ok.

For details, please refer to an article I wrote ▼▼▼▼▼

How to adjust Cmd Order of command line window search ?cmd How command line windows work

For some developers , such as python developer , One computer is installed at the same time python2.x and python3.x, Now python The version opened by the command is of course a version with higher path priority . When python2.x The priority is 3.x When above , That is, using 2.x Develop in a new environment . Therefore, developers may often need to adjust their priorities , So as to complete different tasks on one computer python The development of the environment .

How to change the of environment variables Path Priority ?

The specific steps are shown in the figure below : environment variable —— System variables ——Path—— Move the corresponding path directory up .

Note that this is only the adjustment of the system variable path Search priority , User variables path Will also be searched , Therefore, if necessary, you can also enter... In the user variable Path Edit and adjust .

The priority of user variable is lower than that of system variable , In other words, first search the system variables in order Path( Don't search subdirectories , If the search is not tired ?) Is there a corresponding executable file in (exe), If not, you will go to the user variable Path Search for .

similar python -V and pip install numpy This kind of order , In fact, it is to execute first python And then execute -V,pip Empathy ; They are all subordinate python command 、pip Ordered , instead of Cmd Command below . How to get and python、pip Commands related to executable programs ? See Part III of this article for details .

8、 ... and 、CMD What if there is a space in the path of the command ? Path space problem

See... In this article for details 6.4 chapter .

Mainly use quotation marks , Second, try disk mapping , Finally, try the relative path .

原网站

版权声明
本文为[dssgresadfsrgre]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270740012834.html