批处理文件中的当前目录是什么?

发布于 2024-10-07 01:32:33 字数 90 浏览 0 评论 0原文

我想创建一些批处理文件来自动化程序。

我的问题是,当我创建批处理文件时,当前目录是什么? 它是文件所在的目录还是命令提示符中显示的目录,还是其他目录?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

鸩远一方 2024-10-14 01:32:33

在批处理文件中:

  • %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?

明媚殇 2024-10-14 01:32:33

通常是启动批处理文件的目录,但如果从快捷方式启动批处理文件,则可能会给出不同的启动目录。此外,当您在 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 using c:\dir1\dir2\batch.bat in which case, the current directory will be c:\dir3.

落花随流水 2024-10-14 01:32:33

在批处理文件中,%cd% 是当前目录最常用的命令,尽管您可以设置自己的变量:

set mypath=%cd%
echo %mypath% (where %mypath% is the current directory that the batch file is sitting in)

假设您想要打开 Myprog.exe。如果它位于同一文件夹中,您将使用以下命令:

start %mypath%\Myprog.exe

这将从当前文件夹中打开 Myprog。

另一种选择是在 C: 中创建一个名为 AutomatePrograms 的目录。然后,将文件传输到该文件夹​​,然后可以使用以下命令打开它们:

start "" "C:\AutomatePrograms\Myprog1.exe"
start "" "C:\AutomatePrograms\Myprog2.exe"
start "" "C:\AutomatePrograms\Myprog3.exe"

In a batch file, %cd% is the most commonly used command for the current directory, although you can set your own variable:

set mypath=%cd%
echo %mypath% (where %mypath% is the current directory that the batch file is sitting in)

So say you were wanting to open Myprog.exe. If it was in the same folder, you would use the command:

start %mypath%\Myprog.exe

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:

start "" "C:\AutomatePrograms\Myprog1.exe"
start "" "C:\AutomatePrograms\Myprog2.exe"
start "" "C:\AutomatePrograms\Myprog3.exe"
烟雨扶苏 2024-10-14 01:32:33

%__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)

蓝咒 2024-10-14 01:32:33

假设您正在当前目录中打开一个文件。命令是:

 start %cd%\filename.filetype

我希望我回答了你的问题。

Say you were opening a file in your current directory. The command would be:

 start %cd%\filename.filetype

I hope I answered your question.

乖乖兔^ω^ 2024-10-14 01:32:33

它是您运行命令以执行批处理文件的目录。

正如上面的答案中提到的,您可以将以下命令添加到脚本中进行验证:

> set current_dir=%cd%
> echo %current_dir%  

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:

> set current_dir=%cd%
> echo %current_dir%  
云柯 2024-10-14 01:32:33

它是启动批处理文件的目录。例如,如果您的批处理位于 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 do cd c:\dir3, then run the batch, the current directory will be c:\dir3.

百合的盛世恋 2024-10-14 01:32:33

只是我的 2 美分。
如果从放置在随身碟上的批处理文件 (Windows 7) 调用以下命令,则会失败:

%SystemRoot%\System32\xcopy.exe /e /i "%cd%Ala" "C:\KS\Ala\"

但这可以完成任务:

%SystemRoot%\System32\xcopy.exe /e /i "%~dp0Ala" "C:\KS\Ala\"

Just my 2 cents.
The following command fails if called from batch file (Windows 7) placed on a pendrive:

%SystemRoot%\System32\xcopy.exe /e /i "%cd%Ala" "C:\KS\Ala\"

But this does the job:

%SystemRoot%\System32\xcopy.exe /e /i "%~dp0Ala" "C:\KS\Ala\"
Saygoodbye 2024-10-14 01:32:33

您的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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文