【问题标题】:VBscript and CMD writing into a text fileVBscript 和 CMD 写入文本文件
【发布时间】:2012-03-25 17:25:28
【问题描述】:

我正在编写一个脚本来执行并将所有内容写入文件

这是一个例子,

我将完整的命令存储在变量 'Command' 中,

Command = "ftp ftp.xyz.com 21 " & vbCRLF

然后在命令提示符下执行,

shell.Run "%comspec% /c FTP " & Command & " > " & E:/abc.txt, 0, TRUE

但是当这个程序执行时它不会向文本文件写入任何内容,因为这是一个不完整的命令,这个命令在执行时提示用户输入 FTP 的用户名和密码,

我该怎么做,我的程序会在提示时自动输入用户名和密码,然后将所有内容写入文件?

【问题讨论】:

    标签: shell text command-line vbscript


    【解决方案1】:

    您需要使用无人值守的脚本运行 FTP。 (尝试ftp /? 并查看 -s 开关。)

    看起来像这样:

    Const HOSTNAME = "ftp.myserver.com"
    Const USERNAME = "Login"
    Const PASSWORD = "password"
    
    Set WshShell = CreateObject("WScript.Shell")
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFso.CreateTextFile("session.txt")
    
    With objFile
        .WriteLine "USER username"
        .WriteLine "password"
        .WriteLine "cd /public_html/"  ' continue adding commands like this
        .Close
    End With
    
    strOutput = "C:\somefilepath\output.txt"
    strCommand = "%systemroot%\System32\ftp.exe -s:session.txt > " & strOutput
    strCommand = WshShell.ExpandEnvironmentStrings(strCommand)
    WshShell.Run strCommand, 0, vbTrue
    
    objFso.DeleteFile "session.txt", vbTrue
    

    您可以在我关于 ASP Free 的文章Using FTP in WSH 中阅读更多内容。我还回答了一个相关问题here

    【讨论】:

    • 我猜strCommand = WshShell.ExpandEnvironmentStrings(strFTP) 行中有错字。 strFtp 未定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多