% Cd % and % ~ in Windows next batch processing ~ Dp0 can be used to represent the current directory, but they have different functions in different use cases. The following article details the % cd % and % ~ in DOS batch processing ~ The difference between dp0 and dp0 can be used for reference. % Cd % and % ~ in Windows next batch processing ~ Dp0 can be used to represent the current directory, but they have different functions in different use cases. The following article details the % cd % and % ~ in DOS batch processing ~ The difference between dp0 and dp0 can be used for reference.
Problem Description
Suppose we want to call and execute batch processing in batch processing a. batb.bat
,b.bat
Need to Knowb.bat
And executerun.exe
, As follows:
// directory structure// c:// -a.bat// -program// -b.bat// -run.exe// a.batcall "%cd%\program\b.bat"// b.bat"%cd%\run.exe"
Then, can the task be successfully executed?run.exe
What about it?
Problem analysis
%cd%
And%~dp0
Can be used to represent the current directory, but they have different functions in different use cases:
%cd%
Represents the current working directory (variable );
%~dp0
Indicates the full directory (the batch file's directory, fixed) where the current batch file is located ).
Let's take a look at the following example:
// directory structure// c:// -c.bat// -program// -d.bat// c.batcall "%cd%\program\d.bat"// d.bat@echo offecho cd = %cd%echo dp0 = %~dp0
Run directlyd.bat
, The result is
cd = C:\programdp0 = C:\program\
Run directlyc.bat
, The result is
cd = C:\dp0 = C:\program\
The preceding results show that:
Rund.bat
The current working directory isd.bat
Directory;
Runc.bat
The current working directory isc.bat
Directory.d.bat
The working directory is stillc.bat
Directory.
Problem solving
Let's take a look at the problem mentioned in the problem description-can it be successfully executed?run.exe
What about it?
The answer is: No. "%cd%\run.exe
"Indicates"C:\run.exe
", Not"C:\program\run.exe
". So how can we change it?
There are two solutions:
// plan A// change the current working directory// a.batcd "%~dp0"call "%cd%\program\b.bat"// b.batcd "%~dp0""%cd%\run.exe"// plan B// using %~dp0 directly// a.batcall "%~dp0program\b.bat"// b.bat"%~dp0run.exe"
Problem extension
In the above solution, plan A solves this problem by changing the current directory. another problem exists here. let's take A look at the example below:
// directory structure// c:// -program// -f.bat// d:// -e.bat// plan A// change the current working directory// e.batcd "%~dp0"call "c:\program\f.bat"// f.batcd "%~dp0""%cd%\run.exe"
Nowe.bat
Andf.bat
Not in the same drive letter.e.bat
Switch the current working directoryf.bat
You cannot use cd directly. you must use:
cd /d "%~dp0"
This place is easy to neglect. remember not to make mistakes.
Problem summary
Let's reiterate%~dp0
And%cd%
, %cd%
And%~dp0
Can be used to represent the current directory, but they have different functions in different use cases:
%cd%
Represents the current working directory (variable );
%~dp0
Indicates the full directory (the batch file's directory, fixed) where the current batch file is located ).
From the current usage, try to use%~dp0
, Not recommended%cd%
, Except for other requirements.
More % cd % and % ~ in DOS batch processing ~ For details about the differences between dp0 and dp0, please follow the PHP Chinese network!