PowerShell - Get-GhildItem - Ignore specific directory -


i working script clear old files off our file server. using line in script find files older date:

$oldfiles = get-childitem $oldpath -recurse | where-object { $_.lastwritetime -le $olddate } 

my question is, how ignore directory in $oldpath? instance, if had following:

  • root
    • dir1
    • dir 2
      • subdir 1
      • subdir 2
    • dir 3
      • subdir 1
    • dir 4

and want ignore dir 2 , subdirectories when building list

final working script:

$oldpath = "\\server\share" $newdrive = "i:" $olddate = get-date -date 1/1/2012  $oldfiles = get-childitem $oldpath -recurse -file | where-object {($_.psparentpath -notmatch '\\ignore directory')  -and $_.lastwritetime -le $olddate } $olddirs = get-childitem $oldpath -recurse | where-object {$_.psiscontainer -and ($_.psparentpath -notmatch '\\ignore directory')} | select-object fullname $olddirs = $olddirs | select -unique  foreach ($olddir in $olddirs) {     $strdir = $newdrive + "\" + ($olddir | split-path -noqualifier | out-string).trim().trim("\")     if (!(test-path $strdir)) {         write-host "$strdir not exist. creating directory..."         mkdir $strdir | out-null     } # end if } # end foreach  foreach ($file in $oldfiles) {     $strfile = $newdrive + "\" + ($file.fullname | split-path -noqualifier | out-string).trim().trim("\")     write-host "moving $file.fullname $strfile..."     move-item $file.fullname -destination $strfile -force -whatif } # end foreach  $oldfiles | select pspath | split-path -noqualifier | out-file "\\nelson\network share\archivedfiles.txt" 

modify where-object condition to:

... | where-object {($_.psparentpath -notmatch '\\dir 2') -and ($_.lastwritetime -le $olddate)} 

also, want filter out directory items $oldfiles contains files e.g.:

$oldfiles = get-childitem $oldpath -recurse | {!$_.psiscontainer -and ($_.psparentpath -notmatch '\\dir 2') -and ($_.lastwritetime -le $olddate)} 

if you're on powershell v3 can use new parameter on get-childitem simplify to:

$oldfiles = get-childitem $oldpath -recurse -file | {($_.psparentpath -notmatch '\\dir 2') -and ($_.lastwritetime -le $olddate)} 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -