Page 1 of 1
batch to overwrite files in all subdirectories in one folder if their names match with files in another folder
Posted: 16 Mar 2019 12:02
by terryhenderson
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.
Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder
Posted: 16 Mar 2019 13:08
by aGerman
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
Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder
Posted: 16 Mar 2019 13:37
by terryhenderson
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.
Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder
Posted: 16 Mar 2019 13:53
by Squashman
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.
Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder
Posted: 16 Mar 2019 23:06
by terryhenderson
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.
Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder
Posted: 17 Mar 2019 05:00
by aGerman
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
Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder
Posted: 17 Mar 2019 06:01
by terryhenderson
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.
Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder
Posted: 17 Mar 2019 08:58
by aGerman
untested:
Code: Select all
for /r "C:\folderA" %%a in (*.mp3) do if exist "C:\folderB\%%~nxa" copy /y "C:\folderB\%%~nxa" "%%~a"
Steffen
Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder
Posted: 17 Mar 2019 10:20
by terryhenderson
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.
Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder
Posted: 17 Mar 2019 10:27
by aGerman
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?
Re: batch to overwrite files in all subdirectories in one folder if their names match with files in another folder
Posted: 17 Mar 2019 10:40
by terryhenderson
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.