Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

VB6 - Problem using the shell command to kick off a batch file.


I'm running the following code from a VB6 application -
it's meant to kick off a ftp batch file I've created:

Shell App.Path & "\ftp.bat", vbNormalFocus

I'm using NT 4 and when I run the VB code the DOS window appears for a split second then disappears without executing the commands inside.  The ftp batch file should take around 20 seconds to process.  

If I manually double-click the batch file it works fine.
Any ideas?
  • 2
1 Solution
rbruce5Author Commented:
I've managed to get my initial problem working.  I used:

Shell "cmd /K" & App.Path & "\ftp.bat", vbNormalFocus

However, there appears to be a long filename issue.  The content of the batch file is:

C:\winnt\system32\ftp.exe -v -s:"C:\Program Files\EPFU Packager\temp\ftp.txt"

 - it uses a text file that contains the ftp commands:

The whole thing works fine if I put my batch file and txt file in a directory with a short filename - e.g., c:\ftptest\, but as soon as I try running it from Program Files\EPFU Packager\ directory it doesn't work.

sometimes command line operationg have problems with directories containing spaces.  e.g. c:\program files\, so it is best to contain them within quotes e.g. "c:\program files\".

you do this within your bat file but not within the VB code, try something like.

Shell "cmd /K """ & App.Path & "\ftp.bat""", vbNormalFocus

Can't guarentee this is your problem without taking a closer look but is a small change and worth a try.
In fact most of the time, it's better to use small path to files. So you have to give the dos based path to the function Shell. You can use this code to retreive the short path to a file:

As you might see, this code wasn't mine but it works perfectly.

Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Public Function GetShortPath(strFileName As String) As String
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim lngRes As Long, strPath As String
    'Create a buffer
    strPath = String$(165, 0)
    'retrieve the short pathname
    lngRes = GetShortPathName(strFileName, strPath, 164)
    'remove all unnecessary chr$(0)'s
    GetShortPath = Left$(strPath, lngRes)
End Function
Private Sub Form_Load()
    MsgBox GetShortPath("c:\Program Files\")
End Sub

Hope this help.

rbruce5Author Commented:

Bingo!  Thanks for your help - it works perfectly.

Thanks to all who commented


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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