使用Powershell在没有提示的情况下自动删除文件
我尝试编写一个脚本,用于自动备份游戏的savegame文件夹。 它通常可以工作,但我对Remove-Item cmdlet有一些问题。 脚本总是提示我删除非空子文件夹。我试着用-force和-confirm:$false,但没有任何改变。我还尝试了几种在网上可以找到的方法 我对Powershell没有经验,所以也许我只是在某个地方犯了个愚蠢的错误? 如果有什么事情需要以其他方式完成,我也希望得到一些反馈使用Powershell在没有提示的情况下自动删除文件,powershell,Powershell,我尝试编写一个脚本,用于自动备份游戏的savegame文件夹。 它通常可以工作,但我对Remove-Item cmdlet有一些问题。 脚本总是提示我删除非空子文件夹。我试着用-force和-confirm:$false,但没有任何改变。我还尝试了几种在网上可以找到的方法 我对Powershell没有经验,所以也许我只是在某个地方犯了个愚蠢的错误? 如果有什么事情需要以其他方式完成,我也希望得到一些反馈 # ProjectZomboid sandbox savegame folder: $vSa
# ProjectZomboid sandbox savegame folder:
$vSaveDir = $Env:USERPROFILE+'\Zomboid\Saves\Sandbox\'
# Where is your ProjectZomboid folder?:
$vWorkingDir = 'D:\SteamLibrary\SteamApps\common\ProjectZomboid\'
# How many backups should be keeped?:
$vAMTBackups = 5
clear
$vSaveBak = @()
# init counter for amounts of backups keeped
$iB = 0
# set working directory for script to save dir
Set-Location $vSaveDir
# call the game
$vPID = (Start-Process -FilePath $vWorkingDir'ProjectZomboid64.exe' -WorkingDirectory $vWorkingDir -PassThru).Id
# while process is NOT running, wait 60s, then continue
while (!(Get-Process -Id $vPID)){
Start-Sleep -s 60
}
# while process is running with a 30s cooldown for each iteration
while ((Get-Process -Id $vPID -ErrorAction SilentlyContinue)){
"...process is running ..."
Start-Sleep -s 30
# reset counter for amounts of backups keeped
if ($iB -eq $vAMTBackups) {
$iB = 0
}
# check for existing saves
if (Test-Path $vSaveDir\*) {
# get last modified dir from save dir
$vLastSave = Get-ChildItem $vSaveDir | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object - First 1
# check if last modified dir is backup dir
if (!($vLastSave -match "Backup")) {
"...last save is not a backup savegame..."
if (!(Test-Path -Path $vSaveDir$vLastSave"_Backup_"$iB)) {
Write-Host "...directory not exists..."
$vSaveBakTmp = New-Item -Path $vSaveDir$vLastSave"_Backup_"$iB -ItemType Directory -ErrorAction SilentlyContinue # | Out-Null
$vSaveBak = $vSaveBak + $vSaveBakTmp
Write-Host "...created directory "$vSaveBak[$iB]" ..."
}
# delete any data in actual backup dir
$TempFiles = Get-ChildItem $vSaveBak[$iB] -Recurse
$TempFiles | Foreach ($_) { Remove-Item $_.Fullname -Force -ErrorAction SilentlyContinue -Confirm:$false}
# do backup
Robocopy $vLastSave $vSaveBak[$iB] /e /dcopy:T /NFL /NDL /NJH /NJS /nc /ns /np
# increase counter after backup
$iB++
}
else {
"...last save is backup savegame..."
}
}
else {
"...no savegame found..."
}
}
"...process is terminated."
我也试过了
Get-ChildItem $vSaveBak[$iB] -Recurse | Remove-Item -ErrorAction SilentlyContinue -Confirm:$false
使用删除项C:\tmp\dir-Recurse。您还可以添加-Force开关。您可以添加-Recurse来删除项目,而无需提示
Get-ChildItem -path $DeleteLocation -directory | Remove-Item -recurse
Get-ChildItem -path $DeleteLocation -directory | foreach ($_) {
Write-host Removing $_.fullname
Remove-Item $_.fullname -force -recurse
}
你试过-Recurse参数了吗?下面的一行搞乱了你问题的文本格式,写得正确吗?写主机…创建了目录$vSaveBak[$iB]…是的,它是用于powershell的,但不用于编辑器,因为已转义。我修好了。我尝试了-recurse,但它删除了父文件夹中的所有savegames,我不知道如何控制它。我找到的每个样本都没有参数