4

I have a list of files including their location in a .txt like the following:

\SERVER01\backupsFolder\xx\a\ss.bak
\SERVER01\backupsFolder\xx\a\ss.bak
\SERVER02\backupsFolder\yy\b\dd.bak
\SERVER02\backupsFolder\yy\b\dd.bak

How do I delete everything else recursively starting from the 'backupsFolder' folder for example, and also output all the deleted files?

2
  • 2
    Everything else? So you want to delete everything except what is in the list? Commented Jun 24, 2011 at 14:37
  • @EBGreen That's right. Right now im creating a file list by folder. And run a script similar to the one in this question: stackoverflow.com/questions/2009955/… Commented Jun 24, 2011 at 14:40

1 Answer 1

13

I think this should help.

Script:

$TargetFolder = “Pathofyourfolder”
$Files = Get-ChildItem $TargetFolder -Exclude (gc List.txt)  -Recurse
foreach ($File in $Files)
    { write-host “Deleting File $File” -foregroundcolor “Red”; Remove-Item $File | out-null }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.