Untitled
unknown
powershell
6 months ago
3.8 kB
23
Indexable
# This script renames Minecraft world folders to their in-game name (LevelName).
# It uses the NBTUtil.exe utility to read the level.dat file in each world.
# ==============================================================================
# SCRIPT CONFIGURATION
# ==============================================================================
# 1. SET THE PATH TO YOUR NBTUtil.exe
# Make sure this path is correct. If the script is in the same folder as NBTUtil.exe,
# you can leave this as '.\NBTUtil.exe'
$NBTUtilPath = "C:\NBTExplorer-2.8.0\NBTUtil.exe"
# 2. SET THE PATH TO THE MAIN FOLDER CONTAINING YOUR WORLD FOLDERS
# Replace 'C:\minecraft\worlds' with the actual path to your worlds folder.
# The script will loop through the subfolders inside this folder.
$MainWorldsPath = "C:\minecraft\worlds"
# ==============================================================================
# SCRIPT LOGIC (NO NEED TO EDIT BELOW THIS LINE)
# ==============================================================================
Write-Host "Starting world folder renaming process..." -ForegroundColor Cyan
# Check if NBTUtil.exe exists at the specified path
if (-not (Test-Path $NBTUtilPath)) {
Write-Host "ERROR: NBTUtil.exe not found at '$NBTUtilPath'." -ForegroundColor Red
Write-Host "Please check the path and try again." -ForegroundColor Yellow
exit
}
# Use Get-ChildItem to get all subdirectories in the main worlds path
# The -Directory flag ensures we only get folders, not files
Get-ChildItem -Path $MainWorldsPath -Directory | ForEach-Object {
$currentFolder = $_.FullName
$levelDatPath = Join-Path -Path $currentFolder -ChildPath "level.dat"
Write-Host ""
Write-Host "Processing folder: $($_.Name)" -ForegroundColor Yellow
# Check if the level.dat file exists in the current folder
if (-not (Test-Path $levelDatPath)) {
Write-Host " - Skipping: 'level.dat' not found in this folder." -ForegroundColor Red
return # Move to the next folder in the loop
}
# Use a try/catch block to handle potential errors from NBTUtil.exe
try {
# Execute the NBTUtil command and get the output.
# The .Trim() at the end is crucial to remove leading/trailing spaces.
$worldName = (&$NBTUtilPath --path="$levelDatPath" --printtree | Select-String "LevelName").Line.Split(':')[1].Trim()
# Check if a world name was successfully extracted
if ($worldName) {
Write-Host " - Found LevelName: '$worldName'" -ForegroundColor Green
# Construct the new path for the folder
$newFolderName = $worldName
$newPath = Join-Path -Path $MainWorldsPath -ChildPath $newFolderName
# Check if a folder with the new name already exists to prevent an error
if (Test-Path $newPath) {
Write-Host " - Folder with name '$newFolderName' already exists. Skipping rename." -ForegroundColor Red
} else {
# Rename the folder using the new name
Rename-Item -Path $currentFolder -NewName $newFolderName -Force -Confirm:$false
Write-Host " - Successfully renamed folder to: '$newFolderName'" -ForegroundColor Green
}
} else {
Write-Host " - Could not extract LevelName from 'level.dat'." -ForegroundColor Red
}
} catch {
Write-Host " - An error occurred while processing this folder. Skipping." -ForegroundColor Red
Write-Host " - Error details: $($_.Exception.Message)" -ForegroundColor Red
}
}
Write-Host ""
Write-Host "Script finished." -ForegroundColor Cyan
Editor is loading...
Leave a Comment