FINDSTR:帮助理解结果。 Returns 什么都没有,但是当有预期的时候没有错误?
FINDSTR: help understanding results. Returns nothing, but no error when there is one expected?
我正在写一个批处理文件。该程序的一部分将比较 'source' 文件夹中的文件列表。带有列表内容的文本文件。
我遍历文件夹中的每个文件,并使用 FINDSTR 在文本文件中搜索其文件名
一切正常,直到源文件夹中出现文本文件中不存在的文件名。
findstr代码:
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
)
这是一种为您提供 file.txt
中不存在的文件名列表的方法
@echo off
cd /d "c:\folder\to\check"
for %%a in (*) do findstr /i "%%~nxa" "file.txt" >nul || echo "%%a" is missing
pause
它使用 %%~nxa
而不是 %%a
以防在某些时候使用子目录。
循环变量必须在批处理文件中使用 %%
引用,因为百分号具有特殊含义,因此必须在批处理文件中使用另一个百分号进行转义以逐字指定。这就是为什么在 运行 上,在命令提示符 window 中带有 echo on
的批处理文件导致在执行时显示为 %o
的批处理文件中出现 %%o
的原因.
命令FOR用于
for /f %%o in ('findstr %name% old.txt') do
处理被调用命令findstr
写入stdout的输出。但是 findstr
在文件中搜索一个或多个字符串并且在文件的任何行中都找不到任何匹配的字符串时,不会将任何内容写入标准输出。
因此命令 for 无法处理任何内容,因此在这种情况下 do
之后的 none 个命令完全被处理。
假设列表文件仅包含没有路径的文件名,以下注释批处理文件可用于获取命令dir[=52]执行1次=] 并且仅执行 1 或 2 次控制台应用程序 findstr 这两个列表包含在列表文件中找到和未找到的文件夹中的文件名。批处理文件是为了不产生空文件而写的
@echo off
setlocal
set "ListFile=C:\Temp\List.txt"
if not exist "%ListFile%" goto NoListFile
set "SourceFolder=C:\Temp\Test"
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%\system32\findstr.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%\system32\findstr.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
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
del /?
dir /?
findstr /?
goto /?
if /?
move /?
set /?
我正在写一个批处理文件。该程序的一部分将比较 'source' 文件夹中的文件列表。带有列表内容的文本文件。
我遍历文件夹中的每个文件,并使用 FINDSTR 在文本文件中搜索其文件名
一切正常,直到源文件夹中出现文本文件中不存在的文件名。
findstr代码:
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
)
这是一种为您提供 file.txt
@echo off
cd /d "c:\folder\to\check"
for %%a in (*) do findstr /i "%%~nxa" "file.txt" >nul || echo "%%a" is missing
pause
它使用 %%~nxa
而不是 %%a
以防在某些时候使用子目录。
循环变量必须在批处理文件中使用 %%
引用,因为百分号具有特殊含义,因此必须在批处理文件中使用另一个百分号进行转义以逐字指定。这就是为什么在 运行 上,在命令提示符 window 中带有 echo on
的批处理文件导致在执行时显示为 %o
的批处理文件中出现 %%o
的原因.
命令FOR用于
for /f %%o in ('findstr %name% old.txt') do
处理被调用命令findstr
写入stdout的输出。但是 findstr
在文件中搜索一个或多个字符串并且在文件的任何行中都找不到任何匹配的字符串时,不会将任何内容写入标准输出。
因此命令 for 无法处理任何内容,因此在这种情况下 do
之后的 none 个命令完全被处理。
假设列表文件仅包含没有路径的文件名,以下注释批处理文件可用于获取命令dir[=52]执行1次=] 并且仅执行 1 或 2 次控制台应用程序 findstr 这两个列表包含在列表文件中找到和未找到的文件夹中的文件名。批处理文件是为了不产生空文件而写的
@echo off
setlocal
set "ListFile=C:\Temp\List.txt"
if not exist "%ListFile%" goto NoListFile
set "SourceFolder=C:\Temp\Test"
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%\system32\findstr.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%\system32\findstr.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
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
del /?
dir /?
findstr /?
goto /?
if /?
move /?
set /?