My batch file works, running it from VB does not

Kal2219

New member
Joined
Feb 26, 2008
Messages
2
Programming Experience
1-3
Hi, I have a batch file that runs a .exe with a command line parameter. When I double-click the .bat file, it runs fine and the .exe generates the correct log file in the correct directory. However, when I use code like this:

VB.NET:
Dim p As New Process
p.StartInfo.FileName = Chr(34) & Directory.GetCurrentDirectory() & "\test.bat" & Chr(34)
p.StartInfo.UseShellExecute = True
p.Start()
or code like this:

VB.NET:
Shell(Chr(34) & Directory.GetCurrentDirectory() & "\test.bat" & Chr(34))

The result is that the .bat file says it can't find the .exe file. This ONLY happens when I try to run the batch file from VB. I can run it manually and the batch file works perfectly.

The batch file looks like this:

echo off
if exist log.exe echo "It exists!"
if not exist log.exe echo "I can't find it!"
start log.exe /log-file=..\Logs\testlog.txt
pause

When I run the batch file manually I see "It exists!" and the log file is generated. When I run it via VB code I get "I can't find it!" and an error about the system not finding the file. Any ideas what might be happening?
 
Nevermind, the problem has been fixed. As it turns out the current directory issue is the cause of this. If I put in a line like:

VB.NET:
cd C:\someDirectory\LocationOfExe

Then VB can run the batch file correctly. However, the batch file was just a workaround for another problem I was having where I could not run the log.exe directly from VB. When I changed my code to include a line like:

VB.NET:
Directory.SetCurrentDirectory(pathToExe)

I was able to run the exe from VB. When I used VB code to run a different exe in the past, I didn't have to set the current directory. For some reason it's different this time.
 
Back
Top