[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

File handling

I am writing an application that creates a temporary batch file, runs the batch file and then deletes it.

The part I'm not sure of is how to delete the file when I'm done with it.

Also, If there is a way to close the command window when the batch file finishes, that would be nice.

Mainly I'm conserned with deleting the batch file, but If someone can answer both questions, I'll double the points.
  • 3
  • 3
  • 2
  • +2
1 Solution
Éric MoreauSenior .Net ConsultantCommented:
To delete the file, use the Kill statement (kill pathname).

To close the command window, have you tried inserting EXIT as the last line of your batch file?
fibdevAuthor Commented:
I've tried both :(

Unless my syntax was incorrect ... kill didn't delete my file
Éric MoreauSenior .Net ConsultantCommented:
You won't be able to kill while your window is still open!

Is the window closed?
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Deleting the file (from Books Online):

Deletes files from a disk.
Kill pathname
The required pathname argument is a string expression that specifies one or more file names to be deleted. The pathname may include the directory or folder, and the drive.
Kill supports the use of multiple-character (*) and single-character (?) wildcards to specify multiple files.
An error occurs if you try to use Kill to delete an open file.

Closing the DOS window:

"HOWTO: Close a Shelled Process When Finished Under Windows:

fibdevAuthor Commented:

the points are yours,  I was unable to get the command /c to work with this command line:

dim h
h = shell("command.com /c ftp -s:" & app.path & "\temp.bat", vbnormalfocus)

or like this
h = shell("command.com /c " & app.path & "\runftp.bat"
'with runftp.bat containing ftp -s:temp.bat

this works outside of vb but for some reason I can't iron it out.

If you want the fifty points, you can have them now, or go for 100.

If someone else answers this part of the question, I'll give you your 50 and I'll give that person fifty.
Try to unquote your path - if is your app.path  looking like c:\program files\microsoft offise etc. you should unquote them, i mean:

h = shell("command.com /c ftp -s:""" & app.path & "\temp.bat""", vbnormalfocus)
Try this:

Add the following to a module in your program:

    Public Const INFINITE = -1&

    Private Declare Function OpenProcess Lib "kernel32" (ByVal _
        dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
        ByVal dwProcessID As Long) As Long

   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

      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 CreateProcessA Lib "kernel32" (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 _

   Private Declare Function CloseHandle Lib "kernel32" _
      (ByVal hObject As Long) As Long

   Private Declare Function GetExitCodeProcess Lib "kernel32" _
      (ByVal hProcess As Long, lpExitCode As Long) As Long

   Private Const NORMAL_PRIORITY_CLASS = &H20&
  Public Function ExecCmd(cmdline$)
      Dim start As STARTUPINFO

      ' Initialize the STARTUPINFO structure:
      start.cb = Len(start)

      ' Start the shelled application:
      ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
         NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

      ' Wait for the shelled application to finish:
         ret& = WaitForSingleObject(proc.hProcess, INFINITE)
         Call GetExitCodeProcess(proc.hProcess, ret&)
         Call CloseHandle(proc.hThread)
         Call CloseHandle(proc.hProcess)
         ExecCmd = ret&
    End Function

Then you can do this in your program:

    Dim lpath As String
    lpath = App.Path
    If Not Right$(lpath, 1) = "\" Then lpath = lpath & "\"
    lpath = lpath & "runftp.bat"
    ExecCmd (Environ("COMSPEC") & " /c " & lpath)
    Kill lpath

The batch file will run, the program will wait for it to complete, then the program will delete it....

Hope this helps!


fibdevAuthor Commented:
I'm still having a problem becuase the command line needs to be converted to 8.3 format but I can make it work.  Thanks

I'm posting points for you here:
See the following microsoft KB article:

HOWTO: Get a Short Filename from a Long Filename

Thanks for the points! Glad I could help!


Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now