2

I'm trying to move files and keep duplicate file names by appending (1) to one of the duplicate files.

I'm using

cd /D "source directory"
move *.JPG "target directory"

which doesn't solve the problem. Can someone please help?

Thank you for the assistance.

Kian Nikzad
  • 25
  • 1
  • 5
  • You can't do that from the command line. Windows Explorer does it for you with specialized code, but it's not built in to any of the Windows command line tools. – Ken White Sep 07 '18 at 00:10
  • Yes, you can code it yourself. I'm sure if you search here using **[batch-file] move files rename duplicates** you can find code to do so. Your question was about using move. – Ken White Sep 07 '18 at 00:26

1 Answers1

3

This should do what you want. We dir and search for all .jpg files in the source folder, then check if it exists, if it does, append a number using a counter, if it does not exist, we just move it..

@echo off
setlocal enabledelayedexpansion
set "source=D:\source\"
set "dest=D:\destination\"
set /a cnt=0
for /f "tokens=*" %%a in ('dir /S /B /A-D "%source%*.jpg"') do for /f "tokens=*" %%b in ('dir /B "%%a"') do if exist "%dest%\%%b" (
        set "ext=%%~xa"
        set "fname=%%~na"
        if exist "%dest%\!fname!(!cnt!)!ext!" (set /a cnt=!cnt!+1)
        set /a cnt=!cnt!+1
        move "%%a" "%dest%\!fname!(!cnt!)!ext!"
) else move "%%a" "%dest%\%%b"
Gerhard
  • 22,678
  • 7
  • 27
  • 43