什么是batch file中的当前目录?

我想创build几个batch file来自动化一些事情。

我的问题是当我创build一个batch file。 什么是当前目录? 它是文件所在的目录,还是与cmd中显示的目录相同?

从您的batch file中:

  • %cd%会给你当前的工作目录 (variables)
  • %~dp0会给你batch file目录的完整path(静态)

通常是从中启动batch file的目录,但是如果从快捷方式启动batch file,则可以给出不同的起始目录。 另外,当你在cmd中,并且当前目录是c:\dir3 ,仍然可以使用c:\dir1\dir2\batch.bat启动batch file,在这种情况下,当前目录将是c:\dir3

在batch file中,%cd%是当前目录最常用的命令,尽pipe您可以设置自己的variables:

 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。

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

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

它是从你开始batch file的目录。 例如,如果您的batch file位于c:\dir1\dir2并且执行cd c:\dir3 ,则运行该batch file,则当前目录将为c:\dir3

假设您正在打开当前目录中的文件。 该命令将是:

  start %cd%\filename.filetype 

我希望我回答你的问题。

它是从哪里运行命令来执行batch file的目录。

正如在上面的答案中所提到的,您可以将以下命令添加到脚本中以validation:

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