简体   繁体   English

使用Powershell在没有提示的情况下自动删除文件

[英]Automatically deleting files without prompts with Powershell

I try to write a script for an automated backup of savegame folders of a game.我尝试编写一个脚本来自动备份游戏的游戏存档文件夹。 It works in general, but i have some issues with the Remove-Item cmdlet.它一般有效,但我对 Remove-Item cmdlet 有一些问题。 The script always prompts me for removing non-empty subfolders.该脚本总是提示我删除非空子文件夹。 I tried with -force and -confirm:$false but nothing changed.我尝试使用 -force 和 -confirm:$false 但没有任何改变。 I also tried several methods i could find across the web.我还尝试了几种可以在网络上找到的方法。

I'm not experienced with Powershell, so maybe i just made stupid mistake somewhere?我对 Powershell 没有经验,所以也许我只是在某个地方犯了愚蠢的错误? (I also would appreciate some feedback in general, if there is something that should done an other way) (如果有一些事情应该以其他方式完成,我也希望得到一些反馈)

# 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."

I also tried我也试过

Get-ChildItem $vSaveBak[$iB] -Recurse | Remove-Item -ErrorAction SilentlyContinue -Confirm:$false

Use Remove-Item C:\\tmp\\dir -Recurse .使用Remove-Item C:\\tmp\\dir -Recurse You can also add -Force switch.您还可以添加-Force开关。

You can add -Recurse to remove items without prompt.您可以添加 -Recurse 以在没有提示的情况下删除项目。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM