Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 26th, 2008, 11:24 AM
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?
Reply With Quote
  #2 (permalink)  
Old February 26th, 2008, 12: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,413
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
Reply With Quote
  #3 (permalink)  
Old February 26th, 2008, 12: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.

Reply With Quote
  #4 (permalink)  
Old February 26th, 2008, 01: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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 08:26 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.