问题描述
我有一个应用程序和可执行文件。我希望我的应用程序来运行我的可执行文件。
I have an application and executables. I want my application to run my executables.
可执行文件的文件夹中,让在C:\\样本之称。
The executable files are in a folder, lets say in "c:\sample".
在此目录下有一个调用我的exe文件的批处理文件。这样的:
In this directory there is a batch file that calls my exe's. like:
start a1.exe
start a2.exe
start a3.exe
让我们将其命名为startAll.bat
let's name it as startAll.bat
和假设每一个exe文件有一个数据像a1.dat a2.dat ......而这些数据文件是接近这个exe文件的。
and suppose every exe has a data like a1.dat a2.dat ... and these data files are near this exe's.
我想通过我的应用程序来调用这个批处理文件。
I want to call this batch file by my application.
system("c:\\\\sample\\\\startAll.bat");
当我把它称为这样,命令不能找到这些exe文件的。
when I call it like that, command cannot find these exe's.
如果我添加目录名的批处理文件,它无法找到数据的时间。
我认为这是因为工作目录。
if I add directory names to batch files, it can not find the data that time.I think it is because of working directory.
start c:\sample\a3.exe
我怎样才能改变工作目录之前,我把这个批处理文件?
how can I change the working directory before I call this batch file?
或你建议别的什么吗?
推荐答案
呼叫 CHDIR(C:\\\\样本)
之前调用系统(...)
或者在批处理文件中把 CD
命令
Or put a cd
command in your batch file
修改
既然你不上C:批处理脚本的第一行应该是
Since you're not on C: the first lines of the batch script should be
C:
cd \sample
EDIT2
使用约翰内斯和MattH一个更好的版本BAT文件中提出的建议将开始像这样
Using the suggestions made by Johannes and MattH a much better version of the BAT file would start with something like this
setlocal
set BATDIR=%~dp0
cd /d %BATDIR%
现在bat文件将会不管它在因为没有硬codeD路径目录的工作。 SETLOCAL用于避免从运行脚本(如改变的目录或设置环境变量)的副作用
Now the bat file will work regardless of the directory it's in as there are no hard coded paths. SETLOCAL is used to avoid side effects from running the script (like changing directory or setting environment variables)
这篇关于系统(QUOT; C:\\\\ \\\\样品与startAll.bat QUOT;)不能因为工作目录的运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!