Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Run FTP from vbscript  (Read 29952 times)

0 Members and 1 Guest are viewing this topic.

gsnidow

    Topic Starter


    Rookie

  • Just a guy trying to make work stuff easier
    • Experience: Beginner
    • OS: Unknown
    Run FTP from vbscript
    « on: November 22, 2013, 08:22:30 AM »
    Greetings all.  I am trying to get a vbscript to run ftp command.  If I run "ftp -s:c:\test\ftp.txt>c:\test\ftp_out.txt" (without the quotes) from the command prompt, it works like a charm.  The only thing c:\test\ftp.txt is doing is downloading dir contents to text file c:\test\ftp_out.txt.  Below are the lines in c:\test\ftp.txt (host, user, pass changed to protect the innocent)

    open hostname
    username
    password
    dir "*.txt"
    bye


     However, if I try to run from a vbscript, it only flashes the command window for a split second, then stops.  This is how I am trying to run it in vbscript...

    set WShell = CreateObject("WScript.Shell")
    FTP = WShell.Run("ftp -s:c:\test\ftp.txt>c:\test\ftp_out.txt")

    very simple, but does not behave the same as when I type directly in the command prompt.  I have tried setting a variable equal to the string between the quotes to no avail, and have tried setting various parts as variables, but no success.  I believe it must be something simple I am overlooking.  Any help would be greatly appreciated.  Thank you.

    Greg

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Run FTP from vbscript
    « Reply #1 on: November 22, 2013, 09:06:49 AM »
    A quick check on Google shows many are using Vb script to run the FTP command.
    Did you look at any of those items?
    This one looks nice.
    http://www.naterice.com/articles/51
    FTP Upload and FTP Download with VBScript
     :)
    EDIT: Why not just use one of the many free FTP clients that can run from the command line? The use of Vb Script seems rater involved. Or is the objective just to prove it can be done?

    gsnidow

      Topic Starter


      Rookie

    • Just a guy trying to make work stuff easier
      • Experience: Beginner
      • OS: Unknown
      Re: Run FTP from vbscript
      « Reply #2 on: November 22, 2013, 09:39:05 AM »
      Thank you for the reply.  I should have clarified that I am able to FTP files using VBScript, i.e. 'get', 'mget', etc with no problem.  My issue here is that when I try to direct the results of 'dir' to a local text file, something is causing it to not work.  I can take out the redirect from the shell command, and the vbscript performs as would be expected.  For example this works, and, again, the only thing c:\test\ftp.txt is doing is issue a dir command...

      set WShell = CreateObject("WScript.Shell")
      FTP = WShell.Run("ftp -s:c:\test\ftp.txt")

      Notice that I have taken out the '>c:\test\ftp_out.txt', so the dir list is only displayed on screen.  However, when I put back the '>c:\test\ftp_out.txt', so that my VBScript code ends up being...

      set WShell = CreateObject("WScript.Shell")
      FTP = WShell.Run("ftp -s:c:\test\ftp.txt>c:\test\ftp_out.txt")

      No bananas.  I would expect the dir output would be written to text file c:\test\ftp_out.txt, but it is not.  I can open a command prompt, and enter the exact contents of the shell command and it works, in that the dir output are correctly written to the text file.  I guess a broader question would be to ask why would a command, in this case "ftp -s:c:\test\ftp.txt>c:\test\ftp_out.txt", work at the command prompt, and not via a VBScript shell?

      Thank you.

      gsnidow

        Topic Starter


        Rookie

      • Just a guy trying to make work stuff easier
        • Experience: Beginner
        • OS: Unknown
        Re: Run FTP from vbscript
        « Reply #3 on: November 22, 2013, 10:34:54 AM »
        I don't know why, but adding "%comspec% /c" to the string being shelled made it work. 

        This works...
        FTP = WShell.Run("%comspec% /c ftp -s:c:\test\ftp.txt>c:\test\ftp_out.txt")


        This does not work...
        FTP = WShell.Run("ftp -s:c:\test\ftp.txt>c:\test\ftp_out.txt")

        It seems like maybe I am expicityly telling the shell to run the FTP commands with cmd.exe.  However, if I take out the redirect I don't need to include %comspec%.  Not sure why, but it works now.  Thanks.


        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Run FTP from vbscript
        « Reply #4 on: November 22, 2013, 11:30:30 AM »
        Glad you got it by yourself.
        The Shell command inside of Vb Script and some other languages is not a true call to the one command processor.  It is not re entrant. Use of comspec  is needed to explicitly them the script how to find the command interpreter.
        This might help explain what is the matter.
        COMMAND.COM, SHELL and COMSPEC
        BTW: You did not say what OS you have. The 64 bit Windows 7 can not run 16 bit programs.

        The comspec is needed if other instance of the interpreter has  not closed.
        Use comspec to execute bat files without waiting for last to complete

        Does that help any?

        gsnidow

          Topic Starter


          Rookie

        • Just a guy trying to make work stuff easier
          • Experience: Beginner
          • OS: Unknown
          Re: Run FTP from vbscript
          « Reply #5 on: November 22, 2013, 04:46:43 PM »
          Yes, it all seems a little bit less muddy now.  In fact, I had already found the first link you posted by using the search on this forum.  This is my first foray into using FTP via VBScript, so I figured I would have some kinks to work out.  And in answer to one of your previous questions, this is just to see if it can be done.  I am a SAS user, and for some reason, when I try to use the FTP methods in SAS, it just crashes, but I am able to have SAS call VBScript, which I can then use to FTP.  Kind of round about way of doing it, but it works.  Using any third party software is not an option for me, as my company uses something called "Chameleon FTP" as an add on to our 3270 emulator.  I can't even edit my environment strings without an admin allowing it.  So, for anyone not having edited their environment strings as I have done, when they enter 'FTP' at the command prompt, the Chameleon application opens up.  For some odd reason, though, if they shell a FTP command like I am doing, Chameleon does not run, and they are able to do what I am doing.  I am using 32 bit windows 7, and so far, it all seems to be working.  Anyhow, thank you for the tips.

          Greg