使用Powershell自动删除没有提示的文件

时间:2018-05-10 10:36:23

标签: powershell

我尝试编写一个脚本,用于自动备份游戏的savegame文件夹。 它一般工作,但我有一些Remove-Item cmdlet的问题。 该脚本始终提示我删除非空子文件夹。我尝试使用-force和-confirm:$ false但没有改变。我还尝试了几种可以在网上找到的方法。

我没有使用Powershell,所以也许我只是在某个地方犯了愚蠢的错误? (如果有其他方式可以采取其他方式,我也会很感激一些反馈意见)

# 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

2 个答案:

答案 0 :(得分:1)

使用Remove-Item C:\tmp\dir -Recurse。您还可以添加-Recurse开关。

答案 1 :(得分:1)

您可以添加-Recurse以在没有提示的情况下删除项目。

Get-ChildItem -path $DeleteLocation -directory | Remove-Item -recurse

Get-ChildItem -path $DeleteLocation -directory | foreach ($_) { 
        Write-host Removing $_.fullname  
        Remove-Item $_.fullname  -force -recurse 
    }