VBscript , create directory in FTP

一曲冷凌霜 提交于 2019-12-06 08:05:20

问题


I would like to create a directory in FTP, the name of the directory must be as my computer name,

here is my code,

Dim FoldertoCreate, filesys, newfolder, Ob
Set Ob = Wscript.CreateObject("Wscript.Network" )
FoldertoCreate = "ftp://user:password@ftpserver/url-path/" & ob.ComputerName
Set filesys = CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(FoldertoCreate) Then
   Set newfolder = filesys.CreateFolder(FoldertoCreate)
End If

This code doesn't work however when i replace ftp://user:password@ftpserver/url-path with any local directory like D:/ , it works :S

how to make it work for my ftp too


回答1:


The FileSystemObject does not support FTP. The Shell Automation Object does, but it doesn't seem to like the NewFolder method. That leaves us with automating the FTP.exe command using an unattended FTP session. It might look something like this.

strUser = "myusername"
strPass = "mypassword"
strHost = "ftp.myhost.com"

Const ForWriting = 2

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.OpenTextFile("session.txt", ForWriting, vbTrue)

With objFile
    .WriteLine "OPEN " & strHost
    .WriteLine "USER " & strUser
    .WriteLine strPass
    .WriteLine "mkdir sometestdirectory"
    .Close
End With

strFTP = "%systemroot%\System32\ftp.exe -s:session.txt"
Set WshShell = CreateObject("WScript.Shell")
strFTP = WshShell.ExpandEnvironmentStrings(strFTP)
WshShell.Run strFTP,, vbTrue

objFso.DeleteFile "session.txt", vbTrue


来源:https://stackoverflow.com/questions/9332694/vbscript-create-directory-in-ftp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!