![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Recursively delete files with filter on subdirectories I am trying to achieve a small cmdlet. <Root> <Dir1> <Dir11> - file1.txt - file2.log <Dir12> <Dir2> <Dir21> - file3.txt - file4.log - file5.ini <Dir211> file6.txt Lets say I want to remove all files except extn *.log from <RootDir> excluding any files under <Root\Dir1\Dir11> and <Root\Dir2\Dir21\Dir211> So my operation should delete file3.txt & file5.ini The closest i could get to was this... $rootDir = "F:\Test" $excludeDir ="Dir1\Dir11", "Dir2\Dir21\Dir211" $excludeExt = "*.log" get-childitem $rootDir -exclude $excludeDir | foreach{ Get-ChildItem $_.FullName -Force -Recurse -exclude $exclis t1} | where {$_.attributes -ne "Directory"} | foreach ($_) {remove-item -force -recurse $_.fullname} Is there any way i can tweak this code to achieve what i need? |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Recursively delete files with filter on subdirectories "Ashish" <Ashish@discussions.microsoft.com> wrote in message news:3243ED71-FA29-4831-8C35-D36C3802B3BD@microsoft.com... >I am trying to achieve a small cmdlet. > <Root> > <Dir1> > <Dir11> > - file1.txt > - file2.log > <Dir12> > <Dir2> > <Dir21> > - file3.txt > - file4.log > - file5.ini > <Dir211> > file6.txt > Lets say I want to remove all files except extn *.log from <RootDir> > excluding any files under <Root\Dir1\Dir11> and <Root\Dir2\Dir21\Dir211> > So my operation should delete file3.txt & file5.ini > > The closest i could get to was this... > $rootDir = "F:\Test" > $excludeDir ="Dir1\Dir11", "Dir2\Dir21\Dir211" > $excludeExt = "*.log" > get-childitem $rootDir -exclude $excludeDir | foreach{ Get-ChildItem > $_.FullName -Force -Recurse -exclude $exclis > t1} | where {$_.attributes -ne "Directory"} | foreach ($_) {remove-item > -force -recurse $_.fullname} > > Is there any way i can tweak this code to achieve what i need? Try this (note: this wasn't tested): $rootDir = "F:\Test" $excludeDirs = "$rootDir\Dir1\Dir11", "$rootDir\Dir2\Dir21\Dir211" get-childitem $rootDir\* -force -recurse -exclude *.log | ? {$excludeDirs -notcontains (split-path $_ -parent)} | remove-item -whatif -- Keith |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Recursively delete files with filter on subdirectories Thanks Keith! That almost did it the trick.. I was able to make it work with a minor modification get-childitem $rootDir\* -force -recurse -exclude $excludeTypes | ? {$excludeDirs -notcontains (split-path $_ -parent)} | where {$_.attributes -ne "Directory"} | remove-item -whatif "Keith Hill" wrote: > > > "Ashish" <Ashish@discussions.microsoft.com> wrote in message > news:3243ED71-FA29-4831-8C35-D36C3802B3BD@microsoft.com... > >I am trying to achieve a small cmdlet. > > <Root> > > <Dir1> > > <Dir11> > > - file1.txt > > - file2.log > > <Dir12> > > <Dir2> > > <Dir21> > > - file3.txt > > - file4.log > > - file5.ini > > <Dir211> > > file6.txt > > Lets say I want to remove all files except extn *.log from <RootDir> > > excluding any files under <Root\Dir1\Dir11> and <Root\Dir2\Dir21\Dir211> > > So my operation should delete file3.txt & file5.ini > > > > The closest i could get to was this... > > $rootDir = "F:\Test" > > $excludeDir ="Dir1\Dir11", "Dir2\Dir21\Dir211" > > $excludeExt = "*.log" > > get-childitem $rootDir -exclude $excludeDir | foreach{ Get-ChildItem > > $_.FullName -Force -Recurse -exclude $exclis > > t1} | where {$_.attributes -ne "Directory"} | foreach ($_) {remove-item > > -force -recurse $_.fullname} > > > > Is there any way i can tweak this code to achieve what i need? > > Try this (note: this wasn't tested): > > $rootDir = "F:\Test" > $excludeDirs = "$rootDir\Dir1\Dir11", "$rootDir\Dir2\Dir21\Dir211" > get-childitem $rootDir\* -force -recurse -exclude *.log | ? > {$excludeDirs -notcontains (split-path $_ -parent)} | remove-item -whatif > > -- > Keith > > |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: Recursively delete files with filter on subdirectories I would change where {$_.attributes -ne "Directory"} to where {$_.PSIsContainer -eq $false} I guess I just dont trust attributes. "Ashish" <Ashish@discussions.microsoft.com> wrote in message news:19A4C8B4-2AAE-43B4-B831-036390F33E4F@microsoft.com... > Thanks Keith! That almost did it the trick.. I was able to make it work > with > a minor modification > get-childitem $rootDir\* -force -recurse -exclude $excludeTypes | ? > {$excludeDirs -notcontains (split-path $_ -parent)} | where > {$_.attributes > -ne "Directory"} | remove-item -whatif > > > "Keith Hill" wrote: > >> >> >> "Ashish" <Ashish@discussions.microsoft.com> wrote in message >> news:3243ED71-FA29-4831-8C35-D36C3802B3BD@microsoft.com... >> >I am trying to achieve a small cmdlet. >> > <Root> >> > <Dir1> >> > <Dir11> >> > - file1.txt >> > - file2.log >> > <Dir12> >> > <Dir2> >> > <Dir21> >> > - file3.txt >> > - file4.log >> > - file5.ini >> > <Dir211> >> > file6.txt >> > Lets say I want to remove all files except extn *.log from <RootDir> >> > excluding any files under <Root\Dir1\Dir11> and >> > <Root\Dir2\Dir21\Dir211> >> > So my operation should delete file3.txt & file5.ini >> > >> > The closest i could get to was this... >> > $rootDir = "F:\Test" >> > $excludeDir ="Dir1\Dir11", "Dir2\Dir21\Dir211" >> > $excludeExt = "*.log" >> > get-childitem $rootDir -exclude $excludeDir | foreach{ Get-ChildItem >> > $_.FullName -Force -Recurse -exclude $exclis >> > t1} | where {$_.attributes -ne "Directory"} | foreach ($_) >> > {remove-item >> > -force -recurse $_.fullname} >> > >> > Is there any way i can tweak this code to achieve what i need? >> >> Try this (note: this wasn't tested): >> >> $rootDir = "F:\Test" >> $excludeDirs = "$rootDir\Dir1\Dir11", "$rootDir\Dir2\Dir21\Dir211" >> get-childitem $rootDir\* -force -recurse -exclude *.log | ? >> {$excludeDirs -notcontains (split-path $_ -parent)} | remove-item -whatif >> >> -- >> Keith >> >> |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: Recursively delete files with filter on subdirectories "Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message news:#OYmS00wHHA.3444@TK2MSFTNGP05.phx.gbl... >I would change > where {$_.attributes -ne "Directory"} > to > where {$_.PSIsContainer -eq $false} > > I guess I just dont trust attributes. +1. It is more OO. :-) -- Keith |
My System Specs![]() |
| | #6 (permalink) |
| Guest | Re: Recursively delete files with filter on subdirectories "Ashish" <Ashish@discussions.microsoft.com> wrote in message news:19A4C8B4-2AAE-43B4-B831-036390F33E4F@microsoft.com... > Thanks Keith! That almost did it the trick.. I was able to make it work with > a minor modification > get-childitem $rootDir\* -force -recurse -exclude $excludeTypes | ? > {$excludeDirs -notcontains (split-path $_ -parent)} | where {$_.attributes > -ne "Directory"} | remove-item -whatif With what Brandon mentioned I would simplify it to: get-childitem $rootDir\* -force -recurse -exclude $excludeTypes | ?{!$_.PSISContainer -and ($excludeDirs -notcontains (split-path $_ -parent))} | remove-item -whatif -- Keith |
My System Specs![]() |
| | #7 (permalink) |
| Guest | Re: Recursively delete files with filter on subdirectories For a more practical reason other than I don't trust attributes. The previous version would delete Hidden Directory, Compress Directory, and ReadOnly Directorys... along with any Directory that has any attribute at all. p.s. I KNEW I didnt trust them attributes ![]() "Keith Hill" <r_keith_hill@mailhot.moc.no_spam_I> wrote in message news:20AB0D45-B640-45B9-AAF2-FA6A702AA730@microsoft.com... > "Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message > news:#OYmS00wHHA.3444@TK2MSFTNGP05.phx.gbl... >>I would change >> where {$_.attributes -ne "Directory"} >> to >> where {$_.PSIsContainer -eq $false} >> >> I guess I just dont trust attributes. > > +1. It is more OO. :-) > > -- > Keith |
My System Specs![]() |
| | #8 (permalink) |
| Guest | Re: Recursively delete files with filter on subdirectories Hi all, I found this to be quite helpful as I am trying to accomplish something very similar, but I would like a powershell cmdlet that would delete all files in all subdirectories over a certain age. I don't need to exclude directories but I need to only delete files older than 7 days. Can anyone please help provide a sample of this? Thanks in advance. "Keith Hill" wrote: > "Ashish" <Ashish@discussions.microsoft.com> wrote in message > news:19A4C8B4-2AAE-43B4-B831-036390F33E4F@microsoft.com... > > Thanks Keith! That almost did it the trick.. I was able to make it work > with > > a minor modification > > get-childitem $rootDir\* -force -recurse -exclude $excludeTypes | ? > > {$excludeDirs -notcontains (split-path $_ -parent)} | where > {$_.attributes > > -ne "Directory"} | remove-item -whatif > > With what Brandon mentioned I would simplify it to: > > get-childitem $rootDir\* -force -recurse -exclude $excludeTypes | > ?{!$_.PSISContainer -and ($excludeDirs -notcontains (split-path > $_ -parent))} | > remove-item -whatif > > -- > Keith > |
My System Specs![]() |
| | #9 (permalink) |
| Guest | Re: Recursively delete files with filter on subdirectories get-childItem -recurse | where-object {!$_.PSIsContainer -and $_.lastWriteTime -lt (get-date).addDays(-7)} | remove-item ....if the files are hidden or read-only include the -force parameter on both Cmdlets: get-childItem -recurse -force | where-object {!$_.PSIsContainer -and $_.lastWriteTime -lt (get-date).addDays(-7)} | remove-item -force http://blogs.msdn.com/powershell/arc...owerShell.aspx http://blogs.msdn.com/powershell/arc...Functions.aspx -- Kiron |
My System Specs![]() |
| | #10 (permalink) |
| Guest | Re: Recursively delete files with filter on subdirectories Guess there is a small hitch in this logic. If the directory i want to exclude has subdirectories, then this wouldnt exclude the files on the subdirectories. So if my $excludedirs contains "F:\test\d1", this command would still remove a file "F:\test\d1\d11\file1.txt" I'm afraid this is getting a bit complicated now Do i have to writeanother loop to iterate through all the parent directories of a file..I am wondering how this would affect performance if my directory heirarchy is pretty long. Any ideas? "Keith Hill" wrote: > "Ashish" <Ashish@discussions.microsoft.com> wrote in message > news:19A4C8B4-2AAE-43B4-B831-036390F33E4F@microsoft.com... > > Thanks Keith! That almost did it the trick.. I was able to make it work > with > > a minor modification > > get-childitem $rootDir\* -force -recurse -exclude $excludeTypes | ? > > {$excludeDirs -notcontains (split-path $_ -parent)} | where > {$_.attributes > > -ne "Directory"} | remove-item -whatif > > With what Brandon mentioned I would simplify it to: > > get-childitem $rootDir\* -force -recurse -exclude $excludeTypes | > ?{!$_.PSISContainer -and ($excludeDirs -notcontains (split-path > $_ -parent))} | > remove-item -whatif > > -- > Keith > |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Get-Childitem filter files with multiple extensions? | akcorr | PowerShell | 5 | 07-16-2008 12:55 PM |
| How to delete "old" directories (recursively)? | David Stuart | PowerShell | 3 | 04-15-2008 04:57 PM |
| taking ownership (recursively would be nice) and unlocking files i | Ben Christian | PowerShell | 1 | 03-08-2008 04:51 AM |
| Windows Media Player 11 album art and subdirectories | Extracampine | Vista General | 1 | 02-15-2007 09:38 AM |
| help. Digital filter? how to de-filter? USB audio device | Vista hardware & devices | 0 | 12-09-2006 04:30 PM | |