Csv 检查文件是否存在,并在批处理中找到字符串错误

Csv 检查文件是否存在,并在批处理中找到字符串错误,csv,batch-file,Csv,Batch File,我有一个批处理文件来查找文件。如果文件退出,则在这些文件中查找请求时间单词。如果找到请求时间单词,我想将文件名“O”输出为csv文件。如果找不到单词,则将文件名,“X”输出到csv文件。这是我的批处理脚本 @echo off Set "filePath=%~dp0" :: Please change the filePath with your filePath. if exist %filePath%nodes.txt goto Label1 echo. echo Cannot find

我有一个批处理文件来查找文件。如果文件退出,则在这些文件中查找
请求时间
单词。如果找到
请求时间
单词,我想将
文件名“O”
输出为csv文件。如果找不到单词,则将
文件名,“X”
输出到csv文件。这是我的批处理脚本

@echo off
Set "filePath=%~dp0"
:: Please change the filePath with your filePath.

if exist %filePath%nodes.txt goto Label1
echo.
echo Cannot find  %filePath%nodes.txt
echo.
Pause
goto :eof

:Label1
for /f %%i in (%filePath%nodes.txt) do call :check %%i
Pause
goto :eof

:check
Set "fOut=%filePath%pingRsl.csv"

if EXIST "%filePath%%1.txt" (
find "Request time" %filePath%%1.txt>nul
if %errorlevel% equ 1 (echo %1, O>>"%fOut%") else (echo %1, X>>"%fOut%")
) ELSE (
echo %1,"">>"%fOut%"
) 
goto :eof
但是,结果并不像我预期的那样。我想不出是怎么回事

我做了一些更改(从:Label1开始),希望它们能帮助您

:标签1
对于/F“UseBackQ Delims=“(“%filePath%nodes.txt”)中的%%A,请执行调用:选中“%%~A”
暂停
后藤:EOF
:检查
设置“fOut=%filePath%pingRsl.csv”
如果不存在“%filePath%%1.txt”转到:EOF
(查找/I“请求时间”<%filePath%%1.txt“>Nul&&(
回声%1,O)|回声%1,X)>>%fOut%
后藤:EOF

还要记住,当您临时
设置“filePath=%~dp0”
时,它包含一个尾部反斜杠。因此,您需要确保最终用户在使用您的文件路径时::请使用您的文件路径更改文件路径。它们还包括一个后斜杠,否则您需要使用
%filePath%
调整
%filePath%\

的所有实例。现在,我可以解决这个问题

@echo off
Set "filePath=%~dp0"
:: Please change the filePath with your filePath.

if exist %filePath%nodes.txt goto Label1
echo.
echo Cannot find  %filePath%nodes.txt
echo.
Pause
goto :eof

:Label1
for /f %%i in (%filePath%nodes.txt) do call :check %%i
Pause
goto :eof

:check
Set "fOut=%filePath%pingRsl.csv"
if EXIST "%filePath%%1.txt" (
>nul find "Request time" "%filePath%%1.txt" && (
  echo %1,"X">>"%fOut%"
) || (
  echo %1,"O">>"%fOut%"
)
) ELSE (
echo %1,"">>"%fOut%"
) 
goto :eof

标准
延迟扩展
陷阱。使用顶部栏中的
搜索
功能查找曝光的
延迟扩展

摘要:在执行代码块之前,代码块(括号内的命令序列)中的任何
%var%
都将替换为该变量的值

所以

应该是

if errorlevel 1 (echo %1, O>>"%fOut%") else (echo %1, X>>"%fOut%")

这是
if errorlevel
的原始含义,意味着如果运行时errorlevel为1或大于1,您会得到什么错误?尝试在开始时将turing
@echo打开而不是关闭,并告诉我们执行的最后一行,或者是否有错误,它是什么,在什么位置line@Monacraft现在,我可以自己解决了。谢谢。这个网站已经有了一个系统来表达你的感激之情。把我的答案标记为已接受就足够了。
if errorlevel 1 (echo %1, O>>"%fOut%") else (echo %1, X>>"%fOut%")