Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
| Search | Today's Posts | Mark Forums Read
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 26th, 2008, 12:24 PM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default My batch file works, running it from VB does not

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:

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:

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?
 
Old February 26th, 2008, 01:21 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The problem is likely due to the working directory of the process. Yes, you are calling the batch file where it lives, but from the process perspective, your batch file is referencing log.exe in the working directory which is likely to be "C:\Windows\System32".

Take a look here:
http://msdn2.microsoft.com/en-us/lib...directory.aspx

-Peter
peterlanoie.blog
 
Old February 26th, 2008, 01:48 PM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks! As it turns out the current directory issue is the cause of this. If I put in a line like:

Code:
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:

Code:
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.

Thanks for your help.

 
Old February 26th, 2008, 02:35 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

You can also fully qualify the path to the EXE in the batch file. That way, the solution is self-contained within the .BAT.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Running VB.net EXE file on the server AyatKh VB.NET 5 September 23rd, 2010 01:27 AM
Running a VB 2005 exe file on a system without VB dilionyi Pro Visual Basic 2005 3 September 21st, 2009 07:37 PM
running a batch file from cmd from C# Bill_Thompson C# 2 March 29th, 2008 07:56 AM
Running my VB 2005 exe file dilionyi Visual Basic 2005 Basics 4 August 10th, 2006 10:00 AM
executing vb batch file pat933 VB How-To 0 May 14th, 2006 12:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.