Link to home
Create AccountLog in
Avatar of Tomasz Bojarski
Tomasz BojarskiFlag 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
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of 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
Avatar of Bill Prew
Bill Prew

Yes, let me adjust for that...


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


»bp
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Many thanks Bill.

This little script did the trick.

Regards
Tom
Welcome, glad that was helpful.


»bp