Can I creat a batch file at runtime through code?
Seahag
Printable View
Can I creat a batch file at runtime through code?
Seahag
Sure! Simply open a file for output and print whatever you want the batch file to include.Best regardsVB 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
I will try that.. Whats the use of Hfile & freefile?
Thanks
hfile is just a varaible that holds the first avaialbe file handle which is what FreeFile returns..
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.
I see.. Thanks! alot..
Now How would I writh a pause in the batch file?
That is all
Thanks
Before closing the file simply do this:VB Code:
Print #hFile, "Pause"
I knew that,.. :(
Thanks everbody.
werks pretty nifty
.. ~~ now if there was just a way of erasing the batch file after it exicuted .. .
There sure is.Quote:
Originally posted by SeaHag
.. ~~ now if there was just a way of erasing the batch file after it exicuted .. .
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.
Best regardsVB 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
I was expecting somthing like "erase me!"
:)