question

AlbertEdelstein-0884 avatar image
0 Votes"
AlbertEdelstein-0884 asked AlbertEdelstein-0884 answered

PS help with adding if statement to update File extension value.

Hi Everyone,
The scrips exports Folder, subfolder files and their attributes. I want to modify the script to add a File/Folder column
How can I add a column based on the "FILE_Type" value?
If FILE_Type = "" (Blank), the value should be "Folder" else it Should be file.

Or another option is that if FILE_Type is blank the value should be "Folder"


$dir = "C:_Files"

$results = @()

gci $dir -Recurse -Depth 0 | % {

 $temp = [ordered]@{
     #NAME            = $_
     FILE_COUNT      = (gci -File $_.FullName -Recurse | measure | select -ExpandProperty Count)
     FOLDER_COUNT    = (gci -Directory $_.FullName -Recurse | measure | select -ExpandProperty Count)
     DIRECTORY_PATH  = $_.Fullname
     SIZE            = "{0:N2} MB" -f ((gci $_.Fullname -Recurse | measure -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB)
     FILE_Type        = $_.Extension
     Charchter_Count =  $_.fullname.length
     Created         =   $_.CreationTime
     Modified        = $_.LastWriteTime


 }

 $results += New-Object PSObject -Property $temp

}

$results | export-csv -Path "C:\Temp\Dept.csv" -NoTypeInformation

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

The text of your post seems to be at odds with the code and the post's subject regarding the "FILE_Type" property! Your code says to get the file extension, but the the text asks for it to be either the string "Directory" of "File".

See if this is any help:

 $dir = "C:_Files"
    
 Get-ChildItem $dir -Recurse -Depth 0 | 
     ForEach-Object {
         [PSCustomObject]@{
             NAME            = Split-Path $_.FullName -Leaf
             FILE_COUNT      = (gci -File $_.FullName -Recurse).count
             FOLDER_COUNT    = (gci -Directory $_.FullName -Recurse).count
             DIRECTORY_PATH  = $_.FullName | Split-Path -Parent
             SIZE            = "{0:N2} MB" -f ((gci $_.Fullname -Recurse | measure -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB)
             FILE_Type        = If ($_.PSIsContainer){"Directory"} else {"File"}
             Character_Count =  $_.fullname.length
             Created         =   $_.CreationTime
             Modified        = $_.LastWriteTime
         }
     } | export-csv -Path "C:\Junk\Dept.csv" -NoTypeInformation

Why have you used the "-Recursive" switch and then set the depth to "0"????

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlbertEdelstein-0884 avatar image
0 Votes"
AlbertEdelstein-0884 answered

I modified your script: FILE_Type = If ($_.PSIsContainer){"Directory"} else {"File"}

to: FILE_Type = (&{if($.Extension -eq ""){"Folder"} else {$.Extension}})

Thank you

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.