【问题标题】:Get text file using FTP each 10 seconds每 10 秒使用 FTP 获取文本文件
【发布时间】:2016-04-20 08:58:16
【问题描述】:

我正在寻找可以使用 FTP 获取文本文件并延迟 10 秒永久运行的批处理文件。

这是我的代码:

:loop

ftp -i -s:"%~f0"&GOTO:EOF
open Server_IPADDRESS
userName
Password
get test.txt
close

TIMEOUT 10
goto loop

我不断收到错误

ftp> TIMEOUT 10
Invalid command.
ftp> goto loop
Invalid command.

我想要的是退出ftp并恢复正常cmd,但我无法让它工作。

【问题讨论】:

    标签: batch-file cmd ftp


    【解决方案1】:

    确保您了解-s:"%~f0" 的作用。

    它告诉ftp.exe 自己运行批处理文件,就好像它在哪里运行 FTP 脚本一样。

    ftp.exe 运行批处理文件时,它:

    • :loopftp.exe 行提供两个“无效命令”错误。
    • 运行实际的 FTP 脚本
    • TIMEOUT 10goto loop 命令提供两个“无效命令”错误。

    另一方面,批处理文件解释器(cmd.exe)看到GOTO:EOF,这实际上告诉它停止解释批处理文件。因此TIMEOUT 10goto loop 永远不会被执行。


    你需要:

    • GOTO:EOF 更改为在TIMEOUT 10 命令之前跳转;
    • 使用bye 命令告诉ftp.execlose 之后停止,以免出现后一组“无效命令” 错误。
    :loop
    
    ftp -i -s:"%~f0"&GOTO:afterftp
    open Server_IPADDRESS
    userName
    Password
    get test.txt
    close
    bye
    
    :afterftp
    TIMEOUT 10
    goto loop
    

    虽然更健壮的方法是将 FTP 脚本和批处理文件分开:

    FTP 脚本 (ftp.txt):

    open Server_IPADDRESS
    userName
    Password
    get test.txt
    close
    

    批处理文件:

    :loop
    
    ftp -i -s:ftp.txt
    
    TIMEOUT 10
    goto loop
    

    或者为 FTP 脚本使用一个临时文件:

    :loop
    
    echo open Server_IPADDRESS> ftp.txt
    echo userName>> ftp.txt
    echo Password>> ftp.txt
    echo get test.txt>> ftp.txt
    echo close>> ftp.txt
    
    ftp -i -s:ftp.txt
    
    del ftp.txt
    
    TIMEOUT 10
    goto loop
    

    (实际上,不需要在每个循环中重新创建ftp.txt

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多