2010-09-13 57 views
1

我需要用vbscript來抓取ftp站點的文件,其中文件名的格式爲「vendor(date)(date)(random#).zip 「。這些文件每天更新​​,所以我需要一個正則表達式或方式來選擇服務器上的最新文件並下載它。我知道如何在本地文件系統上處理這個問題,但我不知道如何確定在遠程ftp服務器上獲得哪個文件。vbscript抓取FTP站點上的最新文件

回答

2

有趣的你發佈了這個,因爲我最近不得不刪除一個腳本來做幾乎完全一致的單詞你要求的。基本上,你真正需要做的就是讓腳本創建一個FTP命令文件,然後調用它。

使用你選擇的方法來創建一個字符串來保存你想要的任何格式的日期,我把它稱爲strDate,在我的情況下它最終成爲這個語法:headerinfo.13September10其中headerinfo是一個標準文件頭附上一個數字。

寫了一個FTP命令文件:以後你的腳本

Dim objOutStream 
Set objOutStream = objFSO.OpenTextFile(strCommandFile, ForWriting, True, TristateFalse) 
With objOutStream 
    .WriteLine "USER xxxxxx" ' USERNAME 
    .WriteLine "xxxxxxftp"  ' Password 
    .WriteLine "binary" 
    .WriteLine "prompt n" 
    .WriteLine "lcd " & strNetmonData ' FOLDER I'm changing into 
    .WriteLine "mget *." & strDate ' Get all files with today's date in it 
    .WriteLine "bye" 
    .Close 
End With 

然後你只需把它叫做:

WSHShell.Run "%comspec% /c FTP -n -s:" & strCommandFile & " " & strSite, 0, True 

哪裏strSite是要嘗試連接的網站的IP地址或名稱至。

0

以下代碼將幫助您從ftp服務器獲取最新的文件名,然後您可以下載它。

Const ForWriting = 2 

Dim objOutStream, objjFSO, objShell 

Set objFSO = CreateObject("Scripting.FileSystemObject") 

Set objOutStream = objFSO.OpenTextFile("C:\temp\temp\empty.txt", ForWriting,True) 

With objOutStream 

.WriteLine sUsername ' USERNAME 

.WriteLine sPassword  ' Password 

.WriteLine "cd /"& sRemotePath' FOLDER I'm changing into 

.WriteLine "ls -rt tmp/listing.txt"  

.WriteLine "quit" 

.Close 

End With 

Set objShell = CreateObject("WScript.Shell") 

objShell.Run "%Comspec% /c FTP -i -s:" & "C:\temp\temp\empty.txt" & " " & sSite 

wait(2) 

Set strCommand = objShell.Exec ("%Comspec% /c head -1 tmp\listing.txt") 

Set objStdOut = strCommand.StdOut 

    strFilename = objStdOut.ReadLine