locked
Sort and copy files based on parts of the name RRS feed

  • Question

  • 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 at this point.

    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?

    Also, notice there are spaces in the file names so these need to be ignored.

    Any help/suggestions appreciated.

    Thanks
    Tom

    Wednesday, November 1, 2017 4:57 PM

All replies

  • Original working BAT script:

    @echo off
    setlocal
    
    set BaseDir=D:\BaseFolder
    set DestDir=D:\DestFolder
    
    for %%A in ("%BaseDir%\*-*-*-*.*") do (
        for /f "tokens=1-3 delims=-" %%a in ("%%~nA") do (
            if not exist "%DestDir%\%%b\%%a\%%c\" md "%DestDir%\%%b\%%a\%%c\"
            copy "%%~A" "%DestDir%\%%b\%%a\%%c\"
        )
    )

    Wednesday, November 1, 2017 5:00 PM
  • Original working BAT script:

    @echo off
    setlocal
    
    set BaseDir=D:\BaseFolder
    set DestDir=D:\DestFolder
    
    for %%A in ("%BaseDir%\*-*-*-*.*") do (
        for /f "tokens=1-3 delims=-" %%a in ("%%~nA") do (
            if not exist "%DestDir%\%%b\%%a\%%c\" md "%DestDir%\%%b\%%a\%%c\"
            copy "%%~A" "%DestDir%\%%b\%%a\%%c\"
        )
    )

    Since this is a PowerShell forum your batch file would not be considerable as an answer.


    \_(ツ)_/

    Wednesday, November 1, 2017 5:02 PM
  • Get-Childitem c:\myfolder\*.csv |
        ForEach-Object{
            $parts = $_.BaseName -split ' - '
            $path = [io.path]::Combine('D:',$parts[1],$parts[0],$parts[2])
            Write-Host $path
            Move-Item $_ -Destination $path
        }

    You can just extract and build the name:


    \_(ツ)_/

    Wednesday, November 1, 2017 5:12 PM
  • Thanks for the quick reply!

    I have updated it as follows in order to use more parts of the name and to create directory.

    Get-Childitem D:\Unsorted\*.* |
        ForEach-Object{
            $parts = $_.BaseName -split '-'
            $path = [io.path]::Combine('D:\Sorted',$parts[1],$parts[0],$parts[2],$parts[6])
            Write-Host $path
            New-Item -Path $path -Type Directory
            Copy-Item $_ -Destination $path
        }

    Q1: How do I ignore spaces before/after "-" character? I expect file owners will create them both with and without spaces. I don't want the folder names to include those spaces either.

    Q2: How do I check to see if the DIR already exists so I don't get:

    D:\Sorted\FB\NOR\Get\S
    New-Item : An item with the specified name D:\Sorted\FB\NOR\Get\S already exists.
    At D\Scripts\Sort_files_2.ps1:6 char:9
    +         New-Item -Path $path -Type Directory
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceExists: (D:\Mozenda_sorted\FB\NOR\Get\S:String) [New-Item], IOException
        + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand

    Regards

    Tom


    • Edited by bojar68 Wednesday, November 1, 2017 6:17 PM Reading layout adjustment
    Wednesday, November 1, 2017 6:16 PM
  • My code ignores the spaces but you changed that.

    New-Item -Path$path -Type Directory -Force

    This will test the folder path before trying to create the folder.


    \_(ツ)_/

    Wednesday, November 1, 2017 6:20 PM
  • Thanks again

    What I would like to know if there a way to have it process files with and without spaces in the names. Currently I can have one or the other but not both. Reason I am asking is app owner insists on using spaces  but I know someone else will come in and remove them eventually. 

    When I add the spaces back in it starts creating folders with partial file names which is not what I was after.

    Perhaps running another function to remove all spaces first would address this?

    Thanks

    Tom

    • Proposed as answer by jrv Thursday, November 2, 2017 2:37 PM
    • Unproposed as answer by jrv Thursday, November 2, 2017 2:37 PM
    Thursday, November 2, 2017 10:30 AM
  • Please try to understand the vagueness of your question and use the patter as I posted it.  If there are more spaces then exist in your example then use this pattern:

    -split '\s+-\s+'

    I think the only problem is that you removed the space from my original code.

    If the input strings do not match the pattern you posted then you will have to post more accurate examples.


    \_(ツ)_/

    Thursday, November 2, 2017 2:40 PM