Search code examples
batch-filebatch-rename

Overwrite a file with an image in batch


I want to be able to replace all the files in a directory with an image, using batch. I know a bit about xcopy, copy, and move, but I don't know how I might be able to use them or other commands to pull this off. Is there any simple, straightforward way to do this?

EDIT: For example, in the directory there are files 'foo.txt' and 'bar.doc'. After the batch, this folder would be left with 'foo.png' and 'bar.png', and when I open them I get the same image.


Solution

  • @echo off
    setlocal enableextensions disabledelayedexpansion
    
    set "sourceImage=c:\somewhere\something.png"
    set "targetFolder=c:\myFolder"
    
    for %%a in ("%sourceImage%") do for %%f in ("%targetFolder%\*.*") do (
        del /f /q "%%~ff" >nul 2>nul
        copy /y "%sourceImage%" "%%~dpf%%~nf%%~xa" >nul 2>nul 
    )
    
    endlocal
    

    Not tested. Adapt as needed.