Calling a batch file from vb.net with parameters

E

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
 
E

eric.goforth

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)
 
C

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()
 
E

eric.goforth

CT said:
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
 
H

Herfried K. Wagner [MVP]

CT said:
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>)'.
 
C

Chris Dunaway

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top