Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Recursively delete files with filter on subdirectories

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 07-09-2007   #1 (permalink)
Ashish
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 SpecsSystem Spec
Old 07-10-2007   #2 (permalink)
Keith Hill
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 SpecsSystem Spec
Old 07-10-2007   #3 (permalink)
Ashish
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 SpecsSystem Spec
Old 07-10-2007   #4 (permalink)
Brandon Shell
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 SpecsSystem Spec
Old 07-10-2007   #5 (permalink)
Keith Hill
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 SpecsSystem Spec
Old 07-10-2007   #6 (permalink)
Keith Hill
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 SpecsSystem Spec
Old 07-10-2007   #7 (permalink)
Brandon Shell
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 SpecsSystem Spec
Old 07-11-2007   #8 (permalink)
Jared
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 SpecsSystem Spec
Old 07-11-2007   #9 (permalink)
Kiron
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 SpecsSystem Spec
Old 07-11-2007   #10 (permalink)
Ashish
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 write
another 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 SpecsSystem Spec
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

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


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51