【问题标题】:VBscript , create directory in FTPVBscript,在FTP中创建目录
【发布时间】:2012-03-09 02:45:47
【问题描述】:

我想在FTP中创建一个目录,目录名必须是我的电脑名,

这是我的代码,

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

此代码不起作用,但是当我将 ftp://user:password@ftpserver/url-path 替换为像 D:/ 这样的任何本地目录时,它起作用了:S

如何让它也适用于我的 ftp

【问题讨论】:

  • Scripting.FileSystemObject 用于文件系统,不支持 FTP。
  • 那我用什么?你能告诉我吗?

标签: vbscript upload ftp directory


【解决方案1】:

FileSystemObject 不支持 FTP。 Shell 自动化对象可以,但它似乎不喜欢 NewFolder 方法。这让我们可以使用无人值守的 FTP 会话自动执行 FTP.exe 命令。它可能看起来像这样。

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

【讨论】:

  • 这不是我的ip
  • 您是否确保 strUser、strPass 和 strHost 包含正确的凭据?你有什么错误吗?
  • 你在哪里使用 strHost 变量?它已定义但未在其他任何地方使用。
  • @anilca 对此非常抱歉。我更正了代码示例。注意“OPEN”行。
  • 谢谢@Nilpo。而且我认为 .WriteLine strPassword 行必须是 .WriteLine strPass
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 2012-02-29
  • 1970-01-01
  • 2012-12-31
相关资源
最近更新 更多