Windows: copy files and change extension without duplicates

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error Windows: copy files and change extension without duplicates on this date .

I have a directory that has image files without extension. Let us say it to be D:initial. Now I want to copy those files over to D:final directory and change the extension to .jpg for each file.

My solution using ROBOCOPY:

@echo off

SET srcDir=D:initial
SET destDir=D:final

echo Copying files from %srcDir%
ROBOCOPY %srcDir% %destDir% /s /min:102400
echo Copying done

cd %destDir%

echo Renaming to JPG
ren *. *.jpg

However, there are certain conditions:

  1. Copy only those files that are greater than 100 KB in size.
  2. Do not delete files in the source directory.
  3. The source directory will at certain periods, get newer files; copy them to the destination directory (manually, no automation needed here)

My solution meets the first two conditions, but when I run again after new files arrive, the older ones get copied over too thus giving error at renaming.

Answer

Check file size and if destination file exists with an iterating for on the sourcefiles.

And use xcopy instead of invoking robocopy every time what would be overkill here.

Copying to the new name with extension in one go eliminates the need to rename.

@echo off

SET "srcDir=D:initial"
SET "destDir=D:final"

echo Copying files from %srcDir%
For %%A in ("%srcDir%*.") do (
    if %%~zA gtr 102400 if not exist "%destDir%%%~nA.jpg" copy "%%~fA" "%destDir%%%~nA.jpg" >NUL
)
echo Copying done