powershell - Making a custom dirlisting in PS2 -


please consider following directory tree:

root     dir1         dir11             x.l01 12kb             x.l02 10kb         dir12             dir122                 a.jpg 5kb                 b.xls 3kb                 c.bmp 3kb     dir2         a.l01 100kb         a.l02 200kb         a.l03  50kb     dir3         dir31     dir4 

there 3 possible cases:

  1. a (sub)dir empty; root/dir3/dir31 , root/dir4
  2. a (sub)dir contains (only) l0x files, x number; root/dir1/dir11 , root/dir2
  3. a (sub)dir has files, not of l0x-kind

the desired output custom directory listing 3 columns:

  1. filepath
  2. filesize
  3. lefcount (see below)

the logic follows:

  1. if (sub)dir empty, not list dir
  2. if (sub)dir contains (only) l0x files, list first 1 (root/dir1/dir11/x.l01) but count number of , total filesize of l01s
  3. if (sub)dir has other files, list dir, but count number of , total filesize of files

so example output be:

path                      size     count ---------------------------------------- root/dir1/dir11/x.l01      22kb        2 root/dir1/dir12/dir122     11kb        3 root/dir2/a.l01           350kb        3 

i'm beginning powershell, , have come following, not (a) going in right direction? , (b) how proceed here?

get-childitem "c:\root" -recurse | foreach-object {     if ($_.psiscontainer) {         get-childitem $_.fullname |         foreach-object {             write-host $_.fullname         }     } } 

any appreciated!

this can evolve needs change. create desired output custom object can manipulate , export required.

$rootpath = "c:\temp" get-childitem $rootpath -recurse |      where-object {$_.psiscontainer} |      where-object {(get-childitem $_.fullname | where-object {!$_.psiscontainer}|  measure-object | select-object -expandproperty count) -gt 0} |     foreach-object{          $files = get-childitem $_.fullname          $props = @{             path = $_.fullname             size = "{0:n0} kb" -f (($files | where-object {!$_.psiscontainer} |  measure-object -sum length | select-object -expandproperty sum) / 1024)             count = $files | measure-object | select-object -expandproperty count         }          if($files.extension -match "l\d\d"){             # these special files , assuming alone in directory             # change path             $props.path = $files | where-object {!$_.psiscontainer} | select-object -first 1 | select-object -expandproperty fullname         }          new-object -typename pscustomobject -property $props  } | select path,size,count 

get folders , files recursively $rootpath. filter out files , empty folders based on immediate contents. build custom object requested details. if turns out l0x files present update path first 1 found.

currently assume files of l0x format. if need can confirm.

sample output

path                                            size     count ----                                            ----     ----- c:\temp\win64                                   1,092 kb     2 c:\temp\empy\stuff\new text document - copy.l01 0 kb         2 

Comments