powershell - How to exlude a directory -
i'm beginner on powershell scripting keep cool ^^
i delete file on directory d:\test delete file more 15 days i'don't want delete files on directory in directory d:\test.
my script
#----- define parameters -----# #----- current date ----# $now = get-date #----- define amount of days ----# $days = "15" #----- define folder files located ----# $targetfolder = "d:\test" #----- define extension ----# $extension = "*.bak" #----- define lastwritetime parameter based on $days ---# $lastwrite = $now.adddays(-$days) #----- files based on lastwrite filter , specified folder ---# $nomatch = "d:\test\zz - archives","d:\test\zz - cloture paye" $files = get-childitem $targetfolder -include $extension -recurse | {$_.lastwritetime -le "$lastwrite"} | where-object {$_.fullname -notmatch "$nomatch"} foreach ($file in $files) { if ($file -ne $null) { write-host "deleting file $file" -foregroundcolor "darkred" remove-item $file.fullname | out-null } else { write-host "no more files delete!" -foregroundcolor "green" } }
if on powershell 2.0, files:
get-childitem $targetfolder | where-object { ! $_.psiscontainer }
if on powershell 3.0, do:
get-childitem $targetfolder -file
i don't know if want -recurse
. if want files @ d:\test
, not files under child directories too, don't use -recurse
.
Comments
Post a Comment