0

I have an .exe file from visual compaq combiler. I order to run, I have to give it from the keyboard some variables such as the path for input, a file name with parameters, path for output e.t.c

I want to write a batch file in order to run the exe many times and I don't know how to make the exe file to read the input directly from the batch file in order to avoid giving the input all the time from keeboard myself.

Many thanks

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
nikos
  • 3
  • 1
  • 3
  • http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=t – qwr Jun 10 '13 at 08:55
  • Have a look here: http://stackoverflow.com/questions/154075/using-the-dos-start-command-with-parameters-passed-to-the-started-program – Adrian Fâciu Jun 10 '13 at 08:55

3 Answers3

1

Try this:

@echo off
>input.txt echo input line 1
>>input.txt echo input line 2
>>input.txt echo input line 3
>>input.txt echo input line 4
exefile.exe <input.txt

or, using a syntax with a single redirection for the bunch of the ECHOs but all ) characters in the input lines will also need to be escaped:

@echo off
>input.txt (
echo input line 1
echo input line 2
echo input line 3
echo input line 4
)
exefile.exe <input.txt

If that doesn't work then try

type input.txt | exefile.exe

If neither of those work then your exe file doesn't accept STDIN for input.

If it does work then a batch file can help launch multiple runs.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • thanks a lot it works. I have to mention for a future use of this link that the input.txt file must contain the 'END' statement after input declaretion – nikos Jun 10 '13 at 10:58
  • The input file contents are completely dependent on the EXE file. END is not something that is needed unless you type it in in normal use - or are you referring to a CRLF to end the last line? – foxidrive Jun 10 '13 at 11:05
  • no I literally mean the word END as last line of the input file. otherwise my fortran can not run an returns error message.Perhaps it has to do with the version that anyone uses. – nikos Jun 10 '13 at 11:59
  • if you follow the 2nd option that foxidrive proposed only the input is required, with no quotes. I mean that's how it works in my fortran – nikos Jun 10 '13 at 16:38
  • 1
    What does `>input.txt input line 1` mean exactly? Did you perhaps intend to write `>input.txt echo line 1` instead? – Andriy M Jun 10 '13 at 18:52
  • Thanks for pointing that out guys - I added the echo commands. – foxidrive Jun 10 '13 at 22:28
0

You may get away with the pipe operators. If you type "dir > dir.txt" into the cmd line, it will re-direct, or Pipe the output to the file instead of the standard output. Similarly, you can use the other symbol '<', to Pipe input from a file, instead of the keyboard.

Here, here's a quick sample I knocked-up during the ad-break.

1. C source

#include <cstdio>

int main()
{
    int numPairs, num1, num2, result;
    int curPairIndex;

    printf("Enter number of pairs to add: ");
    scanf("%d", &numPairs);
    printf("\n");
    for (curPairIndex=0; curPairIndex<numPairs; curPairIndex++)
    {
        scanf("%d %d", &num1, &num2);
        printf("%02d. %d + %d = %d\n", curPairIndex, num1, num2, num1+num2);
    }

    return 0;
}

2. sampleInput.txt

3
3   4
10  20
100 1000

3. Sample cmd-line command

001-forumSample < sampleInput.txt > sampleOutput.txt

4. sampleOutput.txt

Enter number of pairs to add: 
00. 3 + 4 = 7
01. 10 + 20 = 30
02. 100 + 1000 = 1100
enhzflep
  • 12,927
  • 2
  • 32
  • 51
  • 1
    **Redirection** use the special characters `>` or `<`, and **pipe** the special character `|`, and they are _entirely different_ operations. – Aacini Jun 10 '13 at 16:36
  • Thanks Aacini. I'll have to brush-up on the concept of pipes and refresh my terminology on each. I appreciate your comment, cheers. – enhzflep Jun 10 '13 at 16:55
0

If your Batch file is used just to execute several times the .exe file and does not contain Batch logic, then you can directly place all the keyboard input in a file (that may have any extension, like .txt) and feed the file into cmd.exe. This trick makes the creation of the file much simpler, because it does not require multiple redirections nor echo commands. For example:

commandFile.txt:

echo off
rem Execute first run:
exefile.exe
first run input line 1
first run input line 2
END
rem Execute second run:
exefile.exe
second run input line 1
second run input line 2
END
rem Terminate the cmd.exe session
exit

To "execute" previous file, enter this:

cmd < commandFile.txt
Aacini
  • 65,180
  • 12
  • 72
  • 108