Results 1 to 6 of 6

Thread: How to call batch file from VB6.0.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    28

    How to call batch file from VB6.0.

    Hi,

    I have created batch file (plz see bellow). This batch file copy the text file from FTP server.
    I have bellow query:
    1. This bellow batch file how to call from VB6.0 application (Not in .Net appln).
    2 .And how to copied file (from FTP) has to store in specific folder in local pc.
    3. Can i use the same batch file for SFTP server also to do the same job.

    Batchfile:

    open 2.121.121.23
    userid
    password
    lcd d:\ etc...
    get test.txt
    bye

    Many thanks in Advance
    Radhakrishnan

  2. #2

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to call batch file from VB6.0.

    Or if your file path will have spaces in it then you will need to double up on the double quotes depending on its needs or just use ShellExecute API.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    5.  
    6. Private Const SW_SHOWNORMAL As Long = 1
    7. Private Const SW_SHOWMINIMIZED As Long = 2
    8. Private Const SW_SHOWMAXIMIZED As Long = 3
    9.  
    10. Private Sub Command1_Click()
    11.     On Error GoTo MyError
    12.     Dim lRet As Long
    13.     lRet = ShellExecute(Me.hwnd, "Open", "CMD.exe", "C:\Test.bat", "C:\", SW_SHOWNORMAL)
    14.     If lRet <= 32 Then
    15.         MsgBox "Error Shelling bat file"
    16.     End If
    17.     Exit Sub
    18. MyError:
    19.     MsgBox Err.Number & " - " & Err.Description
    20. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    28

    Thumbs up Re: How to call batch file from VB6.0.

    Thanks a lot for your reply. Your code working fine.

    Can you give the direction to do the following :

    I want to create batch file from VB6.0 application. In that creating batch file i have to pass the userid, password, FTP server Ip address.

    Thank you once again..

    Radhakrishnan

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to call batch file from VB6.0.

    Using basic File I/O you can write the bat file. Its nothing more then a text file with a bat extension.

    VB Code:
    1. Open "C:\Test.bat" For Output As #1
    2.     Print #1, "Dir C:\"
    3. Close #1
    4.  
    5. '...
    6.  
    7. '/K keeps the console window open (optional)
    8. lRet = ShellExecute(Me.hwnd, "Open", "CMD.exe", " /K C:\Test.bat", "C:\", SW_SHOWNORMAL)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: How to call batch file from VB6.0.

    Quote Originally Posted by RadhakrishnanR
    Thanks a lot for your reply. Your code working fine.

    Can you give the direction to do the following :

    I want to create batch file from VB6.0 application. In that creating batch file i have to pass the userid, password, FTP server Ip address.

    Thank you once again..

    Radhakrishnan
    Something like this, maybe?

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub CreateBatch(ByVal SaveAs As String, ByVal FTPServer As String, ByVal UserID As String, ByVal Password As String)
    4.     Dim lonFF As Long, strBatch As String
    5.    
    6.     strBatch = "open " & FTPServer & vbNewLine
    7.     strBatch = strBatch & "user " & UserID & vbNewLine
    8.     strBatch = strBatch & "pass " & Password & vbNewLine
    9.    
    10.     lonFF = FreeFile
    11.    
    12.     Open SaveAs For Binary Access Write As #lonFF
    13.         Put #lonFF, , strBatch
    14.     Close #lonFF
    15.    
    16.     strBatch = ""
    17. End Sub
    18.  
    19. Private Sub cmdMakeBatch_Click()
    20.     CreateBatch "C:\batch.bat", "localhost", "userid", "password"
    21. End Sub

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