批处理文件中的当前目录是什么?
我想创建一些批处理文件来自动化程序。
我的问题是,当我创建批处理文件时,当前目录是什么? 它是文件所在的目录还是命令提示符中显示的目录,还是其他目录?
I want to create a few batch files to automate a program.
My question is when I create the batch file, what is the current directory?
Is it the directory where the file is located or is it the same directory that appears in the command prompt, or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在批处理文件中:
%cd%
指的是当前工作目录 (变量)%~dp0
指批处理文件目录的完整路径(静态)%~dpnx0
和%~f0
均指批处理目录和文件名的完整路径(静态)。另请参阅:%~dp0 是什么意思,以及它是如何实现的工作?
From within your batch file:
%cd%
refers to the current working directory (variable)%~dp0
refers to the full path to the batch file's directory (static)%~dpnx0
and%~f0
both refer to the full path to the batch directory and file name (static).See also: What does %~dp0 mean, and how does it work?
它通常是启动批处理文件的目录,但如果从快捷方式启动批处理文件,则可能会给出不同的启动目录。此外,当您在 cmd 中且当前目录为
c:\dir3
时,您仍然可以使用c:\dir1\dir2\batch.bat
启动批处理文件> 在这种情况下,当前目录将为c:\dir3
。It usually is the directory from which the batch file is started, but if you start the batch file from a shortcut, a different starting directory could be given. Also, when you'r in cmd, and your current directory is
c:\dir3
, you can still start the batch file usingc:\dir1\dir2\batch.bat
in which case, the current directory will bec:\dir3
.在批处理文件中,%cd% 是当前目录最常用的命令,尽管您可以设置自己的变量:
假设您想要打开 Myprog.exe。如果它位于同一文件夹中,您将使用以下命令:
这将从当前文件夹中打开 Myprog。
另一种选择是在 C: 中创建一个名为 AutomatePrograms 的目录。然后,将文件传输到该文件夹,然后可以使用以下命令打开它们:
In a batch file, %cd% is the most commonly used command for the current directory, although you can set your own variable:
So say you were wanting to open Myprog.exe. If it was in the same folder, you would use the command:
That would open Myprog from the current folder.
The other option is to make a directory in C: called AutomatePrograms. Then, you transfer your files to that folder then you can open them using the following command:
%__CD__%
、%CD%
、%=C:%
、%~dp0
还有另一个动态变量
%__CD__%
指向当前目录,但与%CD%
不同,它末尾有一个反斜杠。如果您想将文件附加到当前目录,这会很有用。此外,
%CD%
在禁用扩展环境下不起作用,但%__CD__%
始终有效。使用
%=C:%
%=D:%
您可以访问相应驱动器的上次访问目录。如果未定义该变量,则表示您尚未在当前 cmd 会话上访问该驱动器。并且
%__APPDIR__%
扩展为运行当前脚本的可执行文件,即cmd.exe
目录。使用
%~dp0
参数 你可以得到脚本本身所在的目录(除非使用 shift 命令)%__CD__%
,%CD%
,%=C:%
,%~dp0
There's also another dynamic variable
%__CD__%
which points to the current directory but unlike%CD%
it has a backslash at the end.This can be useful if you want to append files to the current directory. Also
%CD%
does not work under disabled extensions environment ,but%__CD__%
always works.With
%=C:%
%=D:%
you can access the last accessed directory for the corresponding drive. If the variable is not defined you haven't accessed the drive on the current cmd session.And
%__APPDIR__%
expands to the executable that runs the current script a.k.a.cmd.exe
directory.And with
%~dp0
argument you can get the directory where the script itself is located (unless the shift command was used)假设您正在当前目录中打开一个文件。命令是:
我希望我回答了你的问题。
Say you were opening a file in your current directory. The command would be:
I hope I answered your question.
它是您运行命令以执行批处理文件的目录。
正如上面的答案中提到的,您可以将以下命令添加到脚本中进行验证:
It is the directory from where you run the command to execute your batch file.
As mentioned in the above answers you can add the below command to your script to verify:
它是启动批处理文件的目录。例如,如果您的批处理位于
c:\dir1\dir2
中,并且您执行cd c:\dir3
,然后运行该批处理,则当前目录将为c: \dir3
。It is the directory from where you start the batch file. E.g. if your batch is in
c:\dir1\dir2
and you docd c:\dir3
, then run the batch, the current directory will bec:\dir3
.只是我的 2 美分。
如果从放置在随身碟上的批处理文件 (Windows 7) 调用以下命令,则会失败:
但这可以完成任务:
Just my 2 cents.
The following command fails if called from batch file (Windows 7) placed on a pendrive:
But this does the job:
您的bat 文件应该位于您打开该bat 文件时所在的目录中。但是,如果您想将其放入不同的目录中,可以使用 cd [任意目录] 来实现
Your bat file should be in the directory that the bat file is/was in when you opened it. However if you want to put it into a different directory you can do so with
cd [whatever directory]