簡體   English   中英

Windows命令行使用目錄和EXE文件參數

[英]Windows command line using directory and EXE file parameters

我一直在嘗試運行自己構建的應用程序並將其輸出到文件中。 但是,我遇到了執行此操作所需的命令行參數問題。

這是我使用ipconfig問題的一個示例。

以下命令有效:

ipconfig > output.txt

而這將創建文件,但不使用ipconfig輸出填充該文件:

start /D "C:\>WINDOWS\system32" 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

我正在創建一個字符數組,然后追加到它。 這里最重要的是在系統調用中使用& 這是工作作為and和第一cd'ing到該目錄執行.exe文件之前。

在您的命令中, >將重定向start的輸出,而不是ipconfig的輸出。 這就解釋了為什么您什么都看不到– start只是不輸出任何東西。

根據對問題的評論,可以使用ShellExecute這樣實現目標:

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

我認為您可能不希望使用cd來更改目錄,而是使用start

試試這個批處理文件:

cd "C:\Program Files\Tools\2012"
ip.exe >output.txt

或用於不帶批處理而僅用於命令行的情況:

"C:\Program Files\Tools\2012" ip.exe >output.txt" 

盡管system32PATH所以我不確定為什么要通過完整路徑訪問ipconfig exe,但這應該可以工作。

錯誤是這樣的:

start /D "C:\>WINDOWS\system32" ipconfig.exe > output.txt

應該

start /D "C:\WINDOWS\system32" ipconfig.exe > output.txt

路徑中沒有> 雖然C:\\>cmd.exe提示符下顯示,但它不是路徑名的一部分,並且>據我所知實際上對於此目的是無效的。

另外,我強烈建議您使用:

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

此外,由於start創建了一個新的控制台(以及新的stderrstdout ),您將捕獲start而不是ipconfig的輸出。 因此,您可能想使用:

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

但這將嘗試將output.txt寫入%SystemRoot%\\system32並且在大多數系統上都會失敗,除非您是管理員。 因此,給出一個絕對的路徑,或者干脆放棄它:

ipconfig.exe > output.txt

ipconfig.exe 始終處於默認的系統PATH變量,因此它會工作,除非管理員已經“固定”在這種情況下,你仍然可以做系統:

%SystemRoot%\system32\ipconfig.exe > output.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM