首页 技术 正文
技术 2022年11月15日
0 收藏 864 点赞 3,537 浏览 2412 个字

转载:https://www.jb51.net/article/105325.htm

DOS批处理中%cd%与%~dp0的区别详解

  Windows下批处理中%cd%和%~dp0都能用来表示当前目录,但是他们在不同的使用场景下,功能却不相同。下面这篇文章就来给大家详细介绍了DOS批处理中%cd%与%~dp0的区别,需要的朋友可以参考借鉴。 

假设我们要在批处理a.bat里调用执行批处理b.batb.bat需要知道b.bat的当前位置,并执行run.exe,如下:

?

12345678910 // directory structure// c:// -a.bat// -program//  -b.bat//  -run.exe// a.batcall "%cd%\program\b.bat"// b.bat"%cd%\run.exe"

// directory structure
// c:
// -a.bat
// -program
// -b.bat
// -run.exe
// a.bat
call “%cd%\program\b.bat”
// b.bat
“%cd%\run.exe”

那么现在能不能成功执行run.exe呢?

问题分析

%cd%%~dp0都能用来表示当前目录,但是他们在不同的使用场景下,功能却不相同:

  • %cd%代表的是当前工作目录(current working directory,variable);
  • %~dp0代表的是当前批处理文件所在完整目录(the batch file’s directory,fixed)。

我们来看看下面的例子:

?

1234567891011 // directory structure// c:// -c.bat// -program//  -d.bat// c.batcall "%cd%\program\d.bat"// d.bat@echo offecho cd = %cd%echo dp0 = %~dp0

// directory structure
// c:
// -c.bat
// -program
// -d.bat
// c.bat
call “%cd%\program\d.bat”
// d.bat
@echo off
echo cd = %cd%
echo dp0 = %~dp0

直接运行d.bat,结果为

?

12 cd = C:\programdp0 = C:\program\

cd = C:\program
dp0 = C:\program\

直接运行c.bat,结果为

?

12 cd = C:\dp0 = C:\program\

cd = C:\
dp0 = C:\program\

从上面的结果可以看出:

  1. 执行d.bat时,当前工作目录为d.bat所在目录;
  2. 执行c.bat时,当前工作目录为c.bat所在目录,即使在调用d.bat后,该工作目录依旧是c.bat所在目录。

问题解决

让我们再来看看问题描述中提及的问题——能不能成功执行run.exe呢?

答案是:不能。“ %cd%\run.exe ”表示的是“ C:\run.exe ”,并非“ C:\program\run.exe ”。那么如何更改呢?

有两种方案:

?

1234567891011121314 // 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"

// plan A
// change the current working directory
// a.bat
cd “%~dp0”
call “%cd%\program\b.bat”
// b.bat
cd “%~dp0”
“%cd%\run.exe”
// plan B
// using %~dp0 directly
// a.bat
call “%~dp0program\b.bat”
// b.bat
“%~dp0run.exe”

问题延伸

上面的解决方案中plan A通过更改当前目录来解决该问题,可以这里面也存在另外一个问题,让我们看下面的例子:

?

1234567891011121314 // 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"

// directory structure
// c:
// -program
// -f.bat
// d:
// -e.bat
// plan A
// change the current working directory
// e.bat
cd “%~dp0”
call “c:\program\f.bat”
// f.bat
cd “%~dp0”
“%cd%\run.exe”

现在e.batf.bat不在同一个盘符了,从e.bat切换当前工作目录到f.bat直接使用cd是不行的,必须要使用:

?

1 cd /d "%~dp0"

cd /d “%~dp0”

这个地方容易疏忽,切记不要犯错。

问题总结

我们来重申下%~dp0%cd%的区别, %cd%%~dp0都能用来表示当前目录,但是他们在不同的使用场景下,功能却不相同:

  • %cd%代表的是当前工作目录(current working directory,variable);
  • %~dp0代表的是当前批处理文件所在完整目录(the batch file’s directory,fixed)。

从目前我们的使用情况来看,尽量使用%~dp0,不建议使用%cd%,有其他需求除外。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893