如何制作批处理脚本来备份具有唯一目标文件夹的特定文件(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,一个 mod 管理器和启动器)时是否可以运行该脚本。

首先十分感谢。

已编辑以清晰

答案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"
}

相关内容