0

I have hundreds of PDF files I used an application to apply watermark to, however, the application only succeeded to apply watermarks by applying a copy of each pdf file off the directory tree and luckily file names are maintained the same for each. This caused a big chaos because it would be headache to start doing search, copy and replace file by file back into original directory tree.

Is there any possibility to have a batch script to perform this process?

  • You're best bet is to study the manual for the application you used and figure out how to get it to do an in-place edit. If you want to fix what you've already done then you'll need to tell us how the program did this. How did the program ensure that files weren't overwritten? Did it create a duplicate dir tree? Did it prepend the path to each file name? Did it simply add a random GUID to each file? – krowe2 Jul 29 '15 at 15:40
  • @krowe2 whatever software used, for some file where I perform modifications then I save the file with same name in a workplace folder, then i did this hundreds of times. And it's now the time to replace all these files back to their origins. – Tazo Jul 29 '15 at 15:46
0

This isn't a script writing service and you could've probably taken care of this manually by now but this should do it:

@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.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.