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?
|
|
"Ashish" <Ashish[ at ]discussions.microsoft.com> wrote in message news:3243ED71-FA29-4831-8C35-D36C3802B3BD[ at ]microsoft.com...
[Quoted Text] >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
|
|
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:
[Quoted Text] > > > "Ashish" <Ashish[ at ]discussions.microsoft.com> wrote in message > news:3243ED71-FA29-4831-8C35-D36C3802B3BD[ at ]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 > >
|
|
I would change where {$_.attributes -ne "Directory"} to where {$_.PSIsContainer -eq $false}
I guess I just dont trust attributes.
"Ashish" <Ashish[ at ]discussions.microsoft.com> wrote in message news:19A4C8B4-2AAE-43B4-B831-036390F33E4F[ at ]microsoft.com...
[Quoted Text] > 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[ at ]discussions.microsoft.com> wrote in message >> news:3243ED71-FA29-4831-8C35-D36C3802B3BD[ at ]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 >> >>
|
|
"Brandon Shell" <tshell.mask[ at ]mk.gmail.com> wrote in message news:#OYmS00wHHA.3444[ at ]TK2MSFTNGP05.phx.gbl...
[Quoted Text] >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
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_0039_01C7C338.E6678F80 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
"Ashish" <Ashish[ at ]discussions.microsoft.com> wrote in message = news:19A4C8B4-2AAE-43B4-B831-036390F33E4F[ at ]microsoft.com...
[Quoted Text] > Thanks Keith! That almost did it the trick.. I was able to make it =
work with=20 > a minor modification > get-childitem $rootDir\* -force -recurse -exclude $excludeTypes | ?=20 > {$excludeDirs -notcontains (split-path $_ -parent)} | where = {$_.attributes=20 > -ne "Directory"} | remove-item -whatif
With what Brandon mentioned I would simplify it to:
get-childitem $rootDir\* -force -recurse -exclude $excludeTypes |=20 ?{!$_.PSISContainer -and ($excludeDirs -notcontains (split-path $_ = -parent))} |=20 remove-item -whatif
-- Keith
------=_NextPart_000_0039_01C7C338.E6678F80 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
=FF=FE<=00!=00D=00O=00C=00T=00Y=00P=00E=00 =00H=00T=00M=00L=00 = =00P=00U=00B=00L=00I=00C=00 = =00"=00-=00/=00/=00W=003=00C=00/=00/=00D=00T=00D=00 =00H=00T=00M=00L=00 = =004=00.=000=00 = =00T=00r=00a=00n=00s=00i=00t=00i=00o=00n=00a=00l=00/=00/=00E=00N=00"=00>=00= =0D=00=0A= =00<=00H=00T=00M=00L=00>=00<=00H=00E=00A=00D=00>=00=0D=00=0A= =00<=00M=00E=00T=00A=00 = =00h=00t=00t=00p=00-=00e=00q=00u=00i=00v=00=3D=00C=00o=00n=00t=00e=00n=00= t=00-=00T=00y=00p=00e=00 = =00c=00o=00n=00t=00e=00n=00t=00=3D=00"=00t=00e=00x=00t=00/=00h=00t=00m=00= l=00;=00 = =00c=00h=00a=00r=00s=00e=00t=00=3D=00u=00n=00i=00c=00o=00d=00e=00"=00>=00= =0D=00=0A= =00<=00M=00E=00T=00A=00 = =00c=00o=00n=00t=00e=00n=00t=00=3D=00"=00M=00S=00H=00T=00M=00L=00 = =006=00.=000=000=00.=006=000=000=000=00.=001=006=004=008=001=00"=00 = =00n=00a=00m=00e=00=3D=00G=00E=00N=00E=00R=00A=00T=00O=00R=00>=00<=00/=00= H=00E=00A=00D=00>=00=0D=00=0A= =00<=00B=00O=00D=00Y=00 = =00i=00d=00=3D=00M=00a=00i=00l=00C=00o=00n=00t=00a=00i=00n=00e=00r=00B=00= o=00d=00y=00 =00=0D=00=0A= =00s=00t=00y=00l=00e=00=3D=00"=00P=00A=00D=00D=00I=00N=00G=00-=00R=00I=00= G=00H=00T=00:=00 =001=000=00p=00x=00;=00 = =00P=00A=00D=00D=00I=00N=00G=00-=00L=00E=00F=00T=00:=00 = =001=000=00p=00x=00;=00 =00F=00O=00N=00T=00-=00S=00I=00Z=00E=00:=00 = =001=000=00p=00t=00;=00 =00C=00O=00L=00O=00R=00:=00 = =00#=000=000=000=000=000=000=00;=00 = =00P=00A=00D=00D=00I=00N=00G=00-=00T=00O=00P=00:=00 = =001=005=00p=00x=00;=00 = =00F=00O=00N=00T=00-=00F=00A=00M=00I=00L=00Y=00:=00 = =00A=00r=00i=00a=00l=00"=00 =00=0D=00=0A= =00b=00g=00C=00o=00l=00o=00r=00=3D=00#=00f=00f=00f=00f=00f=00f=00 = =00l=00e=00f=00t=00M=00a=00r=00g=00i=00n=00=3D=000=00 = =00t=00o=00p=00M=00a=00r=00g=00i=00n=00=3D=000=00 = =00C=00a=00n=00v=00a=00s=00T=00a=00b=00S=00t=00o=00p=00=3D=00"=00t=00r=00= u=00e=00"=00 =00=0D=00=0A= =00n=00a=00m=00e=00=3D=00"=00C=00o=00m=00p=00o=00s=00e=00 = =00m=00e=00s=00s=00a=00g=00e=00 =00a=00r=00e=00a=00"=00 = =00a=00c=00c=00_=00r=00o=00l=00e=00=3D=00"=00t=00e=00x=00t=00"=00>=00"=00= A=00s=00h=00i=00s=00h=00"=00 =00=0D=00=0A= =00&=00l=00t=00;=00A=00s=00h=00i=00s=00h=00[ at ]=00d=00i=00s=00c=00u=00s=00s=00= i=00o=00n=00s=00.=00m=00i=00c=00r=00o=00s=00o=00f=00t=00.=00c=00o=00m=00&= =00g=00t=00;=00 =00w=00r=00o=00t=00e=00 =00i=00n=00 = =00m=00e=00s=00s=00a=00g=00e=00 =00=0D=00=0A= =00n=00e=00w=00s=00:=001=009=00A=004=00C=008=00B=004=00-=002=00A=00A=00E=00= -=004=003=00B=004=00-=00B=008=003=001=00-=000=003=006=003=009=000=00F=003= =003=00E=004=00F=00[ at ]=00m=00i=00c=00r=00o=00s=00o=00f=00t=00.=00c=00o=00m=00= ..=00.=00.=00<=00B=00R=00>=00&=00g=00t=00;=00 =00T=00h=00a=00n=00k=00s=00 = =00K=00e=00i=00t=00h=00!=00 =00=0D=00=0A= =00T=00h=00a=00t=00 =00a=00l=00m=00o=00s=00t=00 =00d=00i=00d=00 = =00i=00t=00 =00t=00h=00e=00 =00t=00r=00i=00c=00k=00.=00.=00 =00I=00 = =00w=00a=00s=00 =00a=00b=00l=00e=00 =00t=00o=00 =00m=00a=00k=00e=00 = =00i=00t=00 =00w=00o=00r=00k=00 =00w=00i=00t=00h=00 = =00<=00B=00R=00>=00&=00g=00t=00;=00 =00a=00 =00m=00i=00n=00o=00r=00 = =00=0D=00=0A= =00m=00o=00d=00i=00f=00i=00c=00a=00t=00i=00o=00n=00<=00B=00R=00>=00&=00g=00= t=00;=00 =00g=00e=00t=00-=00c=00h=00i=00l=00d=00i=00t=00e=00m=00 = =00$=00r=00o=00o=00t=00D=00i=00r=00\=00*=00 =00-=00f=00o=00r=00c=00e=00 = =00-=00r=00e=00c=00u=00r=00s=00e=00 =00-=00e=00x=00c=00l=00u=00d=00e=00 = =00=0D=00=0A= =00$=00e=00x=00c=00l=00u=00d=00e=00T=00y=00p=00e=00s=00 =00|=00 =00?=00 = =00<=00B=00R=00>=00&=00g=00t=00;=00 = =00{=00$=00e=00x=00c=00l=00u=00d=00e=00D=00i=00r=00s=00 = =00-=00n=00o=00t=00c=00o=00n=00t=00a=00i=00n=00s=00 = =00(=00s=00p=00l=00i=00t=00-=00p=00a=00t=00h=00 =00$=00_=00 = =00-=00p=00a=00r=00e=00n=00t=00)=00}=00 =00=0D=00=0A= =00|=00&=00n=00b=00s=00p=00;=00 =00w=00h=00e=00r=00e=00 = =00{=00$=00_=00.=00a=00t=00t=00r=00i=00b=00u=00t=00e=00s=00 = =00<=00B=00R=00>=00&=00g=00t=00;=00 =00-=00n=00e=00 = =00"=00D=00i=00r=00e=00c=00t=00o=00r=00y=00"=00}=00 =00|=00 = =00r=00e=00m=00o=00v=00e=00-=00i=00t=00e=00m=00 =00=0D=00=0A= =00-=00w=00h=00a=00t=00i=00f=00<=00B=00R=00>=00<=00B=00R=00>=00W=00i=00t=00= h=00 =00w=00h=00a=00t=00 =00B=00r=00a=00n=00d=00o=00n=00 = =00m=00e=00n=00t=00i=00o=00n=00e=00d=00 =00I=00 =00w=00o=00u=00l=00d=00 = =00s=00i=00m=00p=00l=00i=00f=00y=00 =00i=00t=00 = =00t=00o=00:=00<=00B=00R=00>=00<=00B=00R=00>=00<=00F=00O=00N=00T=00 = =00=0D=00=0A= =00f=00a=00c=00e=00=3D=00C=00o=00u=00r=00i=00e=00r=00>=00g=00e=00t=00-=00= c=00h=00i=00l=00d=00i=00t=00e=00m=00 = =00$=00r=00o=00o=00t=00D=00i=00r=00\=00*=00 =00-=00f=00o=00r=00c=00e=00 = =00-=00r=00e=00c=00u=00r=00s=00e=00 =00-=00e=00x=00c=00l=00u=00d=00e=00 = =00$=00e=00x=00c=00l=00u=00d=00e=00T=00y=00p=00e=00s=00 =00=0D=00=0A= =00|=00&=00n=00b=00s=00p=00;=00<=00B=00R=00>=00?=00{=00!=00$=00_=00.=00P=00= S=00I=00S=00C=00o=00n=00t=00a=00i=00n=00e=00r=00 =00-=00a=00n=00d=00 = =00(=00$=00e=00x=00c=00l=00u=00d=00e=00D=00i=00r=00s=00 = =00-=00n=00o=00t=00c=00o=00n=00t=00a=00i=00n=00s=00 = =00(=00s=00p=00l=00i=00t=00-=00p=00a=00t=00h=00 =00$=00_=00 =00=0D=00=0A= =00-=00p=00a=00r=00e=00n=00t=00)=00)=00}=00 = =00|=00&=00n=00b=00s=00p=00;=00<=00B=00R=00>=00r=00e=00m=00o=00v=00e=00-=00= i=00t=00e=00m=00 =00=0D=00=0A= =00-=00w=00h=00a=00t=00i=00f=00<=00B=00R=00>=00<=00/=00F=00O=00N=00T=00>=00= <=00B=00R=00>=00-=00-=00<=00B=00R=00>=00K=00e=00i=00t=00h=00<=00B=00R=00>= =00<=00/=00B=00O=00D=00Y=00>=00<=00/=00H=00T=00M=00L=00>=00=0D=00=0A= =00 ------=_NextPart_000_0039_01C7C338.E6678F80--
|
|
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[ at ]mailhot.moc.no_spam_I> wrote in message news:20AB0D45-B640-45B9-AAF2-FA6A702AA730[ at ]microsoft.com...
[Quoted Text] > "Brandon Shell" <tshell.mask[ at ]mk.gmail.com> wrote in message > news:#OYmS00wHHA.3444[ at ]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
|
|
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:
[Quoted Text] > "Ashish" <Ashish[ at ]discussions.microsoft.com> wrote in message > news:19A4C8B4-2AAE-43B4-B831-036390F33E4F[ at ]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 >
|
|
|
|
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:
[Quoted Text] > "Ashish" <Ashish[ at ]discussions.microsoft.com> wrote in message > news:19A4C8B4-2AAE-43B4-B831-036390F33E4F[ at ]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 >
|
|
"Ashish"
[Quoted Text] > I am wondering how this would affect > performance if my directory heirarchy is > pretty long. Any ideas? >
Perhaps Microsoft's Log Parser 2.2
PS> LogParser -h -i:FS
Input format: FS (FileSystem properties) Returns properties of files and folders
Perhaps use Log Parser to get the folders (names and locations) and then remove them via PowerShell.
Just another way!
|
|
"Ashish" <Ashish[ at ]discussions.microsoft.com> wrote in message news:068C451C-8526-45FF-AA5D-85C17B638326[ at ]microsoft.com...
[Quoted Text] > 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?
Slight modification. Try this:
get-childitem $rootDir\* -force -recurse -exclude $excludeTypes | ?{!$_.PSISContainer -and !$($path=$_.fullname; $excludeDirs | ?{$path -like "$_*"})} | remove-item -whatif
-- Keith
|
|