Search code examples
windowsbatch-filefile-renamefile-copying

Batch script to duplicate a file multiple times using filenames from a list


I'm Using a batchscript to duplicate a couple of PDFs and output them with different filenames each.

So far, What I have done is

SET Template=C:\Temp\template.pdf
copy /y %Template% .\%Class%\Apples_fruits.pdf 
copy /y %Template% .\%Class%\Oranges_fruits.pdf
copy /y %Template% .\%Class%\Grapes_fruits.pdf
copy /y %Template% .\%Class%\Bananas_fruits.pdf
copy /y %Template% .\%Class%\Strawberry_fruits.pdf

I want to do this without having to type the copy /y line each time. I'd like to have a list of 'fruits' in a .txt or .xlsx file and have batch use the items in the list as filenames.

For example:

Apples_fruits.pdf
Oranges_fruits.pdf
Grapes_fruits.pdf
Bananas_fruits.pdf
Strawberry_fruits.pdf

Solution

  • Windows 10 64-bit.

    The batch file and fruit.txt have to be in the same directory.

    SET Template=%temp%\template.pdf
    SET Class=Class
    IF /i not exist %Class% md %Class%
    FOR /f "delims=" %%g in ('type fruit.txt') do COPY /y %Template% .\%Class%\%%g_fruits.pdf 
    

    fruit.txt:

    Apples
    Oranges
    Grapes
    Bananas
    Strawberry