加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

VBS-自定义上传或下载文件至FTP

(2011-07-06 11:15:14)
标签:

vbs

ftp

get

put

it

分类: VBS

'先通过conf.ini配置FTP相关信息,然后通过conf.ini中file的配置信息从FTP服务器上get需要下载的文件列表(即范例中的command_list.txt),通过command_list.txt中的下载或是上传所需的文件至FTP。

'conf.ini如下,其中password是通过加密程序(http://www.everbox.com/f/TEfCUuH89QSUlfLqWyjR4R99fD)加密过,加密方法:cmd下执行加密.exe,参数为明文的密码,得到的加密串粘贴在conf.ini的password处。file配置ftp上的文件命令列表

host=10.10.20.111
port=21
username=downloads
password=fnpmcm9vdDIwMTQ=
path=/lvming
file=command_list.txt

 

command_list.txt 范例如下:都是FTP命令,这里就不解析了。

cd \
lcd d:\downloads
get "[ChinaHDTV.ORG].All-Time Box Office World-wide.torrent"
put "[ChinaHDTV.ORG].Gun.Metal.Grey.EP01-29(End).720P.HDTV.X264.2Audio-StFaNs.torrent"
bye

 

代码如下:

'On Error Resume Next

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFS = CreateObject ("Scripting.FileSystemObject")
script_path=Left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))

function GetFile(strPath,contect)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strpath, 1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine, contect) Then
GetFile=strLine
end if
Loop
objFile.Close
end function

Function fDecode(sStringToDecode)
'This function will decode a Base64 encoded string and returns the decoded string.
'This becomes usefull when attempting to hide passwords from prying eyes.
Const CharList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim iDataLength, sOutputString, iGroupInitialCharacter
sStringToDecode = Replace(Replace(Replace(sStringToDecode, vbCrLf, ""), vbTab, ""), " ", "")
iDataLength = Len(sStringToDecode)
If iDataLength Mod 4 <> 0 Then
fDecode = "Bad string passed to fDecode() function."
Exit Function
End If
For iGroupInitialCharacter = 1 To iDataLength Step 4
Dim iDataByteCount, iCharacterCounter, sCharacter, iData, iGroup, sPreliminaryOutString
iDataByteCount = 3
iGroup = 0
For iCharacterCounter = 0 To 3
sCharacter = Mid(sStringToDecode, iGroupInitialCharacter + iCharacterCounter, 1)
If sCharacter = "=" Then
iDataByteCount = iDataByteCount - 1
iData = 0
Else
iData = InStr(1, CharList, sCharacter, 0) - 1
If iData = -1 Then
fDecode = "Bad string passed to fDecode() function."
Exit Function
End If
End If
iGroup = 64 * iGroup + iData
Next
iGroup = Hex(iGroup)
iGroup = String(6 - Len(iGroup), "0") & iGroup
sPreliminaryOutString = Chr(CByte("&H" & Mid(iGroup, 1, 2))) & Chr(CByte("&H" & Mid(iGroup, 3, 2))) & Chr(CByte("&H" & Mid(iGroup, 5, 2)))
sOutputString = sOutputString & Left(sPreliminaryOutString, iDataByteCount)
Next
fDecode = sOutputString
End Function


configfile="conf.ini"
host=GetFile(configfile,"host=")
port=GetFile(configfile,"port=")
username=GetFile(configfile,"username=")
password=GetFile(configfile,"password=")
path=GetFile(configfile,"path=")
file=GetFile(configfile,"file=")

ftp_host=lcase(right(host,len(host)-5))
if len(ftp_host) < 7 then
msgbox "host:"&ftp_host&" is not valid,Pls check it!"
wscript.quit
end if

ftp_port=lcase(right(port,len(port)-5))
if len(ftp_port) < 1 or cint(ftp_port) < 20 then
msgbox "port:"&ftp_port&" is not vaild,Pls check it!"
wscript.quit
end if

ftp_username=right(username,len(username)-9)
if len(ftp_username) < 1 then
msgbox "username:"&ftp_username&" is not vaild,Pls check it!"
wscript.quit
end if

ftp_password=right(password,len(password)-9)
if len(ftp_password) < 1 then
msgbox "password:"&ftp_password&" is not vaild,Pls check it!"
wscript.quit
else
ftp_password=fDecode(ftp_password)
end if

ftp_file=right(file,len(file)-5)
if len(ftp_file) < 1 then
msgbox "file:"&ftp_file&" is not vaild,Pls check it!"
wscript.quit
end if

ftp_path=right(path,len(path)-5)
if len(ftp_path) < 1 then
msgbox "path:"&ftp_path&" is not vaild,Pls check it!"
wscript.quit
end if

'msgbox ftp_host&chr(13)&ftp_port&chr(13)&ftp_username&chr(13)&ftp_password&chr(13)&ftp_file

if objFS.fileExists(ftp_file)=True then
objFS.DeleteFile(ftp_file)
end if

objFS.CreateTextFile("temp.txt")
Set objNF=objFS.OpenTextFile("temp.txt",8)
objNF.writeline "open "&ftp_host&" "&ftp_port
objNF.writeline ftp_username
objNF.writeline ftp_password
objNF.writeline "binary"
objNF.writeline "cd "&ftp_path
objNF.writeline "get "&ftp_file
objNF.writeline "bye"
objNF.close

Set objShell = CreateObject("WScript.Shell")
objShell.Run "ftp -s:temp.txt",4,True
objFS.DeleteFile("temp.txt")

Set objFS=nothing
 
Set objFS = CreateObject ("Scripting.FileSystemObject")

if objFS.fileExists("ftp_log.log")=Flase then
objFS.CreateTextFile("ftp_log.log")
Set objNF1 = objFS.OpenTextFile("ftp_log.log",8)
else
Set objNF1 = objFS.OpenTextFile("ftp_log.log",8)
end if

 

objFS.CreateTextFile("tmp.txt")
Set objNF=objFS.OpenTextFile("tmp.txt",8)
objNF.writeline "open "&ftp_host&" "&ftp_port
objNF.writeline ftp_username
objNF.writeline ftp_password
objNF.writeline "binary"

if objFS.fileExists(ftp_file)=Flase then
objNF1.writeline now &"Error: file " &ftp_file& "does not exist on ftp "&ftp_host&" path " &ftp_path
wscript.quit
else
Set objFile = objFS.OpenTextFile(ftp_file,1)
Do Until objFile.AtEndOfStream
command=objFile.ReadLine
objNF.writeline command
Loop
objFile.Close
objNF.writeline "bye"
objNF.Close
end if
objShell.Run "ftp -s:tmp.txt",4,True
objFS.DeleteFile("tmp.txt")
objFS.DeleteFile(ftp_file)

objNF1.close


 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有