[英]Self Delete Folder with scripts powershell
我一直試圖讓Powershell或批處理腳本在完成后刪除包含所有腳本的文件夾。 最初,我嘗試了Remove-Item -Path "C:\\Tool" -Recurse -Force
,如果以腳本形式運行C:\\Tool
之外的位置,則沒有問題。 當從其中的腳本運行時,它將抱怨文件正在使用中。 經過一些研究,我發現&cmd.exe /c rd /s /q "C:\\Tool"
效果更好,但是即使我關閉了GUI,該命令也不會刪除正在使用的img文件/文件夾。
從USB驅動器啟動時,以上兩個命令均能完美運行。
以前,我在temp文件夾中創建了第二個腳本,該腳本將刪除所有文件,然后刪除其自身。 我正在尋找一種新的方法來簡化我正在研究的新設計。 我希望腳本可以從C:\\Tool
或USB驅動器中運行。
控制流程如下:
1)腳本加載所有功能
2)顯示GUI(包含img)
3)按下按鈕
4)GUI關閉
5)包含腳本的文件夾被刪除
如前所述,步驟5是我的問題。 嘗試執行的命令和命令的變體都不會刪除所有文件。
我希望第5步能夠正常工作,無論是否從GUI上的按鈕調用了命令,它都作為腳本的一部分自動運行,還是在USB等其他位置的腳本調用它來刪除文件夾C:\\Tool
我們不知道GUI的顯示方式的具體細節,但是,假設您使用的是在PowerShell代碼中構建的WinForms GUI,那么您的問題可能是GUI構造代碼如何從以后要在文件夾中的文件中加載圖像刪除。
值得注意的是,如果您使用類似:
[Bitmap]::new(<file-path>)
[System.Drawing.Image]::FromFile(<file-path>)
指定的文件顯然會在PowerShell會話的其余部分保持打開狀態 ,並且您將無法刪除文件夾。
解決方案是在內存中創建一個新的映像實例,該實例將復制從文件中加載的映像,然后處置從文件中加載的映像 ,從而釋放基礎文件上的鎖定,如本[C#]中所示回答 。
這是一個最小的腳本demo.ps1
,它演示了該方法:
將其保存到自己的臨時存放文件夾中。
將一個名為demo.png
的小圖像文件demo.png
到同一文件夾中。
然后以<temp-folder>/demo -SelfDestruct
調用它以查看其作用; 需要指定
-SelfDescript
是一種預防措施,因為意外調用會清除腳本所在的整個文件夾。
demo.ps1
: param([switch] $SelfDestruct)
# Load the WinForms assembly.
Add-Type -AssemblyName System.Windows.Forms
# Create the form.
$form = New-Object system.Windows.Forms.Form -Property @{
ClientSize = New-Object System.Drawing.Point 400,100
Text = "Dialog"
}
# Add a PictureBox control that loads its image from 'demo.png'
$form.Controls.Add((New-Object System.Windows.Forms.PictureBox -Property @{
Image = & {
# Load the image from file in the same folder as a script into
# a temporary variable.
$tmpImg = [System.Drawing.Image]::FromFile((Join-Path $PSScriptRoot 'demo.png'))
# Create an in-memory copy of the image.
New-Object System.Drawing.Bitmap $tmpImg
# Dispose of the from-file image, which releases the file.
$tmpImg.Dispose()
}
Location = New-Object System.Drawing.Point 10, 10
}))
# Show the form and wait for the use to close it.
$null = $form.ShowDialog()
if ($SelfDestruct) { # Remove the running script's entire folder.
if ("$($PWD.Path)\" -like "$PSScriptRoot\*") { #"
Push-Location C:\ # must switch to different dir. before deleting.
}
# Remove the entire folder.
Remove-Item -literalpath $PSScriptRoot -Recurse -Force
exit
}
這是一個通過事件處理程序通過點擊click觸發刪除的變體 :
param([switch] $SelfDestruct)
Add-Type -AssemblyName System.Windows.Forms
function remove-OwnFolder {
# If the current dir. is in the subtree of the folder to delete,
# we must switch to different dir. before deleting.
if ("$($PWD.Path)\" -like "$PSScriptRoot\*") { #"
Push-Location C:\
}
# Remove the script's parent folder as a whole.
Remove-Item -literalpath $PSScriptRoot -Recurse -Force
}
# Create the form.
$form = New-Object system.Windows.Forms.Form -Property @{
ClientSize = New-Object System.Drawing.Point 400,100
Text = "Dialog"
}
# Add a PictureBox control that loads its image from 'demo.png'
$form.Controls.Add((New-Object System.Windows.Forms.PictureBox -Property @{
Image = & {
# Load the image from file in the same folder as a script into
# a temporary variable.
$tmpImg = [System.Drawing.Image]::FromFile((Join-Path $PSScriptRoot 'demo.png'))
# Create an in-memory copy of the image.
New-Object System.Drawing.Bitmap $tmpImg
# Dispose of the from-file image, which releases the file.
$tmpImg.Dispose()
}
Location = New-Object System.Drawing.Point 10, 10
}))
# Add a button that will trigger the self-destruction
$btnSelfDestruct = New-Object system.Windows.Forms.Button -Property @{
Text = "Submit"
Location = New-Object System.Drawing.Point 160, 60
}
$form.Controls.Add($btnSelfDestruct)
# Add the button-click event handler.
$btnSelfDestruct.Add_Click({
$form.Close()
if ($SelfDestruct) {
remove-OwnFolder
}
exit
})
# Show the form and wait for the use to close it.
$null = $form.ShowDialog()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.