Delete files in a sharepoint library except one folder

I am trying to use a script. but i only want to delete files in the root of document library and not one specific folder.  basically when the script runs, it looks at the library and delete all files except in a folder called Archive. I use below script but it also delete files in the Archive folder. Can someone help me?

$web = Get-SPWeb "my site"
$Libraries = $web.Lists | where {$_.Title -eq "Shared Documents" }
 
foreach ($library in $Libraries) {
 
    Write-Output "Getting files from $($library.Title)"
        $Files = $library.Items | where {$_.FileSystemObjectType -eq "File" -And ($_.File -Like "*.pdf" -Or $_.File -Like "*.docx" -Or $_.File -Like "*.doc")}
     foreach ($file in $Files) {
        Write-Output "Deleting file $($file.Name)..."
        $file.Delete()
    }
}

Open in new window

stratomanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
the SPFile has a property "ParentFolder" which you can use to compare:
$web = Get-SPWeb "http://dev.rainerj.local/sites/ee"
$Libraries = $web.Lists | where {$_.Title -eq "Shared Documents" }
 
foreach ($library in $Libraries) {
 
    Write-Output "Getting files from $($library.Title)"
        $Files = $library.Items | where {$_.FileSystemObjectType -eq "File" -And ($_.File -Like "*.pdf" -Or $_.File -Like "*.docx" -Or $_.File -Like "*.doc") -And ($_.File.ParentFolder.Name -ne "Archive")}
     foreach ($file in $Files) {
        Write-Output "Deleting file $($file.Name) $($file.File.ParentFolder.Name)..."
        $file.Delete()
    }
} 

Open in new window


HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stratomanAuthor Commented:
Rainer,

i have no words to describe how happy i am now. The script worked perfectly. and Thanks to you. I was looking for this File.ParentFolder.Name property and i just couldn't. how can i get all the properties of a library in sharepoint? Great help to me and thank you again!!!!!!!!!!!
0
Rainer JeschorCommented:
Hi,
no problem.
It is not so easy to get all properties as each and every different property type can have additional/other properties.
But the MSDN object reference is really good and helpful - sometimes hard to navigate (I normally open new tabs and end up with at least 6-10 :-) )
http://msdn.microsoft.com/en-us/library/Microsoft.SharePoint.aspx

Thanks and HTH
Rainer

PS: Please do not forget to mark the question as answered.
0
stratomanAuthor Commented:
Thanks again for the prompt reply!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.