我正在嘗試創建 cmd 代碼來掃描和復制包含特定關鍵字的 pdf 檔案,并將副本保存到單獨的檔案夾中。下面是我的代碼,但它不起作用
@echo off
set "source=C:\instructions"
set "target=C:\instructions\cafe"
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
有人可以幫我嗎?
uj5u.com熱心網友回復:
Find 僅適用于已編碼 pdf 的純文本內容,因此如果關鍵字已加密,則可能無法找到它們。為了解決這個限制,windows 有內容索引,對于 pdf 需要一個 iFilter,它通常由默認的 pdf 閱讀器提供(避免添加多個)。如果您沒有安裝 Adob??e、SumatraPDF、Tracker PDF-Xchange 或 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:\instructions\cafe"
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331379.html
