VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Run .bat file from visual Basic command Button

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
CJSinfield
11404 Views, 10 Replies

Run .bat file from visual Basic command Button

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.
10 REPLIES 10
Message 2 of 11
Hallex
in reply to: CJSinfield

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
Message 3 of 11
CJSinfield
in reply to: Hallex

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

Message 4 of 11
Hallex
in reply to: CJSinfield

Try this project

Tested on A2009 only

Change bat file name in the code

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 5 of 11
CJSinfield
in reply to: Hallex

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

 

Message 6 of 11
Hallex
in reply to: CJSinfield

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
Message 7 of 11
CJSinfield
in reply to: CJSinfield

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
Message 8 of 11
Hallex
in reply to: CJSinfield

I have the same things often Smiley Happy

Glad you solved it though

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 9 of 11
GTVic
in reply to: Hallex

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

 

Message 10 of 11
casinilloronald
in reply to: Hallex

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

watch how to execute a batch file using visual basic 6.0

Message 11 of 11

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost