2

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. The script always prompts me for removing non-empty subfolders. I tried with -force and -confirm:$false but nothing changed. 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? (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
3
  • Have you tried the -Recurse parameter? Commented May 10, 2018 at 10:42
  • And the following line is messing up with the text formatting of your question, is it correctly written? Write-Host "...created directory ""$vSaveBak[$iB]""..." Commented May 10, 2018 at 10:43
  • yeah it was for powershell but not for editor because of the escaped ". i fixed it. i tried -recurse, but it deleted all savegames in parent folder and i was not sure how i could control this. every sample i found was without the parameter Commented May 10, 2018 at 10:45

2 Answers 2

2

You can add -Recurse to remove items without prompt.

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

Get-ChildItem -path $DeleteLocation -directory | foreach ($_) { 
        Write-host Removing $_.fullname  
        Remove-Item $_.fullname  -force -recurse 
    }
Sign up to request clarification or add additional context in comments.

Comments

2

Use Remove-Item C:\tmp\dir -Recurse. You can also add -Force switch.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.