Deleting multiple files in a library
After I solved my problem of checking in multiple files in a library, I quickly realized that I'd be confronted with a new problem pretty soon. Getting rid of said files. Either if you're moving them into a feature, renaming them and uploading new ones, or if you just want to get rid of them because they didn't look good, you have to click each file one at a time to delete them.
I don't like doing things over and over, so, yet again, Powershell came to my rescue.
After looking up how to do some basic deleting of files, I wrote up a quick and dirty script to search for a string, display all files matching that string, and deleting those files is the user was so inclined. A nice side effect of this script, since it prompts if you want to delete the files or not, you can actually list all the files in the seleted library.Here is the full code listing:
# Description # Searches a library based on a filter and prompts to delete matching files # # Syntax # ./delFiles [[-f] <string>] [[-l] <string>] [-norepeat] [-force] # # Parameters # -f <string> # Filter to use when searching for files. # # Example: # ./delFiles -filter file.jpg - returns exact match # ./delFiles -filter ?ile.jpg - single character wildcard # ./delFiles -filter *e.jpg - multiple character wildcard # # -l <string> # Specify an alternate library to delete from # Note: Use list name of library, not filepath (opposite of ./upFiles) # # -norepeat # Don't ask to filter again at end of script # # -force # Don't ask for delete confirmation # # Settings # Only change the -value parameter! # set-variable -option constant -name url -value "http://localhost:31337" # Site collection set-variable -option constant -name library -value "Style Library" # Library to publish # End of settings # Handle input parameters $filter = "" $tryagain = "" for($c = 0; $c -lt $args.length; $c++) { if($args[$c] -eq "-f") { $c++ $filter = $args[$c] } elseif($args[$c] -eq "-l") { $c++ $lib = $args[$c] } elseif($args[$c] -eq "-norepeat") { $norepeat = "yes" } elseif($args[$c] -eq "-force") { $forcedelete = "yes" } else { "Invalid parameter(s)" $tryagain = "endloop" } } if($lib.length -lt 1) { $lib = $library } # Create site and list objects $site = new-object Microsoft.SharePoint.SPSite($url) $web = $site.rootweb $list = $web.Lists[$lib] # Filter and delete while($tryagain -eq "") { if($filter.length -gt 0) { "Filtering by: " + $filter } else { $filter = Read-Host "Filter by" } [Object[]]$files = $list.get_items() | where { $_.Name -like $filter} if($files.count -gt 0) { $files | ft -Autosize Url, Name, ID, Level, HasPublishedVersion if($forcedelete -eq "yes") { $userinput = "y" } else { "Warning! Review the list and make sure it only contains files you want to delete!" $userinput = Read-Host "Delete these files? [y/n]" } if($userinput -eq "y") { $list.get_items() | where { $_.Name -like $filter } | % { ; "Deleting: " + $_.File; $_.Delete(); $fc++ } "Deleted " + $fc + " file(s)" } else { "No files deleted!" } } else { "No files found" } "" $filter = "" if($norepeat -ne "yes") { $userinput = Read-Host "Filter again? [y/n]" if($userinput -ne "y") { $tryagain = "endloop" } } else { $tryagain = "endloop" } } # Dispose of objects $web.Dispose() $site.Dispose() # Changelog # # v1.0 - June 18th, 2008 # First public release
If you want to download the script, use the link below.




