'Windows Script to Recursively Copy Files and add directory name prefix

I'm need a script to recursively copy a set of files from a folder and all the subfolders to a new folder. I do not want the directory structure copied. But I want to add the original directory name (not the full path) as a prefix to the copied file.

For example...copy c:/photos/date1/photo1.jpg c:/photos/date1/photo2.jpg c:/photos/date1/photo3.jpg c:/photos/date2/photo1.jpg c:/photos/date2/photo2.jpg c:/photos/date2/photo3.jpg

to: c:/newfolder/date1-photo1.jpg .....date1-photo2.jpg .....date1-photo3.jpg .....date2-photo1.jpg .....date2-photo2.jpg .....date2-photo3.jpg

I have this for loop to do the original copy which works just fine. But I am stuck at how to add the directory name as a prefix.

for /r %d in (*) do copy "%d" "x:\newfolder"

Appreciate advise/suggestions.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source