English 中文(简体)
使用目录和 EXE 文件参数的 Windows 命令行
原标题:Windows command line using directory and EXE file parameters

我一直在尝试运行一个应用程序, 并将其输出到文件。 然而, 我遇到一些问题, 需要命令线参数来这样做 。

这是我使用 ipconfig 解决问题的例子。

下列命令工作 :

ipconfig > output.txt

这将创建文件, 但不会以 ipconfig 输出来填充文件 :

start /D "C:>WINDOWSsystem32" ipconfig.exe > output.txt

我认为是使用 start 引发了这一问题,但我不确定。

<强 > 溶解

这就是能解决我问题的代码:

            char path[500]; // Create character array
            strcpy (path, "cd "); // Copy  cd  into the array
            strcat (path, toolLocation); // Copy the path of the tool into the array
            strcat (path, " & ip.exe > output.txt"); // Append on the name of the exe and output to a file
            system (path); // Run the built array

我正在创建一个字符阵列, 然后附加给它。 这里的关键部分是系统呼叫中使用的 < code_ amp; 。 这是作为 < code > 和 操作的, 并且在执行. exe 文件之前先使用目录的 cd ing 。

问题回答

在您的命令中, 正在改变 start 的输出方向,而不是 >ipconfig 的输出方向。这就解释了为什么你看不到任何东西 — start 根本就没有输出任何东西。

基于对问题的评论,您可以用以下的ShellExecut 实现您的目标:

ShellExecute(
    0, 
    "open", 
    "cmd.exe", 
    "/C ipconfig > output.txt", 
    NULL, 
    SW_HIDE
);

与其使用 start ,不如使用 start 来更改目录。

尝试此批量文件 :

cd "C:Program FilesTools2012"
ip.exe >output.txt

或用于没有批量和公正命令线的用途:

"C:Program FilesTools2012" ip.exe >output.txt" 

虽然 system32 PATH 中, 所以我不知道为什么您要访问 IPconfig 的完整路径, 但是这应该有效 。

错误是 :

start /D "C:>WINDOWSsystem32" ipconfig.exe > output.txt

应该是

start /D "C:WINDOWSsystem32" ipconfig.exe > output.txt

在路径中没有 ; C: > 显示在提示上 cmd.exe 显示在路径名称的 not 部分和 中,据我所知,此目的实际上无效 。

此外,我强烈建议你使用:

start /D "%SystemRoot%system32" ipconfig.exe > output.txt

此外,由于启动创建了一个新的控制台(以及新的 stderr stdout ),您正在捕捉 启动 的输出,而不是 ipconfig 的输出。 因此,您可能想要使用 :

pushd "%SystemRoot%system32" & ipconfig.exe > output.txt & popd

这将试图将 output.txt 写入 , 并将在大多数系统中失败, 除非您是管理员。 所以给出一条绝对路径, 或干脆不使用粗体 :

ipconfig.exe > output.txt

ipconfig.exe is 总是在默认系统 PATH 变量中存在 PATH 变量,所以它会有效,除非管理员已经“固定”了系统,在这种情况下你仍然可以做到:

%SystemRoot%system32ipconfig.exe > output.txt




相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...