PDA

View Full Version : Solved: Run batch file



dhananjay
12-22-2008, 06:44 AM
Hi, I am new to Vba :hi: ..

I am trying to automate a cad tool through Excel vba...

For this I need to copy a batch file to a particular location(specified by user) and run the batch file from the same location.

I am using the following code with no results..

ChDir (folder.Text)
Shell folder.Text & "\Copy of test_back_test.bat"

I think I am doing some very basic blunder here..

Please :help ....

Kenneth Hobs
12-22-2008, 07:59 AM
What is Folder.Text? If you have space characters in your path, you should probably add quotes around it.

This works if the file exists.
Shell """" & ThisWorkbook.Path & "\Copy of test_back_test.bat" & """"
You can use DIR() to verify that a file exists. If DIR(yourfilenamewithpath)="" then the file does not exist.

dhananjay
12-22-2008, 08:12 PM
Hi kenneth!! thanks for replay...

folder.Text is the value of test-box where the user enters the path...

Here is the code I tried...

ChDir (folder.Text)
Shell """" & folder.Text & "\Copy of test_back_test.bat" & """"

I didnot get a compile error.. nor the output..

When I double-click the batch file.. it runs perfectly..

Can you please solve the puzzle.. I am in the final stage of my project....

Kenneth Hobs
12-22-2008, 09:15 PM
Rather than let the user type a folder name that does not exist, you might try using a FileDialog.

You can use this to show the path in the Immediate window:
debug.print """" & folder.Text & "\Copy of test_back_test.bat" & """"
Also,
debug.print Dir("""" & folder.Text & "\Copy of test_back_test.bat" & """")
if nothing is shown in the Immediate window then the path did not exist.

GTO
12-22-2008, 09:30 PM
Greetings dhananjay,

In your first post you mention needing to copy the bat file, but not why or whether you already have that part of your code working?

As to running the .bat, I suspect you are not returning a proper value from "test-box". Which leads to the next question. What are you referring to as "test-box"? Are you referring to a TextBox on a userform, or an InputBox, or ??? If referring to a TextBox, please post an example workbook or at least the code to the userform, with an explanation as to the controls and what is supposed to happen.

Or... you could try using Application.GetOpenFilename to return the fullname of the bat file, something like:
Sub TestBat()
Dim strFName As String

'// Get the FullName (path and filename) of the desired bat file. //
strFName = Application.GetOpenFilename(FileFilter:="Bat files (*.bat), *.bat", _
Title:="Find/Select the bat file", _
MultiSelect:=False)

If Not strFName = "False" Then
Shell strFName
Else
MsgBox "No bat file was selected.", 0, ""
End If

End Sub
Hope this helps,

Mark

PS - ACK! I just noted Kenneth's response when I previewed mine. Hopefully the above is still a decent question, as the "test-box" is still confusing to me. Also as to Kenneth's excellent suggestion as to not allowing the user the capability of typing in gosh only knows what kind of string for the path, I heartily agree.

dhananjay
12-22-2008, 09:35 PM
Hi kenneth!!! thanks for the suggestion.. I have implemented it..

But I had made sure that the path does exist before I posted..
The batch file is also present in that location.. This is also confirmed!!!

When I double click on the batch file to execute it runs perfectly...
But when I use the above code nothing happens...

Also when I run the same batch file in ms-dos from 'My documents' directory nothing happens....

I tried cd to change directory in ms-dos... Is there a problem with ms-dos or the problem is with ChDir.. Is there any way around this problem...

dhananjay
12-23-2008, 12:18 AM
Hi guys!! I have sovled it...

The reason ChDir did not work is because the cd command in MS-DOS didnot change from one directory to another...
It the bug in ms-dos and not in vba..

Sorry for taking you time..
Please tell me how to delete this thread so that nobody gets confused...

lucas
12-23-2008, 12:28 AM
No need to delete it. Someone else may run into the same problem and find this by searching the site.

Just mark it solved using the thread tools at the top of the page.