如何製作批次腳本來備份具有唯一目標資料夾(Windows)的特定檔案

如何製作批次腳本來備份具有唯一目標資料夾(Windows)的特定檔案

我正在嘗試設定我喜歡的一個舊遊戲(輻射新維加斯 1.4.0.525),但它因保存檔案損壞而臭名昭著。我想問的是是否可以製作一個批次腳本來複製保存檔案並將複製的保存檔案放入該特定一組備份保存的資料夾中,並根據時間為該資料夾分配唯一的名稱腳本運行。我的作業系統是 Windows 10。

基本上我尋求幫助的過程是

Script started -> New Folder with a timestamp for name created -> save copied files to the new folder. 

我想要的時間碼的粗略想法是 HHDDMMYYYY (例如 1203042019)

我希望腳本複製的保存位於 C:\Users-USER-\Documents\My Games\FalloutNV\Saves 中,包含備份保存的資料夾位於 F:\Backups 中。需要複製的保存檔案有 .fos 和 .nvse 檔案類型(.fos 是遊戲的保存檔案類型,.nvse 是 mod 除了用於 mod 配置的常規保存檔案之外創建的保存檔案類型)

如果可能的話,我想知道每當我打開特定程式(Vortex,它是一個模組管理器和啟動器)時是否可以運行該腳本。

首先十分感謝。

為清楚起見進行了編輯

答案1

雖然是用 PowerShell 編寫的,而不是批次文件,但它可以在 Windows 10 上本機運行,無需任何插件或額外元件。

我相信類似下面的內容就是您所追求的。在我的範例中 - 我嘗試添加大量註釋來幫助您入門。它是用 Powershell 編寫的 - 因此您需要將腳本儲存為“myScript.ps1”,然後在桌面上建立“PowerShell.exe -File C:\users\me\whereeverthescriptis\myScript.ps1”的捷徑

這將嘗試備份,如果成功則啟動應用程式:

#What do you want to backup?
$sourceFolder = "C:\users\bbell\Desktop\p"
#Where do you want to backup to?
$destinationFolder = "C:\Installs"

#Figure out a destination folder with a timestamp.  Store it in a variable so that the same folder name is used throughout the script if it takes over a second to run
$destinationFolderWithTimestamp = ($destinationFolder + "\" + (Get-Date -Format yyyy-mm-dd_HH-mmmm-ss))

#if <destination folder>\<current date and time> doesn't exist - create it!
If (!(Test-Path -Path $destinationFolderWithTimestamp)) {
    New-Item -ItemType Directory -Path $destinationFolderWithTimestamp
    Write-Host ($destinationFolderWithTimestamp + " Created")
}

try {
    #try a backup - stop if it fails
    Copy-Item -Recurse -Path $sourceFolder -Destination $destinationFolderWithTimestamp -ErrorAction Stop -force
    #confirm backup worked
    Write-Host "Backup Completed OK"
    #launch an app - in this case notepad - the "&" needs to be kept as it denotes launching something
    & C:\Windows\notepad.exe
} catch {
    #Error happened - inform user and do not launch
    Write-Host "Backup Failed - not launching app"
}

相關內容