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

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


当前回答

在批处理文件中,%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"

其他回答

这只是我的个人意见。 如果从放置在pendrive上的批处理文件(Windows 7)调用以下命令会失败:

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

但这是可行的:

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

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

正如上面的答案所提到的,你可以在你的脚本中添加以下命令来验证:

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

您的bat文件应该在您打开它时bat文件所在的目录中。然而,如果你想把它放到一个不同的目录,你可以使用cd[任意目录]

在批处理文件中,%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"

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

 start %cd%\filename.filetype

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