batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

#1 Post by terryhenderson » 16 Mar 2019 12:02

I need to overwrite all the mp3 files in all subdirectories under one main directory A, with mp3 files from directory B only if their names match.
I'll be more than grateful to get some precious help with this question.

best regards,

T.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

#2 Post by aGerman » 16 Mar 2019 13:08

Is the folder structure the same? That means would you find "A\foo\bar.mp3" in "B\foo" if it exists, or could it also be found in "B\baz"?

Steffen

terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

#3 Post by terryhenderson » 16 Mar 2019 13:37

Thanks very much Steffen for your reply.
Destination folder A has hundreds of subfolders where each of them has one third level subfolder named "resfiles" where all mp3 files exist. Source folder B has no subfolders and all the mp3 files are just staying in its root.
I hope I didn't make it more difficult.
Once again, thank you.

T.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

#4 Post by Squashman » 16 Mar 2019 13:53

Try to explain things in a logical order. Source is usually listed first and would be referred to as folder one or folder A. The destination is normally listed second and would be referred to as folder two or folder B.

terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

#5 Post by terryhenderson » 16 Mar 2019 23:06

I found the following solution at this link: https://stackoverflow.com/questions/533 ... ed-on-name, which worked only on one directory which is the main one and did not run through the tree down to the subdirectories at all levels:

Code: Select all

for /r "C:\folderA" %%a in (*.filetype) do if exist "C:\folderB\%%~nxa" copy /y "C:\folderB\%%~nxa" "C:\folderA\%%~nxa"
can it be adjusted to work on all subdirectories through to the end of the tree?

Appreciating any precious assistance with this question.

T.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

#6 Post by aGerman » 17 Mar 2019 05:00

Still don't understand what problem you're facing. You explained that you only have one folder each where the .mp3 files ar saved. So just check these two.

Steffen

terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

#7 Post by terryhenderson » 17 Mar 2019 06:01

the problem I'm facing is that the previous code does not overwrite mp3 files in their subdirectories. I have to manually change the path of the destination every time I want to run the batch, they are hundreds of subdirectories, actually over 1200. so all that I need is to just put the main directory A as the destination, and the code goes down to the subdirectories where mp3 files are and overwrite them accordingly by copying the same name file from the source directory B.

Many thanks.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

#8 Post by aGerman » 17 Mar 2019 08:58

untested:

Code: Select all

for /r "C:\folderA" %%a in (*.mp3) do if exist "C:\folderB\%%~nxa" copy /y "C:\folderB\%%~nxa" "%%~a"
Steffen

terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

#9 Post by terryhenderson » 17 Mar 2019 10:20

Thanks very much indeed.
it did WORK .. I don't know how to thank you .. it took the entire day and you did it in a few seconds ..
Please accept my best personal regards.

T.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

#10 Post by aGerman » 17 Mar 2019 10:27

terryhenderson wrote:
17 Mar 2019 10:20
taken the entire day searching
Did you specify ECHO ON to see what command lines are processed? Or did you replace COPY with ECHO COPY in order to find out what's wrong with the paths passed to the copy command?

terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder

#11 Post by terryhenderson » 17 Mar 2019 10:40

I did used ECHO ON, and "as usual" it was my silly mistake adding a forward slash to the path.
You did a great favour to me tonight, and I do really thank you very much.
You do have a great value added to this world.

All the best.

T.

Post Reply