我正在写一个批处理文件。程序的一部分将比较“来源”中的文件列表。夹。使用文本文件中列表的内容。
我遍历文件夹中的每个文件,并使用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
这是我使用此页面上的帮助创建的最终批处理文件。它会创建一个hotfolder,它将编辑添加到其中的所有新文件,直到您停止运行脚本:
: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
)
答案 0 :(得分:1)
循环变量必须在批处理文件中使用%%
引用,因为百分号具有特殊含义,因此必须使用批处理文件中的另一个百分号进行转义以按字面指定。这就是为什么在命令提示符窗口中运行带有echo on
的批处理文件导致批处理文件中的%%o
在执行时显示为%o
的原因。
中使用的命令 FOR
for /f %%o in ('findstr %name% old.txt') do
通过被调用的命令findstr
处理写入 stdout 的输出。但是findstr
在搜索文件中的一个或多个字符串时无法向标准输出写入任何内容,并且无法在文件的任何行中找到任何匹配的字符串。
因此命令无法处理任何内容,因此在这种情况下根本不处理do
之后的任何命令。
假设列表文件只包含没有路径的文件名,可以使用以下注释的批处理文件来执行命令 dir ,只执行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
要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
del /?
dir /?
findstr /?
goto /?
if /?
move /?
set /?
答案 1 :(得分:0)
这是一种方法,可以为您提供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
。