Solved

Delete a large list of files

Posted on 2012-03-27
6
625 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
Comment Utility
Please look into this codeplex solution

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

Expert Comment

by:bchallis
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 8

Expert Comment

by:bchallis
Comment Utility
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:
bchallis earned 500 total points
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
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…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now