Daniel Flannery
Daniel Flannery

Reputation: 1178

Windows command line using directory and EXE file parameters

I have been trying to run an application I built and output it to a file. However, I am running into problems with the command line arguments required to do this.

This is an example of my problem using ipconfig.

The following command works:

ipconfig > output.txt

Whereas this will create the file, but not populate it with the ipconfig output:

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

I think it is the use of start that is causing this issue, but I'm not sure.

SOLUTION

This is the code which managed to solve the problem for me:

            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

I am creating a character array and then appending to it. The vital bit here was the & being used in the system call. This is working as an and and first cd'ing to the directory before executing the .exe file.

Upvotes: 1

Views: 2148

Answers (3)

David Heffernan
David Heffernan

Reputation: 613461

In your command, the > is redirecting the output of start rather than the output of ipconfig. That explains why you are seeing nothing – start is simply not outputting anything.

Based on the comments to the question, you can achieve your goals with ShellExecute like this:

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

Upvotes: 1

0xC0000022L
0xC0000022L

Reputation: 21319

The error is this:

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

should be

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

without > in the path. Although C:\> is shown at the prompt with cmd.exe it is not part of the path name and > is actually invalid for the purpose, to my knowledge.

Additionally I would strongly suggest you use:

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

Furthermore because start creates a new console (and new stderr and stdout) you are catching the output of start not of ipconfig. So you may wanna use:

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

but that will attempt to write output.txt into %SystemRoot%\system32 and will fail on most systems unless you are admin. So give an absolute path or simply leave out the crud:

ipconfig.exe > output.txt

ipconfig.exe is always in the default system PATH variable, so it will work unless the admin has "fixed" the system in which case you can still do:

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

Upvotes: 0

Bali C
Bali C

Reputation: 31251

Rather than using start I think you might want to use cd to change the directory.

Try this batch file:

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

Or for use without a batch and just command line:

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

Although system32 is in PATH so I'm not sure why you are accessing the ipconfig exe by it's full path, but this should work.

Upvotes: 0

Related Questions