根据内容(关键字)复制pdf文件



我试图创建cmd代码扫描并复制包含某些关键字的pdf文件,并将副本保存到单独的文件夹。下面是我的代码,但它不起作用

@echo off
set "source=C:instructions"
set "target=C:instructionscafe"
set "string=cafe"
set "logfile=logs.txt"
call :main >> "%logfile%"
pause
goto :EOF
:main
for /r "%source%%dt%" %%a in ("*.pdf") do (
find /c /i "%string%" "%%~a" 1>&2
if not errorlevel 1 (
set /p "out=%%~a / " <nul
if exist "%target%%%~nxa" (
echo:Already exists
) ELSE (
copy "%%~a" "%target%"
if errorlevel 1 (
echo:Failed
) ELSE (
echo:Success
)
)
)
)
goto :EOF
有谁能帮我一下吗?

Find仅适用于编码pdf的纯文本内容,因此如果关键字被加密,则可能找不到它们。为了绕过这个限制,窗口有内容索引,对于pdf需要一个iffilter,它通常由默认的pdf阅读器提供(避免添加多个)。如果您没有从Adobe, SumatraPDF, Tracker pdf - exchange或Foxit Reader安装一个。你可以在https://www.pdflib.com/download/tet-pdf-ifilter/找到一个不错的(免费但有限)

假设文本是可检测的

您的主要问题是setlocal enabledelayedexpansion的共同需求,还有其他一些需求(例如目标文件夹不存在),所以我建议您删除隐藏消息,但已经纠正了主要问题。

@echo off
REM use delayed expansion for testing !errorlevel!
setlocal enabledelayedexpansion
set "source=C:instructions"
set "target=C:instructionscafe"
set "string=cafe"
set "logfile=logs.txt"
call :main >> "%logfile%"
pause
goto :EOF
:main
REM &dt% will default to nothing ? is it needed?
for /r "%source%%dt%" %%a in ("*.pdf") do (
find /c /i "%string%" "%%~a" 1>&2
REM your test here needs changing to this
if !errorlevel! == 0 (
set /p "out=%%~a / " <nul
if exist "%target%%%~nxa" (
echo:Already exists
) ELSE (
copy "%%~a" "%target%"
REM your test here needs changing to this
if !errorlevel! == 1 (
echo:Failed
) ELSE (
echo:Success
)
)
)
)
goto :EOF