Solved

Delete a large list of files

Posted on 2012-03-27
6
634 Views
Last Modified: 2012-04-16
Hi All,

I'm trying to delete bad file archive stubs so they can be restored from backup. I have a 80 meg list of 800,000 files with full local path I need deleted. It works, just need it to be smoother, logging, no errors etc.

Ideally I want to get-content from a txt file then remove-item with -LiteralPath switch as . Obviously avoiding errors escape characters and spaces in directory names as well, and any logging of errors would alos be great though I know this may not be possible.

Originally we have a powershell simple script that looked like this.

get-content "c:\deletelist.txt" | Remove-Item

This works well with a few files that don't have [1] in file name or spaces or escape keys, but using this command with the -LiteralPath option to sidestep this seems impossible to my feeble mind.



Can anyone suggest a way they would do this? Much appreciation in advance.
0
Comment
Question by:MC2010
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:Yagya Shree
ID: 37774620
Please look into this codeplex solution

http://bulkfilemanager.codeplex.com/
0
 
LVL 8

Expert Comment

by:Brent Challis
ID: 37774953
A PowerShell soultion, with simple logging follows.  Here is a link to a more complex logging process
http://powershell.com/cs/media/p/10676.aspx


function Log-Message
{
    Param
    (
        $LogFileName,
        $Message
    )
    ("{0} {1}" -f (Get-Date), $Message) | Out-File $LogFileName -append
}

$logFile = "c:\DeleteFiles\log.txt"
$files = get-content "C:\DeleteFiles\deletelist.txt"
$count = 0
Log-Message $logFile "Starting deletions"
foreach ($file in $files)
{
    if (Test-Path $file)
    {
        try
        {
            Remove-Item -LiteralPath $file
            Log-Message log.txt "Deleted $file"
            $count++
        }
        catch
        {
            Log-Message $logFile ("There was a problem deleting {0}: {1}" -f $file, $_.Message )
        }
    }
    else
    {
        Log-Message $logFile "File $file not found"
    }
}

Log-Message $logFile ("Completed deleting {0} files" -f $count)
0
 
LVL 1

Author Comment

by:MC2010
ID: 37779659
bchallis : While this is a brilliant script, it still does error out on filenames containing [1] eg doc1[1].doc and reports "cannot find file" this should be fixed by using  -LiteralPath, but as my simple script. it doesn't seem to work.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 8

Expert Comment

by:Brent Challis
ID: 37779759
I changed to Test-Path line to
    if (Test-Path -LiteralPath $file)

and it worked when I tested it.  Can you try this?

Cheers.
0
 
LVL 8

Accepted Solution

by:
Brent Challis earned 500 total points
ID: 37779858
I just noticed that I missed a modification to my code to use a variable for the file name of the logfile so I am included the updated version here -  with the test for the files existence included.  Also if you want more sophisticated logging than the simple Log-Message function i included, here is a link to some loggin functions that I have written:
http://www.ddls.net.au/blog/2012/03/logging-in-powershell/

function Log-Message
{
    Param
    (
        $LogFileName,
        $Message
    )
    ("[{0}] {1}" -f (Get-Date), $Message) | Out-File $LogFileName -append
}

$logFile = "c:\DeleteFiles\log.txt"
$files = get-content "C:\DeleteFiles\deletelist.txt"
$count = 0
Log-Message $logFile "Starting deletions"
foreach ($file in $files)
{
    if (Test-Path -LiteralPath $file)
    {
        try
        {
            Remove-Item -LiteralPath $file
            Log-Message $logFile "Deleted $file"
            $count++
        }
        catch
        {
            Log-Message $logFile ("There was a problem deleting {0}: {1}" -f $file, $_.Message )
        }
    }
    else
    {
        Log-Message $logFile "File $file not found"
    }
}

Log-Message $logFile ("Completed deleting {0} files" -f $count)
0
 
LVL 1

Author Closing Comment

by:MC2010
ID: 37854127
Excellent! thank you very much for your experience here. It has cured my problem and saved us alot of problems. Well done.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question