system(“c:\\sample\\startAll.bat”) 由于工作目录而无法运行?

发布于 2024-08-03 22:43:38 字数 551 浏览 4 评论 0原文

我有一个应用程序和可执行文件。我希望我的应用程序运行我的可执行文件。

可执行文件位于一个文件夹中,假设在“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 技术交流群。

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

发布评论

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

评论(4

狼性发作 2024-08-10 22:43:38

在调用 system(...) 之前调用 chdir("C:\\sample")

或者在批处理文件中放入 cd 命令

< 文件

由于您不在 C 上:批处理脚本的第一行应该是

C:
cd \sample

EDIT2

使用 Johannes 和 MattH 提出的建议,将启动一个更好版本的 BAT 现在,无论它位于哪个目录,bat 文件都可以工作,

setlocal
set BATDIR=%~dp0
cd /d %BATDIR%

因为没有硬编码路径。 SETLOCAL 用于避免运行脚本的副作用(例如更改目录或设置环境变量)

Call chdir("C:\\sample") before calling system(...)

Or put a cd command in your batch file

EDIT

Since you're not on C: the first lines of the batch script should be

C:
cd \sample

EDIT2

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%

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)

菊凝晚露 2024-08-10 22:43:38

system 函数可以采用如下所示的多个命令:

system("C: && cd \\sample && startAll.bat");

这比更改调用进程的当前工作目录更简洁,因为这可能会产生不需要的副作用。

根据您设置这些文件的方式,它可能比将cd命令硬编码到批处理文件中更简洁。

编辑:我使用如下所示的 C 程序对此进行了测试:

#include "stdafx.h"
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
    system("C: && cd \\temp && test.bat");
    return 0;
}

和一个名为 C:\temp\test.bat 的批处理文件,如下所示:

echo "Hello world" > pog

当我运行该 C 程序时( (与 c:\temp 不同的目录中),果然在 C:\temp 中出现了一个名为 pog 的文件。

The system function can take multiple commands like this:

system("C: && cd \\sample && startAll.bat");

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:

#include "stdafx.h"
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
    system("C: && cd \\temp && test.bat");
    return 0;
}

and a batch file called C:\temp\test.bat like this:

echo "Hello world" > pog

and when I run that C program (in a different directory from c:\temp), sure enough a file called pog appears in C:\temp.

荒岛晴空 2024-08-10 22:43:38

如果我只想使用相对于批处理文件的路径,我通常更喜欢让批处理文件忽略调用者的工作目录。您可以在文件开头使用以下命令来执行此操作:

SET BATDIR=%~dp0
CD %BATDIR%

或者您可以在调用外部文件时使用%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:

SET BATDIR=%~dp0
CD %BATDIR%

Or you can %BATDIR% when calling your external files.

To understand how the above works, take a look here

戏舞 2024-08-10 22:43:38

尝试使用双斜杠


system("c:\\sample\\startAll.bat");

Try with double slashes


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