How Can I Rename Save Files to Fix Gaps in Naming?

0
3
Asked By CuriousCoder92 On

I have a bunch of save files from a Ren'Py game that are supposed to be sequentially numbered. The files currently go like this: 1-1-LT1.save, 1-2-LT1.save, ... up to 2-2-LT1.save. However, I deleted a few, which has created gaps in the sequence. I'm looking to rename the files so they fill in those gaps. Specifically, I need to change the names of files like 2-4-LT1.save to 2-3-LT1.save, and so on. Can anyone help me out with a PowerShell script that can automate this renaming process? I don't have any coding experience and would really appreciate the help!

3 Answers

Answered By ScriptSavvy79 On

You can also try running this code to get your save files renamed. Make sure to back them up first!

```powershell
$FileList=(Get-Item .*save | Sort-Object -Property Name).Name
$max_minor=6
$counter_major=1
$counter_minor=1
$pattern="{0}-{1}-LT1.save"
foreach ($OldFile in $FileList) {
$NewFile = $pattern -f @( $counter_major, $counter_minor )
if ($OldFile -ne $NewFile) { Move-Item .$OldFile .$NewFile }
$counter_minor++
if ($counter_minor -gt $max_minor) { $counter_major++; $counter_minor = 1 }
}
```
This should help automate the renaming process nicely! Just navigate to your save folder and execute the script.

Answered By PowerShellPro46 On

Here's a refined version of your script:

```powershell
$SaveFolder = 'pathtosavefolder'
$counter_major = 1
$counter_minor = 1
$max_minor = 6
$pattern = '{0}-{1}-LT1.save'
$FileList = Get-ChildItem -Path $SaveFolder -File -Filter '*.save' | Sort-Object -Property @{Expression = { ($_.Name -split '-')[0] -as [int] } }, @{Expression = { ($_.Name -split '-')[1] -as [int] } }
foreach ($OldFile in $FileList) {
$NewFile = $pattern -f $counter_major, $counter_minor
if ($OldFile.Name -ne $NewFile) { Rename-Item -LiteralPath $OldFile.FullName -NewName $NewFile }
$counter_minor++
if ($counter_minor -gt $max_minor) { $counter_major++; $counter_minor = 1 }
}
```
This code includes a few improvements to handle file sorting and renaming more effectively. Good luck!

Answered By FileFixer83 On

It sounds like you need a script to handle the renaming. You could start with this snippet:

```powershell
$folder = "C:PathToYourFolder"
$files = Get-ChildItem -Path $folder -Filter "*.save" | Sort-Object Name
$index = 1
foreach ($file in $files) {
$tempName = "temp_{0:D5}_{1}" -f $index, $file.Name
Rename-Item -Path $file.FullName -NewName $tempName
$index++
}

$tempFiles = Get-ChildItem -Path $folder -Filter "temp_*.save" | Sort-Object Name
$maxSecond = 6
$currentFirst = 1
$currentSecond = 1
foreach ($file in $tempFiles) {
$newName = "$currentFirst-$currentSecond-LT1.save"
Rename-Item -Path $file.FullName -NewName $newName
$currentSecond++
if ($currentSecond -gt $maxSecond) {
$currentSecond = 1
$currentFirst++
}
}
```
This script renames your files first to temporary names to avoid any conflicts and then to the correct sequential names. Just make sure to adjust the folder path accordingly!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.