Remove-Item cmdlet 无法正常工作
Remove-Item cmdlet not working properly
所以我有一个脚本梳理了大约1200首歌曲的目录,用户选择一首歌曲,(然后将其放入$Selected
变量)然后通过"script magic"(我如果需要,可以提供代码,但我不认为这是为了我们的目的)一封电子邮件被发送到我需要它发送到的电子邮件帐户。然后我想从目录 aaaannnnndddd 中删除这首歌,那是我 运行 进入问题时。这是我最初尝试删除歌曲的代码:
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 artice and this Stack Overflow thread and this 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 = "myemail@gmail.com"
$Password = "mypassword"
# Email the script is sending
$to = "emailtoingest@gmail.com"
$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
所以问题是发送邮件对象正在锁定文件,请尝试在删除项目命令行开关之前使用此代码段:
$smtp = $null
foreach ($attachment in $message.attachments){
$attachment.dispose();
}
$message.dispose();
所以我有一个脚本梳理了大约1200首歌曲的目录,用户选择一首歌曲,(然后将其放入$Selected
变量)然后通过"script magic"(我如果需要,可以提供代码,但我不认为这是为了我们的目的)一封电子邮件被发送到我需要它发送到的电子邮件帐户。然后我想从目录 aaaannnnndddd 中删除这首歌,那是我 运行 进入问题时。这是我最初尝试删除歌曲的代码:
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 artice and this Stack Overflow thread and this 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 = "myemail@gmail.com"
$Password = "mypassword"
# Email the script is sending
$to = "emailtoingest@gmail.com"
$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
所以问题是发送邮件对象正在锁定文件,请尝试在删除项目命令行开关之前使用此代码段:
$smtp = $null
foreach ($attachment in $message.attachments){
$attachment.dispose();
}
$message.dispose();