|
-
Nov 19th, 2001, 10:34 AM
#1
Thread Starter
Fanatic Member
Creating Batch files --> runtime
Can I creat a batch file at runtime through code?
Seahag
-
Nov 19th, 2001, 10:40 AM
#2
Sure! Simply open a file for output and print whatever you want the batch file to include.
VB Code:
Dim hFile As Integer
hFile = FreeFile
Open "C:\MyBatch.bat" For Output As #hFile
Print #hFile, "@Echo Off"
Print #hFile, "Dir c:\ /s > c:\DirList.txt"
Close #hFile
Best regards
-
Nov 19th, 2001, 10:43 AM
#3
Thread Starter
Fanatic Member
Yikes!
I will try that.. Whats the use of Hfile & freefile?
Thanks
-
Nov 19th, 2001, 10:44 AM
#4
hfile is just a varaible that holds the first avaialbe file handle which is what FreeFile returns..
-
Nov 19th, 2001, 10:47 AM
#5
When you open a file you need to assign a file handle to it.
This file handle is simply a number between 1 and 255.
The FreeFile function will give you an unused file handle so even if you could have gone with #1 it's better practise to always call FreeFile.
The hFile is just a variable that will recieve the return value from FreeFile you could have called that whatever you wanted.
-
Nov 19th, 2001, 10:50 AM
#6
Thread Starter
Fanatic Member
I see.. Thanks! alot..
Now How would I writh a pause in the batch file?
That is all
Thanks
-
Nov 19th, 2001, 10:58 AM
#7
Before closing the file simply do this:
-
Nov 19th, 2001, 11:02 AM
#8
Thread Starter
Fanatic Member
I knew that,..
Thanks everbody.
werks pretty nifty
.. ~~ now if there was just a way of erasing the batch file after it exicuted .. .
-
Nov 19th, 2001, 11:10 AM
#9
Originally posted by SeaHag
.. ~~ now if there was just a way of erasing the batch file after it exicuted .. .
There sure is.
Add the following code to a module and then use the ShellAndWait function to shell the bat file.
This function will not return before the application you shell has stoped executing.
So simply shell it and on the next line kill the file.
VB Code:
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function WaitForSingleObject _
Lib "kernel32" ( _
ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcess _
Lib "kernel32" Alias "CreateProcessA" ( _
ByVal lpApplicationName As Long, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle _
Lib "kernel32" ( _
ByVal hObject As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Private Const STARTF_USESHOWWINDOW = &H1
Public Sub ShellAndWait(ByVal sCmd As String, Optional ByVal WindowStyle As VbAppWinStyle = vbNormalFocus)
Dim udtStart As STARTUPINFO
Dim udtProc As PROCESS_INFORMATION
udtStart.cb = Len(udtStart)
udtStart.dwFlags = STARTF_USESHOWWINDOW
udtStart.wShowWindow = WindowStyle
CreateProcess 0&, sCmd, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, udtStart, udtProc
WaitForSingleObject udtProc.hProcess, INFINITE
CloseHandle udtProc.hProcess
End Sub
Best regards
-
Nov 19th, 2001, 11:16 AM
#10
Thread Starter
Fanatic Member
Neet stuff
I was expecting somthing like "erase me!"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|