Results 1 to 10 of 10

Thread: Creating Batch files --> runtime

  1. #1

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    Creating Batch files --> runtime

    Can I creat a batch file at runtime through code?


    Seahag

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Sure! Simply open a file for output and print whatever you want the batch file to include.
    VB Code:
    1. Dim hFile As Integer
    2. hFile = FreeFile
    3. Open "C:\MyBatch.bat" For Output As #hFile
    4. Print #hFile, "@Echo Off"
    5. Print #hFile, "Dir c:\ /s > c:\DirList.txt"
    6. Close #hFile
    Best regards

  3. #3

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    Yikes!

    I will try that.. Whats the use of Hfile & freefile?

    Thanks

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    hfile is just a varaible that holds the first avaialbe file handle which is what FreeFile returns..
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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.

  6. #6

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    I see.. Thanks! alot..

    Now How would I writh a pause in the batch file?

    That is all
    Thanks

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Before closing the file simply do this:
    VB Code:
    1. Print #hFile, "Pause"

  8. #8

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    I knew that,..
    Thanks everbody.

    werks pretty nifty

    .. ~~ now if there was just a way of erasing the batch file after it exicuted .. .

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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:
    1. Private Type STARTUPINFO
    2.     cb As Long
    3.     lpReserved As String
    4.     lpDesktop As String
    5.     lpTitle As String
    6.     dwX As Long
    7.     dwY As Long
    8.     dwXSize As Long
    9.     dwYSize As Long
    10.     dwXCountChars As Long
    11.     dwYCountChars As Long
    12.     dwFillAttribute As Long
    13.     dwFlags As Long
    14.     wShowWindow As Integer
    15.     cbReserved2 As Integer
    16.     lpReserved2 As Long
    17.     hStdInput As Long
    18.     hStdOutput As Long
    19.     hStdError As Long
    20. End Type
    21.  
    22. Private Type PROCESS_INFORMATION
    23.     hProcess As Long
    24.     hThread As Long
    25.     dwProcessID As Long
    26.     dwThreadID As Long
    27. End Type
    28.  
    29. Private Declare Function WaitForSingleObject _
    30.  Lib "kernel32" ( _
    31.  ByVal hHandle As Long, _
    32.  ByVal dwMilliseconds As Long) As Long
    33.  
    34. Private Declare Function CreateProcess _
    35.  Lib "kernel32" Alias "CreateProcessA" ( _
    36.  ByVal lpApplicationName As Long, _
    37.  ByVal lpCommandLine As String, _
    38.  ByVal lpProcessAttributes As Long, _
    39.  ByVal lpThreadAttributes As Long, _
    40.  ByVal bInheritHandles As Long, _
    41.  ByVal dwCreationFlags As Long, _
    42.  ByVal lpEnvironment As Long, _
    43.  ByVal lpCurrentDirectory As Long, _
    44.  lpStartupInfo As STARTUPINFO, _
    45.  lpProcessInformation As PROCESS_INFORMATION) As Long
    46.  
    47. Private Declare Function CloseHandle _
    48.  Lib "kernel32" ( _
    49.  ByVal hObject As Long) As Long
    50.  
    51. Private Const NORMAL_PRIORITY_CLASS = &H20&
    52. Private Const INFINITE = -1&
    53. Private Const STARTF_USESHOWWINDOW = &H1
    54.  
    55. Public Sub ShellAndWait(ByVal sCmd As String, Optional ByVal WindowStyle As VbAppWinStyle = vbNormalFocus)
    56.     Dim udtStart As STARTUPINFO
    57.     Dim udtProc As PROCESS_INFORMATION
    58.    
    59.     udtStart.cb = Len(udtStart)
    60.     udtStart.dwFlags = STARTF_USESHOWWINDOW
    61.     udtStart.wShowWindow = WindowStyle
    62.     CreateProcess 0&, sCmd, 0&, 0&, 1&, _
    63.      NORMAL_PRIORITY_CLASS, 0&, 0&, udtStart, udtProc
    64.     WaitForSingleObject udtProc.hProcess, INFINITE
    65.     CloseHandle udtProc.hProcess
    66. End Sub
    Best regards

  10. #10

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    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
  •  



Click Here to Expand Forum to Full Width