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

StratAn
StratAn used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016
Commented:
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

Author

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
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Yes, let me adjust for that...


»bp
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Bill PrewTest your restores, not your backups...
Top Expert 2016

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


»bp
Test your restores, not your backups...
Top Expert 2016
Commented:
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

Author

Commented:
Many thanks Bill.

This little script did the trick.

Regards
Tom
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Welcome, glad that was helpful.


»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial