Run .bat file from visual Basic command Button

Run .bat file from visual Basic command Button

Anonymous
Not applicable
12,151 Views
10 Replies
Message 1 of 11

Run .bat file from visual Basic command Button

Anonymous
Not applicable
I have a .bat file set up and rather than search through explorer I want to be able to click a button on a userform to run the batch routine. Userform and button are also set up.
0 Likes
Accepted solutions (1)
12,152 Views
10 Replies
Replies (10)
Message 2 of 11

Hallex
Advisor
Advisor

Try this code

 

Option Explicit
''
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long

''
Public Function RunCMD(ByVal FileName As String) As String

   RunCMD = ShellExecute(0, "open", FileName, "", "", 1)
   
End Function
''
Private Sub CommandButton1_Click()

Dim fname As String

fname = "C:\\Temp\\test.bat"

RunCMD fname

DoEvents

MsgBox "| Done |"

End Sub

 

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 3 of 11

Anonymous
Not applicable

hallex,

 

I tried it out but it still doesn't work.

 

As i Run the routine, click on the command button the cmd window pops and then the msgbox "DONE" but it is still not running the .bat file

 

I changed the path to the path where my .BAT is saved.

 

I have also independently checked the .bat file which defiantly works.

 

Do you have any other suggestions?

 

CJS

0 Likes
Message 4 of 11

Hallex
Advisor
Advisor

Try this project

Tested on A2009 only

Change bat file name in the code

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 5 of 11

Anonymous
Not applicable

Your coding works but i still can't get it to work on my bat routine,

 

However i think i have found the problem but can't find the solution,

 

i have tried it on 2 seperate routines.

 

  • 1 routine, the file path has no spaces - this works - gives me a TXT file listing all drawings named "CSKTransport.txt"
  • 1 routine, the file path has space - this doesn't work (the one i need)

I think the problem lies with the spaces. Here is my bat code:

 

@echo off
setlocal enabledelayedexpansion
title Drawing Lists
color 0a

cd "P:\027566\F08 - ITE\Sheet\CSK Transport\"
dir "P:\027566\F08 - ITE\Sheet\CSK Transport\"*.dwg /b /s>CSKTransport.txt
echo 
pause
msg * List Created! 

I am pretty sure the VBA part is correct as it is opening up this bat file (title "Drawing Lists" appears on DOS window)

 

any suggestions?

 

CJS

 

0 Likes
Message 6 of 11

Hallex
Advisor
Advisor
Accepted solution

I used following bat file

@echo off
setlocal enabledelayedexpansion
title Drawing Lists
color 0a

cd "C:\Test\New Folder\"
dir "C:\Test\New Folder\"*.dwg /b /s>CSKTransport.txt
echo 
pause
msg * List Created!

 

working good on my machine

I don't know how to fix your problem, sorry

Try search Google about blank spaces in the file name

or rename your folders

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 7 of 11

Anonymous
Not applicable
Hallex, Thanks for your advice. For some strange reason I tried it first thing this morning and it worked without me changing any of the code! CJS
0 Likes
Message 8 of 11

Hallex
Advisor
Advisor

I have the same things often Smiley Happy

Glad you solved it though

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 9 of 11

GTVic
Advisor
Advisor

You should probably replace the .BAT code with VB for the best results:

 

Dim s as string, t as string, i as integer
t = "P:\027566\F08 - ITE\Sheet\CSK Transport\"
i = freefile()
open t + "CSKTransport.txt" for output access write as #i
s = Dir$(t + "*.dwg",vbArchive + vbHidden + vbNormal + vbReadOnly + vbSystem)
Do While s <> ""
    print #i, t + s
    s = Dir$
Loop
close #i

 

0 Likes
Message 10 of 11

Anonymous
Not applicable

https://www.youtube.com/watch?v=1GAYtDkCG4A

watch how to execute a batch file using visual basic 6.0

0 Likes
Message 11 of 11

Anonymous
Not applicable
https://www.youtube.com/watch?v=1GAYtDkCG4A
watch tutorial on how to run a batch file (CMD) using VB6 command button
0 Likes