现在的位置: 首页 > 综合 > 正文

powershell List Folder and File hierarchy

2012年09月04日 ⁄ 综合 ⁄ 共 1549字 ⁄ 字号 评论关闭

function getFile ( $folder )
{
 if (test-path $folder -pathtype Container )
  { 
   set-location $folder
   #write-host "============================================="
   $spacestring = getSpaceHolder $folder
   #write-host "$($spacestring)folder:$folder"
   out-file -FilePath "D:\PowershellScripts\MyFavorites.txt" -inputobject "$($spacestring)folder:$folder" -append
   $spacecounts = getCharFrequency $folder
   #write-host "`\ number counts: $spacecounts"
   get-childitem  | foreach-object -process { getFile $_.FullName }
  }
 else
    {
  $spacecounts = getCharFrequency $folder
  $spacestring = getSpaceHolder $folder
  #write-host "$($spacestring)$folder"
  #write-host "`\ number counts: $spacecounts"
  #write-host $folder
  out-file -FilePath "D:\PowershellScripts\MyFavorites.txt" -inputobject "$($spacestring)$folder" -append
    }
}
function getCharFrequency ($string)
{
 $frequency = 0
 $remainstring = $string
 while($remainstring.indexof("\") -gt 0 )
 {
  $frequency  = $frequency +1
  $remainstring = $remainstring.substring($remainstring.indexof("\")+1, $remainstring.Length-$remainstring.indexof("\")-1)
 }
 return $frequency 
}

function getSpaceHolder($string)
{
 $SpaceString = ""
 $Frequency = getCharFrequency $string
 $SpaceHolder = $Frequency -2
 $IncrementCount = 1
 while( $IncrementCount -lt $SpaceHolder)
 {
  $SpaceString = $SpaceString + "        "
  $IncrementCount = $IncrementCount  +1
 }
 return $SpaceString
}
 

set-location $args[0]
if ( test-path "D:\PowershellScripts\MyFavorites.txt" ) { remove-item "D:\PowershellScripts\MyFavorites.txt" }
get-childitem | foreach-object -process { getFile $_.FullName }
#get-childitem | foreach-object -process { write-host $_.FullName }

抱歉!评论已关闭.