# How can I tell VB to run a batch file



## AMD2800 (May 3, 2005)

Hi everyone,

I am trying to write a program, that when click on a Button a batch file would run. Now this program would be in a CD rom so this batch file would be included on the CD. BUt how can I write the code that would run the batch file and know what drirectory to run it from.

Any help would be appreciated

Thanks
Jac


----------



## MyName (Oct 25, 2001)

put the batch file in the same directory as your application and on button click do:

Dim retval

retval = Shell(App.Path & "mybatch.bat", vbHide)

note:
You can choose things like VBMaxFocus or MinFocus for the vbHide if you actually want to see it run.

<EDIT>

You may/may not have to add scripting run-time to use this. Go to Project in the menu bar, choose References, and then select "Microsoft Scripting Runtime" and click OK.


----------



## AMD2800 (May 3, 2005)

Thanks for the replay. I put the code but I am getting a Run time error 53 'file not found'
What I am doing is trying to run a batch file that will run a Power Point presentation, this the the batch file:
@echo off
ppview32.exe Thermal.ppt
EXIT
everything is in the same folder. When I click the bat file it works it gives me the presentation and when finish it close just fine.

I have also a button that when click it opens explorer and I thought it would open explorer on the directory where my application is but it does not it opens on My documents this is the code for that button:
Dim fsoX As Scripting.FileSystemObject
Dim strX As String
Set fsoX = New Scripting.FileSystemObject
strX = fsoX.GetSpecialFolder(0).Path
strX = fsoX.BuildPath(strX, "\Explorer.exe")
Shell strX, vbNormalFocus

Thanks for all the help


----------



## JBotAlan (Apr 30, 2005)

AMD2800 said:


> Thanks for the replay. I put the code but I am getting a Run time error 53 'file not found'
> What I am doing is trying to run a batch file that will run a Power Point presentation, this the the batch file:
> @echo off
> ppview32.exe Thermal.ppt
> ...


'file not found' tells me that the path you are handing over to Shell() is bad. Try taking the entire expression you are passing to Shell and pass it to MsgBox(), the point being to check what you are passing. Make sure that the path is valid. The only thing I can think is that you are missing a \ on the end of the app.path, a bug in VB6, when the file is in the root. Try doing a app.path & "\whatever_the_batch_file_is_called.bat" (replacing the name, of course)

On a side note, if you are planning to share this CD you are describing with computers with no previous access to VB, then you will need to install the VB6 runtime libraries. Download from here: http://www.microsoft.com/downloads/...61-7a9c-43e7-9117-f673077ffb3c&displaylang=en

Oh, as for the explorer thing, I think you can call shell with "explorer" and it will launch. If this doesn't work, I don't know. I'm not familiar with the method you tried to use.

Good luck.


----------



## MyName (Oct 25, 2001)

JBotAlan,

Do batch files allow you to use the App.Path syntax? I was unaware you could do that, but it may be my mistake.

Another way of doing this would be to actually create the batch file during runtime using something like:

Set regstream = fso.CreateTextFile(App.Path & "\PrintAll.bat", True, False)

Then just use the regstream.write command to write whatever you want to your batch file.

regstream.write "This is what will be in the batch file"
regstream.writeblanklines(1)
regstream.write "This is the second command"
regstream.writeblanklines(1)

Also, for Explorer, just do:

Set retval2 = Shell("explorer",vbNormalFocus)


----------



## MustBNuts (Aug 21, 2003)

Actually I've done this before. The problem you will encounter is that if you run your Powerpoint on a PC that doesn't have the software, it won't work.

What you need to do it get a hold of the free Powerpoint viewer from Microsoft, put that on your CD with your presentation.

Then create an autostart file so that when you insert the CD, it will automatically open the PPT viewer.

Hope this helps,

MBN


----------



## AMD2800 (May 3, 2005)

Thanks giuys,
I got it running thansk to you all. This is what I have:

Dim errcode As Double
errcode = Shell("Runpptcc.bat", vbMinimized)

the batch file has the following:

@echo off
ppview32.exe Thermal.ppt
EXIT

The ppview32.exe is the power viewer so anyone can see the presentation.
Again thanks for all the help.
JAC


----------



## JBotAlan (Apr 30, 2005)

MyName said:


> JBotAlan,
> 
> Do batch files allow you to use the App.Path syntax? I was unaware you could do that, but it may be my mistake.


No, they don't. I meant within VB itself, the launcher program.


----------

