FINDSTR:帮助理解结果.不返回任何内容,但在预期有错误时不会返回错误



我正在写一个批处理文件。该程序的一部分将比较"源"文件夹中的文件列表。使用文本文件中列表的内容。

我循环遍历文件夹中的每个文件,并使用FINDSTR

在文本文件中搜索其文件名

一切正常,直到源文件夹中有一个文本文件中不存在的文件名。

findstr code:

for /f %%o in ('findstr %name% old.txt') do (
    echo o=%%o >> result.txt
    if %%o==%name% (
        echo %name% exists
    ) ELSE (
        echo %name% does not exists
    )
)

同样,当FINDSTR搜索不在文本文件中的文件名时,就会出现问题。

到达该点时,它将变量%%o输出为'%o'并且不回显任何内容。所以它不向results.txt发送任何内容。

这不会触发ERRORLEVEL更改,但也不会回显任何内容。我已经尝试输出错误级别,但它们也是空的。我只是不明白FINDSTR在这个实例中做什么。

完整批处理文件:(这是我的第一个。原谅任何错误)

  ::return the raw (/b) list of files 
  FORFILES /p %~dp0source /s /m "*.cr2" /C "cmd /c echo @path" > new.txt
  ::pull file path for each file and send to subroutine
  for /f %%n in ('FORFILES /p %~dp0source /s /m "*.cr2" /C "cmd /c echo @path"') do (
    call :dequote %%n
  )
::subroutine for removing quotes 
::and returning the filename, extension, and path
:dequote
set fullPath=%~f1
set fileName=%~n1
set fileExt=%~x1
set filePath=%~dp1
set name=%fileName%& set npath=%filePath%& set ext=%fileExt%& set fpath=%fullPath%
echo %fpath%
echo %npath%
echo %name%
echo %ext%
for /f %%o in ('findstr %name% old.txt') do (
    echo o=%%o >> result.txt
    if %%o==%name% (
        echo %name% exists
    ) ELSE (
    echo %name% does not exists
    )
)

这只发生在发送给findstr的最后一个文件名上。任何建议或方向将非常感激。我试着读了所有我能找到的书。

谢谢你的时间。


更新:9-9-15

下面是我使用本页上的帮助创建的最终批处理文件。它会创建一个热文件夹,将编辑添加到其中的任何新文件,直到您停止脚本运行:

  :start
  :: return the raw (/b) list of files and full path to source text
  FORFILES /p %~dp0source /s /m "*.cr2" /C "cmd /c echo @path" > source.txt
  IF %ERRORLEVEL% EQU 1 goto :start
  ::join new and old data, return only what is shared in common (/g)
  findstr /I /L /G:"source.txt" "output.txt" > found.txt
  IF %ERRORLEVEL% EQU 1 copy /y source.txt notFound.txt
  ::join found file names and source filenames, return those that do not have a match
  findstr /I /L /V /G:"found.txt" "source.txt" >> notFound.txt
  IF %ERRORLEVEL% EQU 2 echo error no match
  ::for each line of notFound.txt, dequote and break apart
  for /f %%n in (notFound.txt) do (
    echo n=%%n
    call :dequote %%n
  )
  :dequote
  set fullPath=%~f1
  set fileName=%~n1
  set fileExt=%~x1
  set filePath=%~dp1
  set name=%fileName%& set npath=%filePath%& set ext=%fileExt%& set fpath=%fullPath%
  echo %fpath%
  echo %npath%
  echo %name%
  echo %ext%
  cd %nPath%
  if NOT [%1]==[] (
    echo converted %name%
    convert -negate -density 600 -colorspace gray flatField.cr2 %name%%ext% -compose Divide -composite %name%.tif
    move %name%.tif %~dp0output
    cd %~dp0
    del notFound.txt
    copy /y source.txt output.txt
    ) ELSE (
     echo end of batch else
     cd %~dp0
  )

循环变量必须在批处理文件中用%%引用,因为百分号有特殊的含义,因此必须在批处理文件中用另一个百分号转义以字面上指定它。这就是为什么在命令提示窗口中使用echo on运行批处理文件会导致批处理文件中的%%o在执行时显示为%o的原因。

命令用于

for /f %%o in ('findstr %name% old.txt') do

处理被调用命令findstr写入stdout的输出。但是,当findstr在文件中搜索一个或多个字符串,并且在文件的任何行中都找不到任何匹配的字符串时,它不会向标准输出写入任何内容。

所以的命令不能处理任何东西,因此在这种情况下,do之后的命令都不会被处理。

假设列表文件只包含没有路径的文件名,下面注释的批处理文件可以用于执行命令dir,只需执行控制台应用程序findstr 1或2个列表,其中包含在列表文件中被找到和未被找到的文件夹中的文件名。批处理文件是为了不产生空文件而编写的。

@echo off
setlocal
set "ListFile=C:TempList.txt"
if not exist "%ListFile%" goto NoListFile
set "SourceFolder=C:TempTest"
if not exist "%SourceFolder%*" goto NoSourceFolder
set "AllFileNames=%TEMP%AllFileNames.txt"
set "FoundFileNames=%TEMP%FoundFileNames.txt"
set "NotFoundFileNames=%TEMP%NotFoundFileNames.txt"
rem Get alphabetic list of files in source folder without path.
dir /A /B /ON "%SourceFolder%" >"%AllFileNames%"
rem Find all file names in list file with a case-insensitive
rem search matching completely a file name in list file and
rem output the found file names to another list file.
%SystemRoot%system32findstr.exe /I /L /X "/G:%AllFileNames%" "%ListFile%" >"%FoundFileNames%"
if errorlevel 1 goto NoFileNameFound
rem Find all file names with a case-insensitive search found
rem before in all file names list and output the lines not
rem containing one of the file names to one more list file.
%SystemRoot%system32findstr.exe /I /L /V "/G:%FoundFileNames%" "%AllFileNames%" >"%NotFoundFileNames%"
if errorlevel 1 goto AllFileNamesFound
rem Some file names are found in list file and others not.
del "%AllFileNames%"
goto :EndBatch
:NoFileNameFound
move /Y "%AllFileNames%" "%NotFoundFileNames%"
del "%FoundFileNames%"
goto EndBatch
:AllFileNamesFound
del "%AllFileNames%"
del "%NotFoundFileNames%"
goto EndBatch
:NoListFile
echo %~f0:
echo Error: No list file %ListFile%
goto EndBatch
:NoSourceFolder
echo %~f0:
echo Error: No folder %SourceFolder%
:EndBatch
endlocal

为了理解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

  • del /?
  • dir /?
  • findstr /?
  • goto /?
  • if /?
  • move /?
  • set /?

这个方法会给你一个file.txt

中不存在的文件名列表
@echo off
cd /d "c:foldertocheck"
for %%a in (*) do findstr /i "%%~nxa" "file.txt" >nul || echo "%%a" is missing
pause

它使用%%~nxa而不是%%a,以防在某些时候使用子目录。

最新更新