system(“c:\\sample\\startAll.bat”) 由于工作目录而无法运行?
我有一个应用程序和可执行文件。我希望我的应用程序运行我的可执行文件。
可执行文件位于一个文件夹中,假设在“c:\sample”中。
在此目录中,有一个调用我的 exe 的批处理文件。例如:
start a1.exe
start a2.exe
start a3.exe
让我们将其命名为 startAll.bat
并假设每个 exe 都有一个类似 a1.dat a2.dat 的数据...并且这些数据文件位于该 exe 附近。
我想通过我的应用程序调用这个批处理文件。
system("c:\\\\sample\\\\startAll.bat");
当我这样调用它时,命令找不到这些 exe。
如果我将目录名称添加到批处理文件中,则无法找到当时的数据。 我认为这是因为工作目录。
start c:\sample\a3.exe
在调用此批处理文件之前如何更改工作目录?
或者你还有什么建议吗?
I have an application and executables. I want my application to run my executables.
The executable files are in a folder, lets say in "c:\sample".
In this directory there is a batch file that calls my exe's. like:
start a1.exe
start a2.exe
start a3.exe
let's name it as startAll.bat
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");
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?
or do you suggest anything else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在调用
system(...)
之前调用chdir("C:\\sample")
或者在批处理文件中放入
cd
命令< 文件
由于您不在 C 上:批处理脚本的第一行应该是
EDIT2
使用 Johannes 和 MattH 提出的建议,将启动一个更好版本的 BAT 现在,无论它位于哪个目录,bat 文件都可以工作,
因为没有硬编码路径。 SETLOCAL 用于避免运行脚本的副作用(例如更改目录或设置环境变量)
Call
chdir("C:\\sample")
before callingsystem(...)
Or put a
cd
command in your batch fileEDIT
Since you're not on C: the first lines of the batch script should be
EDIT2
Using the suggestions made by Johannes and MattH a much better version of the BAT file would start with something like this
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)
system
函数可以采用如下所示的多个命令:这比更改调用进程的当前工作目录更简洁,因为这可能会产生不需要的副作用。
根据您设置这些文件的方式,它可能比将
cd
命令硬编码到批处理文件中更简洁。编辑:我使用如下所示的 C 程序对此进行了测试:
和一个名为
C:\temp\test.bat
的批处理文件,如下所示:当我运行该 C 程序时( (与 c:\temp 不同的目录中),果然在
C:\temp
中出现了一个名为pog
的文件。The
system
function can take multiple commands like this:That's neater than changing the current working directory of your calling process, because that can have its own unwanted side-effects.
Depending on how you set up these files, it might be neater than hard-coding a
cd
command into the batch file.Edit: I tested this with a C program like this:
and a batch file called
C:\temp\test.bat
like this:and when I run that C program (in a different directory from c:\temp), sure enough a file called
pog
appears inC:\temp
.如果我只想使用相对于批处理文件的路径,我通常更喜欢让批处理文件忽略调用者的工作目录。您可以在文件开头使用以下命令来执行此操作:
或者您可以在调用外部文件时使用%BATDIR%。
要了解上述工作原理,请查看此处
I often prefer to make my batch files ignore the working directory of the caller, if I only intend to work with paths relative to the batch file. You can do this with the following at the start of the file:
Or you can %BATDIR% when calling your external files.
To understand how the above works, take a look here
尝试使用双斜杠
Try with double slashes