Aarebrot.net

Frodes website about Sharepoint and other stuff...

  • Increase font size
  • Default font size
  • Decrease font size

Powershell: Deleting multiple files

E-mail Print
User Rating: / 0
PoorBest 

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:

  1. # Description
  2. #   Searches a library based on a filter and prompts to delete matching files
  3. #
  4. # Syntax
  5. #   ./delFiles [[-f] <string>] [[-l] <string>] [-norepeat] [-force]
  6. #
  7. # Parameters
  8. #   -f <string>
  9. #       Filter to use when searching for files.
  10. #
  11. #       Example:
  12. #       ./delFiles -filter file.jpg     - returns exact match
  13. #       ./delFiles -filter ?ile.jpg     - single character wildcard
  14. #       ./delFiles -filter *e.jpg       - multiple character wildcard
  15. #
  16. #   -l <string>
  17. #       Specify an alternate library to delete from
  18. #       Note: Use list name of library, not filepath (opposite of ./upFiles)
  19. #
  20. #   -norepeat
  21. #       Don't ask to filter again at end of script
  22. #
  23. #   -force
  24. #       Don't ask for delete confirmation
  25. #
  26. # Settings
  27. #   Only change the -value parameter!
  28. #
  29. set-variable -option constant -name url     -value "http://localhost:31337"     # Site collection
  30. set-variable -option constant -name library -value "Style Library"              # Library to publish
  31. # End of settings
  32.  
  33. # Handle input parameters
  34. $filter = ""
  35. $tryagain = ""
  36. for($c = 0; $c -lt $args.length; $c++)
  37. {  
  38.     if($args[$c] -eq "-f")
  39.     {
  40.         $c++
  41.         $filter$args[$c]
  42.     }
  43.     elseif($args[$c] -eq "-l")
  44.     {
  45.         $c++
  46.         $lib$args[$c]
  47.     }  
  48.     elseif($args[$c] -eq "-norepeat")
  49.     {
  50.         $norepeat = "yes"
  51.     }
  52.     elseif($args[$c] -eq "-force")
  53.     {
  54.         $forcedelete = "yes"
  55.     }
  56.     else
  57.     {
  58.         "Invalid parameter(s)"
  59.         $tryagain = "endloop"
  60.     }
  61. }
  62.  
  63. if($lib.length -lt 1)
  64. {
  65.     $lib = $library
  66. }
  67.  
  68. # Create site and list objects
  69. $site    =    new-object Microsoft.SharePoint.SPSite($url)                                                       
  70. $web     =    $site.rootweb
  71. $list    =    $web.Lists[$lib]
  72.  
  73. # Filter and delete
  74. while($tryagain -eq "")
  75. {
  76.     if($filter.length -gt 0)
  77.     {
  78.         "Filtering by: " + $filter
  79.     }
  80.     else
  81.     {
  82.         $filter = Read-Host "Filter by"
  83.     }
  84.  
  85.     [Object[]]$files = $list.get_items() | where { $_.Name -like $filter}
  86.     if($files.count -gt 0)
  87.     {
  88.         $files | ft -Autosize Url, Name, ID, Level, HasPublishedVersion
  89.         if($forcedelete -eq "yes")
  90.         {
  91.             $userinput = "y"
  92.         }
  93.         else
  94.         {
  95.             "Warning! Review the list and make sure it only contains files you want to delete!"
  96.             $userinput = Read-Host "Delete these files? [y/n]"
  97.         }
  98.         if($userinput -eq "y")
  99.         {
  100.             $list.get_items() | where { $_.Name -like $filter } | % { ; "Deleting: " + $_.File; $_.Delete(); $fc++ }
  101.             "Deleted " + $fc + " file(s)"
  102.         }
  103.         else
  104.         {
  105.             "No files deleted!"
  106.         }
  107.     }       
  108.     else
  109.     {
  110.         "No files found"
  111.     }
  112.  
  113.     ""
  114.     $filter = ""
  115.     if($norepeat -ne "yes")
  116.     {
  117.         $userinput = Read-Host "Filter again? [y/n]"
  118.         if($userinput -ne "y")
  119.         {
  120.             $tryagain = "endloop"
  121.         }
  122.     }
  123.     else
  124.     {
  125.         $tryagain = "endloop"
  126.     }
  127.  
  128. }
  129.  
  130. # Dispose of objects
  131. $web.Dispose()
  132. $site.Dispose()
  133.  
  134. # Changelog
  135. #
  136. #   v1.0 - June 18th, 2008
  137. #       First public release

If you want to download the script, use the link below.

Attachments:
 delFiles.ps1[Powershell: Delete multiple files]2 KbFeb-11-2010 7:17pm
Last Updated on Monday, 10 November 2008 09:15  

Add your comment

Your name:
Subject:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification: