batch-file - 如何修复批处理脚本中的错误“%%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
。
解决方案
如果你看看这一行:
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%"
推荐阅读
- php - 在 SFC Payment Gateway -> SFC Pay - Request Encryption API 中获取中间件错误作为响应
- linux - 如何修复导入错误:libstdc++.so.6 错误,GLIBCXX 3.4.26
- css - Element.io 多选自动宽度
- javascript - 如何处理在 My Sql 中单击一次时保存两次的数据
- android - 带有 BottomSheetDialog 的半透明导航栏
- javascript - 为什么 _react-native-otp-verify_ 库调用会引发“不是函数”错误?
- visual-studio - visual stdio 2019 无法识别 net 5.0 框架
- android - 如何从可组合函数中获取当前活动?
- kubernetes - 如何在 Helm 中的其他 pod 之前先安装 istio mutating webhook 和 istiod?
- ant-media-server - 启用 ABR 后,我们如何在 Ant Media 服务器中自定义 MP4 节省?