Adam McGurk
Adam McGurk

Reputation: 476

Remove-Item cmdlet not working properly

So I have a script that combs through a directory of about 1200 songs, the user selects one song, (which is then put into the $Selected variable) and then through "script magic" (I can provide the code if necessary, but I don't think it is for our purposes) an email is sent to an email account I need it sent to. I then want to delete the song from the directory aaaannnnndddd that is when I run into issues. Here is the code I originally tried to delete the song with:

Remove-Item C:\Users\woafr\Desktop\Songs\$Selected -recurse -force

And with that code I got hit with this error message:

Remove-Item : Cannot remove item C:\Users\woafr\Desktop\Songs\song.mp3: The process cannot access the file C:\Users\woafr\Desktop\Songs\song.mp3' because it is being used by another process

So I then read this artice and this Stack Overflow thread and this Server Fault thread and modified my code to this:

Get-ChildItem C:\Users\woafr\Desktop\Songs\$Selected -recurse -force | Remove-Item

And still got hit with the same errors. Is there something I can do here that will have me delete the songs, or will I have to do it by hand (the horror!)

Here is the whole script for reference:

# Search Engine part
$SearchInput = Read-Host "Enter song name here:"
$Items = Get-ChildItem C:\Users\woafr\Desktop\Songs -Recurse -Filter *$SearchInput*
IF (-Not $Items)
{Write-Host 'Nothing returned...
The search engine does not care about capitilization (so "That" and "that" are read the exact same by the search engine) 
But it does care about punctuation (so "That''s" and "Thats" are not read the same by the search engine). Try Again' -ForegroundColor Red}

# Choose you this day what song you want
IF (-Not $Items)
{cmd /c pause}
$Index = 1
$Count = $Items.Count
foreach ($Item in $Items) {
    $Item | Add-Member -MemberType NoteProperty -Name "Index" -Value $Index
    $Index++
}
$Items | Select-Object Index, Attributes, LastWriteTime, Name | Out-Host
$Input = Read-Host "Select an item by index number, then press enter (1 to $Count)"
$Selected = $Items[$Input - 1]
Write-Host "You have selected $Selected"


# Email account the script is sending from
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "[email protected]"
$Password = "mypassword"

# Email the script is sending
$to = "[email protected]"
$subject = "Songs To Ingest"
$body = "Ingest attachment into WideOrbit"
$attachment = New-Object System.Net.Mail.Attachment("C:\Users\woafr\Desktop\Songs\$Selected")

$attachment.ContentDisposition.FileName = "$Selected"

# Act of sending the email
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $body
$message.to.add($to)
$message.from = $username
$message.attachments.add($attachment)

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
write-host "Mail Sent" -ForegroundColor Green

cmd /c pause

# Trying to delete the damn thing
Get-ChildItem C:\Users\woafr\Desktop\Songs\$Selected -recurse -force | Remove-Item

cmd /c pause

Upvotes: 0

Views: 586

Answers (1)

ClumsyPuffin
ClumsyPuffin

Reputation: 4069

So the problem is send mail objects are locking the file ,try using this snippet just before your remove-item commandlet:

$smtp = $null

foreach ($attachment in $message.attachments){
$attachment.dispose();

} 
$message.dispose();

Upvotes: 1

Related Questions