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.

2
  • 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. Commented Sep 7, 2018 at 0: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. Commented Sep 7, 2018 at 0:26

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.