I'm trying to find a way to group files together for easy copying. The requirement is that I have filenames like:

aa0048.pdf and fg345_aa0048_somethingsomething.xml. There are 25000 files in the folder and the match between pairs is that the part of the name is equal. In this example aa0048.

I tried grouping and filtering in Windows Explorer but it didn't give good results. Is there another tool that can do this for me or some PowerShell/cmd script to make these combinations and move the first 100 couples to another folder?

The reason is that I need to process the couples later on with a batch program but want to do it in chunks of 100 instead of all 25000 files at the same time as that will overload the system where I'm processing to.


This script will move 100 files (50 pairs).

Set $sourceDir and $destDir as appropriate. Remove the -WhatIf only if you are satisfied it will do what you want (until then it won't actually move anything; it will just tell you what it would move).

I'm assuming that the pattern to search for is two alphanumeric characters, and then then four digits ('\w{2}\d{4}'). You can tighten that up if you need to.

$sourceDir = "."
$destDir = "DestDir"
Get-ChildItem $sourceDir | 
    Group-Object -Property { $_ -match '\w{2}\d{4}'; $Matches[0] } | 
    Select-Object -First 100 -ExpandProperty Group | 
    Move-Item -WhatIf -Destination $destDir
  • Tried it out but unfortunately it's not working. The first 445 items were moved but only pdf documents, no name corresponding xml files. – Kris van der Mast Sep 8 '15 at 5:39
  • @XIII, You may need to fine-tune the regular expression, '\w{2}\d{4}', that is used to match up the files. Based on the two example file names that you gave, it should work. Do all the filenames have the same format as those two? – dangph Sep 8 '15 at 6:37
  • Yes they do. That's the only thing they're consistent of. – Kris van der Mast Sep 9 '15 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.