user2399871
user2399871

Reputation:

A duplicate file name exists, or the file cannot be found

I have seen previous issues here on it anyway, but I'm asking for the particular case at hand.

Currently trying to rename some files, but I actually want the file with the duplicate name to be overwritten.

@echo off
setlocal
for /R %%A in ("tgt*.nif") do (
   set "fname=%%~A"
   call ren "%%fname%%" "%%fname:*tgt=%%"
)
pause

Any suggestions?

EDIT: As suggested, an example. Well this is not real, but simply putting it (Reading this it's not "simply" but pft):

I have two files, one which is TimeGoog.pdf and another which is Goog.pdf. Now I have these variations of these two files in many folders, so it may be TimeToot.pdf and Toot.pdf. I want to rename all the TimeGoog.pdf to Goog.pdf, deleting the old Goog.pdf at the same time as it's been overwritten.

The script above does that, except because it's renaming to something that's already there, it won't do it. Note that the batch code I put above would work in the parent folder, and all the sub folders would take effect.

Upvotes: 2

Views: 14852

Answers (2)

PourVos
PourVos

Reputation: 1

This helped me : for renaming files with no extension, to files with a extension. (.csv)

ren *.* *.csv (wrong)

gives : A duplicate file name exists, or the file cannot be found

ren *.* *.*csv (works)

Works.

Upvotes: 0

MC ND
MC ND

Reputation: 70923

@echo off
    setlocal enableextensions disabledelayedexpansion

    for /r %%A in ("tgt*.nif") do (
        set "folder=%%~dpA"
        set "fileName=%%~nxA"
        setlocal enabledelayedexpansion
            echo move /y "!folder!!fileName!" "!folder!!fileName:*tgt=!"
        endlocal
    )
    pause

Upvotes: 1

Related Questions