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
StratAnAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Pretty easy to do with a small BAT script (I know, you asked for PS, but this was so easy...).  I "moved" the files, but if you want to "copy" instead just change the MOVE to a COPY.   Adjust the folder names at the top (no trailing backslashes, so the root of D drive would be just D:).

@echo off
setlocal

set BaseDir=B:\EE\EE29065900\Files
set DestDir=B:\EE\EE29065900\Dest

for %%A in ("%BaseDir%\*-*-*-*.csv") do (
    for /f "tokens=1-3 delims=-" %%a in ("%%~nA") do (
        if not exist "%DestDir%\%%b\%%a\%%c\" md "%DestDir%\%%b\%%a\%%c\"
        move "%%~A" "%DestDir%\%%b\%%a\%%c\"
    )
)

Open in new window


»bp
0
StratAnAuthor Commented:
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
0
Bill PrewIT / Software Engineering ConsultantCommented:
Yes, let me adjust for that...


»bp
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Bill PrewIT / Software Engineering ConsultantCommented:
There are never spaces embeded in those folder names, is that true?  Just before or after...


»bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
This will remove any spaces around the three components of the file name that make up the destination folder path.  As long as there are no multiple word values for these folder names then this will be okay.

@echo off
setlocal EnableDelayedExpansion

set BaseDir=B:\EE\EE29065900\Files
set DestDir=B:\EE\EE29065900\Dest

for %%A in ("%BaseDir%\*-*-*-*.csv") do (
    for /f "tokens=1-3 delims=-" %%a in ("%%~nA") do (
        for %%z in (%%a) do set Country=%%z
        for %%z in (%%b) do set Service=%%z
        for %%z in (%%c) do set Company=%%z
        set ToDir=%DestDir%\!Service!\!Country!\!Company!
        if not exist "!ToDir!\" md "!ToDir!\"
        move "%%~A" "!ToDir!"
    )
)

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
StratAnAuthor Commented:
Many thanks Bill.

This little script did the trick.

Regards
Tom
0
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that was helpful.


»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.