Home All Groups Group Topic Archive Search About

Calling a batch file from vb.net with parameters

Author
14 Jun 2006 10:19 PM
eric.goforth
Hello,

I have a simple batch file that I'm trying to call from a VB.NET
application:

@ECHO OFF
IF (%1)==() GOTO END
DIR %1 > MYDIR.TXT
:END
@ECHO ON

In VB.NET I can call the batch file without the sMYDir parameter:

System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory
& "saveMylist.bat ")

But when I add my parameter:

System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory
& "saveMylist.bat " & sMYDir)

I get:

"The system cannot find the file specified"

Does anyone have an idea how to work around this?  I don't want to hard
code the path in my batch file.

AppDomain.CurrentDomain.BaseDirectory is:

"C:\Documents and Settings\MyUser\My Documents\Visual Studio
2005\Projects\MyProj\bin\Debug\"

I had problems with spaces i the patch when I tried running the command
from a command prompt, so I tried changing the commandline to:

System.Diagnostics.Process.Start(ControlChars.Quote &
AppDomain.CurrentDomain.BaseDirectory & "saveMylist.bat" " &
ControlChars.Quote & " " & sMYDir)

and get the same error.

I CAN run the complete concatenated string returned by
(ControlChars.Quote & AppDomain.CurrentDomain.BaseDirectory &
"saveMylist.bat" " & ControlChars.Quote & " " & sMYDir) from the
command prompt with no errors, but it doesn't work when I call it from
System.Diagnostics.Process.Start.

Thanks,
Eric

Author
14 Jun 2006 10:22 PM
eric.goforth
eric.gofo***@gmail.com wrote:

> I had problems with spaces i the patch when I tried running the command
> from a command prompt, so I tried changing the commandline to:
>
> System.Diagnostics.Process.Start(ControlChars.Quote &
> AppDomain.CurrentDomain.BaseDirectory & "saveMylist.bat" " &
> ControlChars.Quote & " " & sMYDir)
>

I had an extra quote in there, it should be:

System.Diagnostics.Process.Start(ControlChars.Quote &
AppDomain.CurrentDomain.BaseDirectory & "saveMylist.bat" &
ControlChars.Quote & " " & sMYDir)
Author
15 Jun 2006 6:11 AM
CT
Try this:

        Dim process As New System.Diagnostics.Process
        Dim startInfo As New ProcessStartInfo( _
            ControlChars.Quote & AppDomain.CurrentDomain.BaseDirectory &
"saveMylist.bat", sMYDir)
        process.StartInfo = startInfo

        process.Start()


--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk
---------
Voodoo Programming: Things programmers do that they know shouldn't work but
they try anyway, and which sometimes actually work, such as recompiling
everything. (Karl Lehenbauer)
---------
<eric.gofo***@gmail.com> wrote in message
Show quoteHide quote
news:1150323777.702356.265970@g10g2000cwb.googlegroups.com...
>
> eric.gofo***@gmail.com wrote:
>
>> I had problems with spaces i the patch when I tried running the command
>> from a command prompt, so I tried changing the commandline to:
>>
>> System.Diagnostics.Process.Start(ControlChars.Quote &
>> AppDomain.CurrentDomain.BaseDirectory & "saveMylist.bat" " &
>> ControlChars.Quote & " " & sMYDir)
>>
>
> I had an extra quote in there, it should be:
>
> System.Diagnostics.Process.Start(ControlChars.Quote &
> AppDomain.CurrentDomain.BaseDirectory & "saveMylist.bat" &
> ControlChars.Quote & " " & sMYDir)
>
Author
15 Jun 2006 11:46 AM
eric.goforth
CT wrote:
> Try this:
>
>         Dim process As New System.Diagnostics.Process
>         Dim startInfo As New ProcessStartInfo( _
>             ControlChars.Quote & AppDomain.CurrentDomain.BaseDirectory &
> "saveMylist.bat", sMYDir)
>         process.StartInfo = startInfo
>
>         process.Start()
>
Thanks, that fixed it.

-Eric
Author
15 Jun 2006 11:47 AM
Herfried K. Wagner [MVP]
"CT" <carstent@spammersgoawayintegrasol.dk> schrieb:
>        Dim process As New System.Diagnostics.Process
>        Dim startInfo As New ProcessStartInfo( _
>            ControlChars.Quote & AppDomain.CurrentDomain.BaseDirectory &
> "saveMylist.bat", sMYDir)
>        process.StartInfo = startInfo
>
>        process.Start()

.... or 'Process.Start(<batch file>, <arguments>)'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
15 Jun 2006 5:56 AM
Cor Ligthert [MVP]
Eric,

I am not sure if I understand your question but have a look at this.

http://msdn2.microsoft.com/en-us/library/system.environment.getfolderpath.aspx

if you are using version 2005 you can as well look to this.

http://msdn2.microsoft.com/en-us/library/0b485hf7(vs.80).aspx

I hope this helps,

Cor

<eric.gofo***@gmail.com> schreef in bericht
Show quoteHide quote
news:1150323541.604303.233050@p79g2000cwp.googlegroups.com...
> Hello,
>
> I have a simple batch file that I'm trying to call from a VB.NET
> application:
>
> @ECHO OFF
> IF (%1)==() GOTO END
> DIR %1 > MYDIR.TXT
> :END
> @ECHO ON
>
> In VB.NET I can call the batch file without the sMYDir parameter:
>
> System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory
> & "saveMylist.bat ")
>
> But when I add my parameter:
>
> System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory
> & "saveMylist.bat " & sMYDir)
>
> I get:
>
> "The system cannot find the file specified"
>
> Does anyone have an idea how to work around this?  I don't want to hard
> code the path in my batch file.
>
> AppDomain.CurrentDomain.BaseDirectory is:
>
> "C:\Documents and Settings\MyUser\My Documents\Visual Studio
> 2005\Projects\MyProj\bin\Debug\"
>
> I had problems with spaces i the patch when I tried running the command
> from a command prompt, so I tried changing the commandline to:
>
> System.Diagnostics.Process.Start(ControlChars.Quote &
> AppDomain.CurrentDomain.BaseDirectory & "saveMylist.bat" " &
> ControlChars.Quote & " " & sMYDir)
>
> and get the same error.
>
> I CAN run the complete concatenated string returned by
> (ControlChars.Quote & AppDomain.CurrentDomain.BaseDirectory &
> "saveMylist.bat" " & ControlChars.Quote & " " & sMYDir) from the
> command prompt with no errors, but it doesn't work when I call it from
> System.Diagnostics.Process.Start.
>
> Thanks,
> Eric
>
Author
15 Jun 2006 1:35 PM
Chris Dunaway
eric.gofo***@gmail.com wrote:

> I have a simple batch file that I'm trying to call from a VB.NET
> application:
>
> @ECHO OFF
> IF (%1)==() GOTO END
> DIR %1 > MYDIR.TXT
> :END
> @ECHO ON

I presume that your batch file performs other processes as well but you
can duplicate this functionality using the classes in the System.IO
namespace. What is done with the output file, mydir.txt, after you have
created it?