Virendra Kumar
Virendra Kumar

Reputation: 987

Redirecting dir output to my executable

I wrote a simple command line c program and made its executable. It should take file name as input and do some operations on the file.

My task is to take the input from dir /s /b *.c command and redirect it to my executable Link.exe.

I am trying it this way:

dir /s /b *.c | Link.exe

But it does not works that way.

The only way it works in if we give the file name on the right side of Link.exe file.

Upvotes: 0

Views: 277

Answers (3)

SomethingDark
SomethingDark

Reputation: 14325

Since Link.exe can take a filename as a parameter, you could run the dir command through a for loop and use the for variable as the parameter for Link.exe

for /f %%A in ('dir /b /s *.c') do Link.exe "%%A"

Note that if you're running this on the command line instead of from a batch file, you need to replace %%A with %A

Upvotes: 1

Angus Comber
Angus Comber

Reputation: 9708

You need to write Linke.exe so that it takes input from stdin. Eg in a C++ program would something like:

#include <iostream>
#include <string>

int main() {
    std::string s;
    while(std::cin >> s)
      std::cout << s << std::endl;
}

Upvotes: 1

CuriousMind
CuriousMind

Reputation: 3173

You should change it to

dir /s /b */c > Link.exe

Note in Windows, it should be angular (greater than) symbol to stream output to another program.

To test this, you can run sample test like

dir > abc.txt

The output of above statement will result into list of files present in current directory getting appended in abc.txt.

Upvotes: -1

Related Questions