Avatar of Tomasz Bojarski
Tomasz Bojarski
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Sort and copy files based on parts of the name using PowerShell

I have a task to write a PS script in order to sort and copy files into specific folders based on their name.

Files are named following convention of:

DNK - FB - YouSee - S -AFC - R- S-Default-2017-10-05.csv
NOR - FB - YouSee - S -AFC - R- S-Default-2017-10-05.csv

Meaning behind the parts of the file name:

<Country>-<Service>-<Company> - rest does not really matter.

Folders however follow structure of:

D:\FB\DNK\YouSee\
D:\FB\NOR\YouSee\

Question:

What's the best way to sort and place the files in respective folders based on the first three parts of the name (Country-Service-Company?

Any help/suggestions appreciated.
Thanks
Tom
PowershellRegular Expressions

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Tomasz Bojarski

ASKER
Thanks Bill,

Works like a charm!

This is exactly what I needed. Is there a way to ignore the spaces in the file names so that folder names are created without them?

Currently folder names include the spaces from original file names. I could ask the app owner to make sure they use correct naming convention but chances are they will forget at some point.

Tom
Bill Prew

Yes, let me adjust for that...


»bp
Bill Prew

There are never spaces embeded in those folder names, is that true?  Just before or after...


»bp
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Tomasz Bojarski

ASKER
Many thanks Bill.

This little script did the trick.

Regards
Tom
Bill Prew

Welcome, glad that was helpful.


»bp