2017-03-18 73 views
0

所以我有一個腳本,通過一個約1200首歌曲的目錄進行梳理,用戶選擇一首歌曲(然後放入$Selected變量),然後通過「script magic」(I可以在必要時提供代碼,但我認爲這不是我們的目的),我們會將一封電子郵件發送到我需要發送到的電子郵件帳戶。然後我想從目錄aaaannnnndddd中刪除歌曲,當我遇到問題時。下面是我最初試圖刪除歌曲與代碼:Remove-Item cmdlet無法正常工作

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

有了這樣的代碼,我被擊中了與此錯誤消息:

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

所以我當時讀this articethis Stack Overflow threadthis Server Fault thread並將我的代碼修改爲:

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

並且仍然遭到了同樣的錯誤。有什麼我可以在這裏做,將有我刪除的歌曲,否則我將不得不做手工

下面是引用整個腳本(恐怖!):

# 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 
+0

您能否提供魔術腳本? –

+0

@Flynn Handley哈哈,是的。剛剛編輯過 –

+0

我注意到你的-force在管道的左側,我想你可能希望它在右側,這樣它會讀取-recurse | Remove-Item -Force否則你試圖強制Get-Content,而不是Remove-Item cmdlet。 – Random206

回答

1

所以問題是發送郵件的對象正在鎖定文件,請嘗試在您的remove-item命令提示符之前使用此代碼段:

$smtp = $null 

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

} 
$message.dispose();