首页 > 解决方案 > 如何修复批处理脚本中的错误“%%i 此时意外”?

问题描述

我正在尝试从 FTP 服务器下载一些文件,并根据它们的文件名将它们放在不同的文件夹中。

使用:

FOR /F "tokens=1 delims=" %%i IN (%binpath%inputfilelist.txt) DO ECHO. get %%i >> %binpath%unixftp_get1.txt

我收到错误消息:

%%i 出乎意料

我已经检查了 中的文件inputfilelist.txt,有2可用的文件。

REM **********************Determine input file count*******************************************

SET target=TXT
CHDIR /d %binpath%
find /c  "%target%" < %binpath%inputfilelist.txt >  %binpath%inputfilecount.txt 
SET /p inputfile_cnt=<%binpath%inputfilecount.txt

IF %inputfile_cnt%!==!0 GOTO PROCEED

IF %inputfile_cnt%==0 GOTO END

:PROCEED

REM ******************** Dynamically create the ftp get commands file and download the files*************************
copy %binpath%unix_ftp.config %binpath%unixftp_get1.txt

FOR /F "tokens=1 delims=" %%i IN (%binpath%inputfilelist.txt) DO ECHO. get %%i >> %binpath%unixftp_get1.txt

ECHO. bye >>  %binpath%unixftp_get1.txt

ftp -v -s:"%binpath%unixftp_get1.txt" %server% >> "%logpath%%ftp_log%"

预期的结果是abc.txt需要在文件中附加的文件 名unixftp_get1.txt

标签: batch-fileftp

解决方案


如果你看看这一行:

IF %inputfile_cnt%!==!0 GOTO PROCEED

这不会GOTO PROCEED是因为您的比较期望#!匹配!0, #包含的行数在哪里TXT,但它显然永远不会。

要解决这个问题,您通常会使用以下语法:

IF NOT "%inputfile_cnt%"=="0" GOTO PROCEED

然后,您将其下方的行更改为:

GOTO END

这是您的代码段的重写,试一试并在必要时提供反馈:

REM ************************ Determine input file count ************************
SET "target=TXT"
CD /D "%binpath%"

FOR %%A IN (logpath,ftp_log)DO IF NOT DEFINED %%A GOTO END

FOR %%A IN ("inputfilelist.txt","unix_ftp.config","%logpath%"
)DO IF NOT EXIST "%%~A" GOTO END

FOR /F %%A IN ('FIND /C "%target%"^<"inputfilelist.txt"'
)DO IF "%%A"=="0" GOTO END

:PROCEED
REM *** Dynamically create the ftp get commands file, and download the files ***
COPY /Y "unix_ftp.config" "unixftp_get1.txt"

(   FOR /F "DELIMS=" %%A IN ("inputfilelist.txt")DO ECHO get %%A
    ECHO bye
)>>"unixftp_get1.txt"

ftp -v -s:"unixftp_get1.txt" %server% >>"%logpath%%ftp_log%"

推荐阅读