【问题标题】:Running .BAT file from Access从 Access 运行 .BAT 文件
【发布时间】:2013-11-26 15:19:56
【问题描述】:

我在通过访问运行 .bat 文件时遇到问题。 .bat 文件已创建,日志已创建(其中没有任何内容),当我在 Windows 资源管理器中单击 .bat 文件时将运行它,但我无法使用 .RUN 命令运行它。

sFTP = "ftp -s:FTPCMD.TXT > Z:\Recon\FTPUtilSrc\ftprslt.txt"
Open sFileName For Output As #2
Print #2, sFTP
'Print #2, "EXIT"
Close #2
oShell.Run sFileName, 0, True    

编辑:

sFileName = "Z:\Recon\FTPUtilSrc\MYFTP.BAT"
sFTP = "ftp -s:FTPCMD.TXT > Z:\Recon\FTPUtilSrc\ftprslt.txt"
Open sFileName For Output As #2
Print #2, sFTP
Print #2, "EXIT"
Close #2
oShell.run sFileName, 0, True

【问题讨论】:

    标签: vba batch-file


    【解决方案1】:

    试试这个:

    Dim cmd_str As String
    cmd_str = "cmd.exe /C ftp -s:FTPCMD.TXT > Z:\Recon\FTPUtilSrc\ftprslt.txt"
    Call Shell(cmd_str, vbNormalFocus)
    

    【讨论】:

    • 我认为它很接近,但它仍然在做同样的事情。我需要添加 .bat 文件的路径吗?它正在创建 ftprslt.txt 文件,但不运行 .bat
    • @user2183985 抱歉,我想我误解了其中的一部分。您在哪里定义脚本中批处理文件的路径。还值得为路径添加一个打印语句,以便仔细检查它是如何出现的。
    • Chris 的代码直接运行 ftp 转储而不使用批处理文件...为什么需要它从批处理文件运行?
    • @Blackhawk Chris 的代码不起作用。它仍然和我的代码之前做的一样。我添加并更新了显示批处理文件的额外代码行。
    【解决方案2】:

    尝试更改一些内容以查看发生了什么:

    sFileName = "Z:\Recon\FTPUtilSrc\MYFTP.BAT"
    sFTP = "ftp -s:FTPCMD.TXT > Z:\Recon\FTPUtilSrc\ftprslt.txt"
    Open sFileName For Output As #2
    Print #2, sFTP
    Print #2, "PAUSE" 'PAUSE forces the command prompt window to stay open.  This temporarily allows you to see if there are any issues with the ftp statment.
    Close #2
    oShell.run sFileName, 0, True '3 is "Activates the window and displays it as a maximized window." according to msdn.  This will allow you to see the execution of the ftp statment
    

    查看这些更改是否有助于您识别问题,以便您可以将其改回。顺便说一句,我通过运行以下代码测试了代码,它按预期工作,这让我相信这是 ftp 命令的问题。

    Public Sub TestBat()
        Set oShell = CreateObject("WScript.Shell")
        sFileName = "C:\Temp\MYECHO.bat" '"Z:\Recon\FTPUtilSrc\MYFTP.BAT"
        sFTP = "echo ""hi"" > C:\Temp\test.txt" '"ftp -s:FTPCMD.TXT > Z:\Recon\FTPUtilSrc\ftprslt.txt"
        Open sFileName For Output As #2
        Print #2, sFTP
        Print #2, "PAUSE"
        Close #2
        oShell.Run sFileName, 3, True
    End Sub
    

    【讨论】:

    • 感谢 Pause 命令帮了大忙。当它位于 Z 驱动器上时,它会在驱动器上寻找我的批处理文件。那解决了它。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 2020-03-09
    • 2010-10-24
    相关资源
    最近更新 更多