FTP from a VB Script using the MSInet Control

12/21/08

Permalink 09:28:31 am, by dave Email , 920 words, 4288 views   English (US)
Categories: Windows Scripting, Wsh Scripts

FTP from a VB Script using the MSInet Control

I had a need to ftp a variety of files recently and while I could have thrown together a perl or batch script to do it,I decided it would be interesting to use VB Script to accomplish it. The first thing I found out, was that most folks doing this are using third party controls. I could have gone that route, but I didn't want to have to pay for a control. So I decided to try to do it using the MSInet Active X Control. This worked well for me once I registered the control on the machine that runs the script.

[More:]

This sample script uses the named command line arguments to bind to a server and retrieve a file. If the file already exists in the target location, that file will be deleted.

'==========================================================
'==     FTP using MSInet Active X Control VBScript Sample
'==
'==           Copyright © 2008, Dave Moats
'==
'== This sample is provided 'AS-IS', without any
'== express or implied warranty. In no event will the
'== authors be held liable for any damages arising from
'== the use of this sample code.
'==
'== Permission is granted to anyone to use this sample
'== code for any purpose, including commercial applications,
'== subject to the following restrictions:
'==
'== The origin of this code must not be misrepresented;
'== you must not claim that you wrote the original code.
'==
'== If you use this code, an acknowledgment in the
'== documentation is requested - shown below:
'==
'==             Portions Copyright © 2008,
'==        Dave Moats (http://www.davemoats.com/).
'==
'==========================================================

'==========================================================
'== NOTE: watch for wrapped lines and html special
'==        characters in the web representation of this
'==        sample code
'==========================================================

'==========================================================
'==
'== ftpFiles.vbs - example of how to get a file over ftp using
'==                  vbscript and the MSInet.ocx [ InetCtls.Inet.1 ]
'==
'==========================================================
option explicit

'==========================================================
'== declare the local variables to be used
'==========================================================
dim fso, ftp, namedArgs
dim scriptName, userName, userPswd, serverURL
dim remotePath, remoteFile, localPath, localFile

'==========================================================
'== get the name of the running script
'==========================================================
scriptName = wscript.scriptname

'==========================================================
'== get the named arguments
'==========================================================
set namedArgs = wscript.arguments.named

'==========================================================
'== check to see if all the required arguments were passed in
'==========================================================
if not namedArgs.exists("s") then
   wscript.echo "Usage: " & scriptName & " /s:<server url> is required"
   wscript.quit
else
   serverURL = namedArgs.item("s")
end if

if not namedArgs.exists("rp") then
   wscript.echo "Usage: " & scriptName & " /rp:<remote path> is required"
   wscript.quit
else
   remotePath = namedArgs.item("rp")
end if

if not namedArgs.exists("rf") then
   wscript.echo "Usage: " & scriptName & " /rf:<remote file> is required"
   wscript.quit
else
   remoteFile = namedArgs.item("rf")
end if

if not namedArgs.exists("lp") then
   wscript.echo "Usage: " & scriptName & " /lp:<local path> is required"
   wscript.quit
else
   localPath = namedArgs.item("lp")
end if

if not namedArgs.exists("lf") then
   wscript.echo "Usage: " & scriptName & " /lf:<local file> is required"
   wscript.quit
else
   localFile = namedArgs.item("lf")
end if

if not namedArgs.exists("u") then
   wscript.echo "Usage: " & scriptName & " /d:<user name> is required"
   wscript.quit
else
   userName = namedArgs.item("u")
end if

if not namedArgs.exists("p") then
   wscript.echo "Usage: " & scriptName & " /p:<user password> is required"
   wscript.quit
else
   userPswd = namedArgs.item("p")
end if

'==========================================================
'== now that we have our arguments, do the real work
'==========================================================
set fso = createobject("Scripting.FileSystemObject")
set ftp = createobject("InetCtls.Inet.1")

'==========================================================
'== delete the file we are going to download, if it already exists
'==========================================================
if fso.fileexists( localPath & "\" & localFile ) = true then
   fso.deletefile localPath & "\" & localFile, true
end if

'==========================================================
'== connect to the ftp server
'==========================================================
ftp.url = serverURL
ftp.username = userName
ftp.password = userPswd
wscript.echo "Connecting"
do
   wscript.echo "......"
   wscript.sleep 100
loop while ftp.StillExecuting
wscript.echo "Connected"

'==========================================================
'== cd to the target directory
'==========================================================
wscript.echo "Changing Directory"
ftp.execute , "CD " & remotePath
do
   wscript.echo "######"
   wscript.sleep 100
loop while ftp.StillExecuting
wscript.echo "Directory Changed"

'==========================================================
'== try to get the remote file
'==========================================================
wscript.echo "Getting File"
ftp.execute, "Get " & remoteFile & " " & localPath & "\" & localFile
do
   wscript.echo "++++++"
   wscript.sleep 100
loop while ftp.StillExecuting
wscript.echo "File Downloaded"

'==========================================================
'== close the ftp connection
'==========================================================
wscript.echo "Closing Connection"
ftp.execute, "Close"
do
   wscript.echo "......"
   wscript.sleep 100
loop while ftp.StillExecuting

wscript.echo "File Transfer Complete"

'==========================================================
'== clean up the object references
'==========================================================
set namedArgs = nothing
set ftp = nothing
set fso = nothing

wscript.quit

Pay close attention to the lines wrapping in this sample, the script does not have any multi-line statements.

I hope this post helped out. If it didn't, I am always looking for new scripts to add so submit a request for your question or need and I will see if I can answer it.

Dave



Did you like this post? If so, Share it!  del.icio.us digg reddit slashdot this article Facebook Twitter MySpace Email



Pingbacks:

No Pingbacks for this post yet...

This post has 37 feedbacks awaiting moderation...

Scripts

This is somewhere I can post interesting snippets as I come across them. Hopefully some folks out there will find this helpful.

Search

Follow Me:

Misc

Who's Online?

  • Guest Users: 5

powered by b2evolution free blog software