CSV remove full folder path and leave just the folder name and size information

Welcome Forums General PowerShell Q&A CSV remove full folder path and leave just the folder name and size information

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #161793

    Participant
    Topics: 1
    Replies: 2
    Points: 16
    Rank: Member

    I have CSV file that has some information like below.

    How can I remove the full path and have just the folder name and folder size information and output results back to csv file. I am new to PS and would appreciate any help on how to accomplish this.

    FolderName                                                                                                                     Size(GB)   Size(TB)
    ———- ——– ——–
    vms:\192.168.0.1.@222\Some-Datacenter\Volume-00\file-path\Folder1       111 GB       0.11 TB
    vms:\192.168.0.1.@222\Some-Datacenter\Volume-00\file-path\Folder2       121 GB      0.12 TB
    vms:\192.168.0.1.@222\Some-Datacenter\Volume-00\file-path\Folder3       131 GB      0.13 TB
    vms:\192.168.0.1.@222\Some-Datacenter\Volume-00\file-path\Folder4       141 GB      0.14 TB
    vms:\192.168.0.1.@222\Some-Datacenter\Volume-00\file-path\Folder5       151 GB       0.15 TB

    Desired Output Example:

    FolderName                       Size(GB)   Size(TB)
    ———- ——– ——–
    Folder1                                111 GB        0.11 TB
    Folder2                                121 GB       0.12 TB
    Folder3                                131 GB       0.13 TB
    Folder4                                141 GB       0.14 TB
    Folder5                                151 GB       0.15 TB

  • #161799

    Participant
    Topics: 37
    Replies: 155
    Points: 479
    Helping Hand
    Rank: Contributor

    Not very elegant...

    $Results = @()
    Get-Content "C:\temp\56.txt" | select-object -skip 2 | % {
    $item=$_-replace'(^\s+|\s+$)',''-replace'\s+',''
    $Results+=[PSCustomObject]@{
    Folder=(Split-Path$item.Split("")[0]-Leaf).Trim()
    SizeGB=$item.Split("")[1]+""+$item.Split("")[2]
    SizeTB=$item.Split("")[3]+""+$item.Split("")[4]
    }
    }
    # results $Results

     

    
    
    Folder SizeGB SizeTB
    ------ ------ ------
    Folder1 111 GB 0.11 TB
    Folder2 121 GB 0.12 TB
    Folder3 131 GB 0.13 TB
    Folder4 141 GB 0.14 TB
    Folder5 151 GB 0.15 TB

     

     

  • #161804

    Participant
    Topics: 1
    Replies: 302
    Points: 145
    Helping Hand
    Rank: Participant

    I would use Split-Path:

    $csv = Import-Csv E:\temp\example.csv
    
    foreach ($row in $csv) {
    
        $row.FolderName = $row.FolderName | Split-Path -Leaf
        $row | Export-Csv e:\temp\new_example.csv -Append -NoClobber -NoTypeInformation
    
    }
    
  • #161811

    Participant
    Topics: 1
    Replies: 2
    Points: 16
    Rank: Member
    Thank you so much Matt. That script worked brilliantly. I was looking into -replace comparison operator and did not read up on Split-Path -Leaf
  • #161814

    Participant
    Topics: 1
    Replies: 2
    Points: 16
    Rank: Member

    Thank you so much Matt. That script worked brilliantly. I was looking into -replace comparison operator and did not read up on Split-Path -Leaf

The topic ‘CSV remove full folder path and leave just the folder name and size information’ is closed to new replies.