使用用户名和密码的VBScript FTP登录 [英] VBScript FTP Login with Username and Password

查看:179
本文介绍了使用用户名和密码的VBScript FTP登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新一个VBScript(很少有这方面的经验,我做了很多VB.NET),它读取一个FTP目录并每天将某些文件移动到新的本地目录。我有旧的代码在使用匿名登录的FTP站点上工作,但我现在需要它来访问需要用户名和密码的FTP站点。

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

它表示该文件夹不存在,但我知道它确实存在,并且当我对一个ftp站点运行此代码时不需要用户名和密码,它工作正常。我想我的问题是 - 如何在使用VBScript将用户名和密码传递到FTP站点之前尝试访问文件夹等?

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?

谢谢。

推荐答案

这确实是一个非常糟糕的方法。您不能仅将远程FTP站点上的文件夹视为本地文件夹。

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

您真的应该使用InetCtrls.Inet.1

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 = "myname@mymailsite.com"

'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!"

这篇关于使用用户名和密码的VBScript FTP登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆