Hi I've developed a C# program that takes a directory list as its arguments.

What I want to do is execute a batch script that would run a command like dir /b and send its ouput to my C# program.

I believe that if I were using Linux, my script would be something like: ./MyProgram ´ls -l´

Is this possible to do this in batch script? If so, how can I do it?

Thanks,
Komyg

A batch script is a script. And a script is like any other command. So syntax is same as what you said ./myApp `/tmp/myBatchScript.sh` .

Hi Thekashyap,

As I said before I believe that something like: ./myApp `/tmp/myBatchScript.sh` is indeed the way to do it on a Linux or an Unix environment, however I am using Windows XP, so the script should be written on batch script and not bash script.

That is where my problem lies, I don't know the equivalent of the above command for a Windows shell.

Thanks,
Komyg

you could download and use cygwin then you will have the bash command installed and then ./myapp `ls -l` would work nicely

Yes, I suppose I could, however this code should be executed on my client's production environment which does not have cygwin... Is there no way to write a similar code for a Windows shell?

If neither cygwin nor PowerShell is an option, you can try to play with FOR /F . Look here for examples.

commented: Beaten ;) +18

Since you mentioned C#, I guess you're using a modern MS OS that uses cmd.exe as the command processor.

Step 1 is read and understand the "set /?" output.

Finally, support for delayed environment variable expansion has been
added.  This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE.  See CMD /?

Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed.  The following example
demonstrates the problem with immediate variable expansion:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "%VAR%" == "after" @echo If you see this, it worked
    )

would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement.  So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal.  Similarly, the following example
will not work as expected:

    set LIST=
    for %i in (*) do set LIST=%LIST% %i
    echo %LIST%

in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:

    for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time.  If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "!VAR!" == "after" @echo If you see this, it worked
    )

    set LIST=
    for %i in (*) do set LIST=!LIST! %i
    echo %LIST%

Then something like this sequence will set a variable to the output of a command.

> SET LIST=

> ls
foo.exe
bar.exe

> for /f %i in ('ls') do SET LIST=!LIST! %i

> SET LIST=!LIST! foo.exe

> SET LIST=!LIST! bar.exe

> echo %LIST%
  foo.exe bar.exe

1. There is a space at the end of the first command. Yet another of those annoyances, CMD expands a variable reference into a literal string if the variable is undefined. An empty string would have been far more useful.
2. An example command you want to collect the output of.
3. Collecting the output into a variable.
4. Passing the collected output variable to another command of your choice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.