MC2010
asked on
Delete a large list of files
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.
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.
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)
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
$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)
ASKER
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.
I changed to Test-Path line to
if (Test-Path -LiteralPath $file)
and it worked when I tested it. Can you try this?
Cheers.
if (Test-Path -LiteralPath $file)
and it worked when I tested it. Can you try this?
Cheers.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent! thank you very much for your experience here. It has cured my problem and saved us alot of problems. Well done.
http://bulkfilemanager.codeplex.com/