在 Windows 中搜索并替换“覆盖”批量 PDF 文件回目录树

在 Windows 中搜索并替换“覆盖”批量 PDF 文件回目录树

我有数百个 PDF 文件,我使用应用程序为其添加水印,但是,该应用程序只能通过从目录树中复制每个 pdf 文件才能成功添加水印,而且幸运的是,每个文件的文件名都相同。这造成了很大的混乱,因为开始搜索、复制并将文件逐个替换回原始目录树会很麻烦。

是否有可能使用批处理脚本来执行这个过程?

答案1

这不是脚本编写服务,您现在可能已经可以手动处理这个问题,但是这应该可以做到:

@echo off
SETLOCAL ENABLEEXTENSIONS

SET OUTFILE=%~dp0
SET OUTFILE=%OUTFILE%filematch_go.bat

echo.
echo ----------------------------------
echo File Name Match in Dir Tree
echo ----------------------------------
echo.
echo This script will create the code for another batch script which you may 
echo run in order to replace all files under the Destination Path with those 
echo found in the Source Folder (but only if the file names match; no other 
echo checks are done). Since some hand editing is probably going to need to 
echo be made we'll just output the code for another script which will complete 
echo the task of doing the overwrites. This file will be saved to the same 
echo directory as this script with the filename: filematch_go.bat
echo.
echo Parameters
echo ----------
echo.
echo Source Folder: %1
echo Destination Path: %2
echo Output File: %OUTFILE%
echo.

SET ATTR=%~a1
SET DIRATTR=%ATTR:~0,1%
IF /I NOT "%DIRATTR%"=="d" GOTO SRCERR

SET ATTR=%~a2
SET DIRATTR=%ATTR:~0,1%
IF /I NOT "%DIRATTR%"=="d" GOTO DSTERR

PAUSE
echo @ECHO OFF > %OUTFILE%

FOR /f "tokens=*" %%S IN ('dir /S /B "%1*.txt"') DO (
    FOR /f "tokens=*" %%D IN ('dir /S /B "%2*.txt"') DO (
        IF /I "%%~nxS"=="%%~nxD" (
            echo COPY "%1%%~nxS" "%2%%~nxD" >> %OUTFILE%
        )
    )
)

GOTO ENDSCRIPT
:SRCERR
echo.
echo ERROR: The given source folder is not a valid directory.
GOTO ERROR

:DSTERR
echo.
echo ERROR: The given destination path is not a valid directory.

:ERROR
echo.
echo An error has occurred; script halted.
echo.

:USEAGE
echo.
echo Useage Instructions:
echo.
echo     filematch.bat ^<source^> ^<destination^>
echo.
echo   source        The folder where the new files are located
echo.
echo   destination   The root path of the directory tree where the files are 
echo                 to be placed.
echo.

:ENDSCRIPT
echo Done.

相关内容