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?
 
Bill PrewCommented:
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
 
Bill PrewCommented:
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
Bill PrewCommented:
Yes, let me adjust for that...


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


»bp
0
 
StratAnAuthor Commented:
Many thanks Bill.

This little script did the trick.

Regards
Tom
0
 
Bill PrewCommented:
Welcome, glad that was helpful.


»bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.