none
Move files to folders base on file name RRS feed

  • General discussion

  • Hi  Gurus 

    folder where files located: C:/processdata/calldata/

    filnames

    data-121220171633.txt
    data-121220171534.txt
    data-121220171835.txt
    callesdata-121220170812.txt
    callesdata-121220171612.txt
    filedataresponce-121220171812.txt
    filedataresponce-121220171845.txt
    calldet-121220171745.txt
    calldet-121220172040.txt
    calldet-121220172141.txt

    Finally all files are moved to following location . These folders must be created through the script if not exist. many different files are coming to to this location. I gave an example set of files. 

    C:/processdata/calldata/data

    data-121220171633.txt
    data-121220171534.txt
    data-121220171835.txt

    C:/processdata/calldata/callesdata

    callesdata-121220170812.txt
    callesdata-121220171612.txt

    C:/processdata/calldata/filedataresponce

    filedataresponce-121220171812.txt
    filedataresponce-121220171845.txt

    C:/processdata/calldata/calldet

    calldet-121220171745.txt
    calldet-121220172040.txt
    calldet-121220172141.txt

    +++++++++++++++++++++Script I wrote++++++++++++++++++++

    $Directory = "C:/processdata/calldata/data";
    $AllFiles = Get-ChildItem $Directory | where {$_.extension -eq ".txt"};
    Write-Host  "$AllFiles"
    $FileNames = New-Object System.Collections.ArrayList;
    foreach($File in $AllFiles) 
    {
    $Index = $file.IndexOf('-');
    $Index=$index-1;
      $FileNames.Add($File.Name.SubString(0,$Index));  
    }
    $UniqueNames = $FileNames | get-unique;
    foreach($Name in $UniqueNames)
    {
    New-Item $DestinationFolder\$Name -type directory
    }
    foreach($File in $AllFiles)
    {
        $Dir = $File.Name.SubString(0,9);        
        Move-Item $Directory\$File $Directory\$Name\$File;
    }

    ++++++++++++++++++++++++++++++++++++++++++++

    Issue are 

    1.files not  move to corresponding folder .all files move to last created folder

    2.When folder exist does , then still try to create folders

    Its a great help if any one help me out. Thanks Heaps.

    regards


    • Edited by ashwan Tuesday, January 2, 2018 10:20 PM
    • Changed type Bill_Stewart Wednesday, February 14, 2018 7:21 PM
    • Moved by Bill_Stewart Wednesday, February 14, 2018 7:21 PM This is not "fix/debug/rewrite my script for me" forum
    Tuesday, January 2, 2018 10:18 PM

All replies