Score:0

Copy files from the same subdirectory to the same Directory in Windows

fr flag

I am having a hard time describing the problem and I can't find the solution anywhere so here is an example

SourceFolder\ProjectA\Legal\ContractA.txt
SourceFolder\ProjectB\Legal\ContractB.txt
SourceFolder\ProjectC\Legal\ContractC.txt
SourceFolder\ProjectD\Legal\ContractD.txt
etc..

I need to copy the contractletter.txt file from hundreds of these Projectletter folders to a single location such as this:

DestinationFolder\ContractA.txt
DestinationFolder\ContractB.txt
DestinationFolder\ContractC.txt
DestinationFolder\ContractD.txt
etc..

Is there a PowerShell command that will do it?

Lieven Keersmaekers avatar
in flag
`gci SourceFolder\Contract* -rec | % {copy-item $_ -destination "DestinationFolder\$($_.name)"}` might do but you would overwrite identical filenames
Artrelay avatar
fr flag
I think this would be ok if the path up to the Contract.txt files would be the same but the Project folder names are all different
Lieven Keersmaekers avatar
in flag
If the filenames are all different, the oneliner could suffice. Add a `-whatif` just before the `}` to do a dry run.
Artrelay avatar
fr flag
It's not just the files that are different it is also the folder before, the ProjectA,B,C and D folders
Lieven Keersmaekers avatar
in flag
Did you try it? (*with* the -whatif parameter!)
Artrelay avatar
fr flag
Great, that did it, thanks for your help
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.