views:

2357

answers:

3

I am trying to update a VBScript (very little experience with this, I do a lot of VB.NET), that reads an FTP directory and moves certain files to a new local directory on a daily basis. I have old code that works on an FTP site that uses anonymous logins, but I now need it to access an FTP site that requires username and password.

Here is my current code -

Sub MoveNSPurolatorFile()

Dim NSPurolatorFTPSite, NSPurolatorMoveFilePath, NSPurolatorFTPFolder, NSPurolatorFTPFileName

Dim folder, files
Dim fso

set fso = CreateObject("Scripting.FileSystemObject")

NSPurolatorFTPSite="\\xxx.xxx.x.xx\"
NSPurolatorMoveFilePath = "F:\TestDirectory"
NSPurolatorFTPFolder = "TestFolder"

NSPurolatorFTPFileName =  "MAN0201.CSV"

If InStr(NSPurolatorFTPFileName, "_processed") = 0 and InStr(NSPurolatorFTPFileName, ".CSV") > 0 Then

    If fso.FolderExists(NSPurolatorFTPSite & NSPurolatorFTPFolder) Then

        If fso.FileExists(NSPurolatorFTPSite & NSPurolatorFTPFolder & NSPurolatorFTPFileName) Then

         objfile.writeline "NS Purolator File Found: " & NSPurolatorFTPSite & NSPurolatorFTPFolder & NSPurolatorFTPFileName
         fso.copyFile NSPurolatorFTPSite & NSPurolatorFTPFolder & NSPurolatorFTPFileName, NSPurolatorMoveFilePath & "\" 

        Else
         objfile.writeline "File does not exist: " & NSPurolatorFTPSite & NSPurolatorFTPFolder & NSPurolatorFTPFileName 
        End If 

    End If

End If

Next

End Sub

It says the folder does not exist, but I know it does and when I run this code against an ftp site that does not require username and password it works fine. I guess my question is - How do I pass in the username and password using VBScript to the ftp site before trying to access folders, etc?

Thanks.

A: 

How is the script being run? Manually, automatically? By a service?

Mapped-letter drives are not always available when running as a service.

Experiment with the script to ensure that it even able to see the F:\ drive, and then see what else is visible.

Michael Paulukonis
It will be run automatically, but it is not the F:\drive giving me the problem. It is the FTP site before that. The code doesn't even make it to the F:\drive part.
Since the error was "folder doesn't exist" I assumed that you were referring to the local file-system. It's not completely clear.
Michael Paulukonis
A: 

Is the FTP site accessed by a UNC path (looks like it is)? If it is just a standard FTP address then you can incorporate the username / password in the URL e.g. ftp://user:[email protected]. If it is a UNC path that you are trying to access using different credentials then the easiest way would probably be to map a drive, do the work and then unmap the drive. 2 different approaches can be found here

Macros
+1  A: 

This really is an incredibly bad way to do this. You can't just treat folders on a remote FTP site as local folders.

You really should be using InetCtrls.Inet.1

Here's an example I lifted from somewhere else that does not do what you want, but contains all the parts you need - you need to pick it apart to suit your needs.

'Option Explicit
'const progname="FTP upload script by Richard Finegold"
'const url = "ftp://ftp.myftpsite.com"
'const rdir = "mydir"
'const user = "anonymous"
'const pass = "[email protected]"

'This is an example of ftp'ing without calling the external "FTP" command
'It uses InetCtrls.Inet.1 instead
'Included is a "hint" for simple downloading

'Sources:
'http://msdn.microsoft.com/library/partbook/ipwvb5/loggingontoftpserver.htm
'http://msdn.microsoft.com/library/partbook/egvb6/addinginternettransfercontrol.htm
'http://cwashington.netreach.net/ - search on "ftp" - inspiration only!

'Insist on arguments
dim objArgs
Set objArgs = Wscript.Arguments
If 0=objArgs.Count Then
   MsgBox "No files selected for operation!", vbOkOnly + vbCritical, progname
   WScript.Quit
End If


'Force console mode - csforce.vbs (with some reorganization for efficiency)
dim i
if right(ucase(wscript.FullName),11)="WSCRIPT.EXE" then
  dim args, y
  For i = 0 to objArgs.Count - 1
    args = args + " " + objArgs(i)
  Next
  Set y = WScript.CreateObject("WScript.Shell")
  y.Run "cscript.exe " & wscript.ScriptFullName + " " + args, 1
  wscript.quit
end if


'Do actual work
dim fso, ftpo
set fso = WScript.CreateObject("Scripting.FileSystemObject")
set ftpo = WScript.CreateObject("InetCtls.Inet.1")     'Msinet.ocx
ftpo.URL = url
ftpo.UserName = user
ftpo.Password = pass
WScript.Echo "Connecting..."
ftpo.Execute , "CD " & rdir
do
'     WScript.Echo "."
     WScript.Sleep 100     'This can take a while loop while ftpo.StillExecuting


for i = 0 to objArgs.Count - 1
     dim sLFile
     sLFile = objArgs(i)

     if (fso.FileExists(sLFile)) then
          WScript.Echo "Uploading " & sLFile & " as " & FSO.GetFileName(sLFile) & " "
          ftpo.Execute , "Put " & sLFile & " " & FSO.GetFileName(sLFile)
          'ftpo.Execute , "Get " & sRemoteFile & " C:\" & sLFile
          do
          'WScript.Echo "."
               WScript.Sleep 100     'This can take a while
          loop while ftpo.StillExecuting
     else
          MsgBox Chr(34) & sLFile & Chr(34) & " does not exist!", _
           vbOkOnly, progname
     end if
next
WScript.Echo "Closing"
ftpo.Execute , "Close"
WScript.Echo "Done!"
Izzy