Jump to content

Copying and merging folders with wildcards


Recommended Posts

Posted

We need a way of copying all files from multiple folders that share parts of the same name into a new directory, for example:

 

We have 3 folders called 123folder folder832 and xfolder45 and we want all items to be moved into "folder". In the original directory, there are 600 folders, and they need brining down to 300/400 folders by merging folders with similar filenames as per my example. This is to move a mess of users home directories, onto a new server from a site we've just taken over. For some reason there are two or three folders for some users.

 

We've tried xcopy "C:\*folder*\*.*" "C:\folder" but this doesn't work.

 

Also, we've tried FOR /d %a in (folder*) DO XCOPY "C:\%a" "C:\%a\" /s /e /f /h /k /y with no luck.

 

Hopefully this makes sense...

 

 

If anyone has a script or can link to some info or some software that can do what we require it'll be appreciated.

 

Cheers

Posted (edited)

I'm fairly sure this could be done with a simple PowerShell script. Something like this.

 

$commonName = "folder"
$targetPath = "C:\$commonName"

foreach ($ci in Get-ChildItem -Path C:\ -Recurse -Directory) {
   if ($ci.Name -like "*$commonName*") {
       Get-ChildItem $ci | Move-Item $targetPath
   }
}

 

That'll match anything with "folder" in the directory name, and move its contents to the target "C:\folder". If you want it to work with more matches than just folder create an array for the keywords, and wrap it in a foreach loop.

 

You might also want to focus the initial search path a little more than just on the root.

Edited by jamesb
  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...