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.
LVL 3
fibdevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

É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?
0
fibdevAuthor Commented:
I've tried both :(

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

Is the window closed?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Erick37Commented:
Deleting the file (from Books Online):

KILL
Deletes files from a disk.
Syntax:
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.
Remarks:
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:

http://support.microsoft.com/support/kb/articles/q161/3/03.asp
0
fibdevAuthor Commented:
Erick37,

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.
0
zacharCommented:
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)
0
mcriderCommented:
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

   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 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 _
      PROCESS_INFORMATION) As Long

   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 proc As PROCESS_INFORMATION
      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!


Cheers!

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

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

HOWTO: Get a Short Filename from a Long Filename
http://support.microsoft.com/support/kb/articles/Q175/5/12.ASP?LNG=ENG&SA=MSDN 
 

Thanks for the points! Glad I could help!


Cheers!
0
mcriderCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.