Script for Adding UIDs to SavedTimetable.xml

You are here: Home > Forum > General > Timetabling > Script for Adding UIDs to SavedTimetable.xml

Page 1 of 1

Script for Adding UIDs to SavedTimetable.xml Yesterday at 23:23 #160922
rfw
Avatar
190 posts
When writing my current timetable I was too lazy at the UIDs at time that I was entering data, this really came back to bug me, so I created the below/attached PowerShell script to add UIDs to the trains that are missing them in a given SavedTimetable.xml file.
I hope it can be of some use to others.

Note:
If you are editing your execution policy to run this script, please put it back to what it was before running script when finished

Quote:


<#UID Adder for SimSig WTT XML - RFW - 2025
This Script adds a UID to trains are missing a UID in the SavedTimetable.xml file
To use:
Make a backup of your WTT file - use of this script may corrupt the WTT file
Extract the SavedTimetable.xml from the WTT file
Right click file in explorer and select Run with PowerShell
Select SavedTimetable.xml file when prompted
For each train that is missing a UID, the script will show a prompt for you to enter a UID, by default it uses the ID of the that train
Once script has run, add SavedTimetable.xml to the WTT archive
Check SimSig loads the modified WTT file without issue

This script is provided 'as is' and I claim no reposibility for data loss as a result of this script
If reducing the restriction level of your execution policy to run this script, always revert your changes after running

#>

# Load required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Function to draw input box for UID
function Show-InputBox {
param (
[string]$Message,
[string]$Title,
[string]$DefaultResponse = ""
)

$form = New-Object System.Windows.Forms.Form
$form.Text = $Title
$form.Size = New-Object System.Drawing.Size(400,150)
$form.StartPosition = "CenterScreen"

$label = New-Object System.Windows.Forms.Label
$label.Text = $Message
$label.Size = New-Object System.Drawing.Size(380,20)
$label.Location = New-Object System.Drawing.Point(10,20)
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Text = $DefaultResponse
$textBox.Size = New-Object System.Drawing.Size(360,20)
$textBox.Location = New-Object System.Drawing.Point(10,50)
$form.Controls.Add($textBox)

$okButton = New-Object System.Windows.Forms.Button
$okButton.Text = "OK"
$okButton.Location = New-Object System.Drawing.Point(220,80)
$okButton.Add_Click({ $form.Tag = $textBox.Text; $form.Close() })
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Text = "Cancel"
$cancelButton.Location = New-Object System.Drawing.Point(300,80)
$cancelButton.Add_Click({ $form.Tag = $null; $form.Close() })
$form.Controls.Add($cancelButton)

$form.ShowDialog() | Out-Null
return $form.Tag
}

# Function to prompt the user to select the XML file
function Get-XMLFilePath {
$fileDialog = New-Object System.Windows.Forms.OpenFileDialog
$fileDialog.Filter = "XML Files (*.xml)|*.xml" # Restrict to XML files
$fileDialog.Title = "Select XML File"

$dialogResult = $fileDialog.ShowDialog()
if ($dialogResult -eq [System.Windows.Forms.DialogResult]::OK) {
return $fileDialog.FileName
} else {
return $null
}
}

# Prompt the user to select the XML file
$xmlPath = Get-XMLFilePath

if ($xmlPath) {
# Load the selected XML file
[xml]$xml = Get-Content -Path $xmlPath

# Loop through each Timetable
foreach ($timetable in $xml.SimSigTimetable.Timetables.Timetable) {
$idNode = $timetable.ID
$uidNode = $timetable.UID

if (-not $uidNode) {
$idValue = $idNode

# Ask user for UID
$uid = Show-InputBox -Message "Enter UID for Train ID: $idValue" -Title "Missing UID" -DefaultResponse $idValue

if ($uid) {
# Create UID element
$newUID = $xml.CreateElement("UID")
$newUID.InnerText = $uid

# Find where to insert
$childNodes = $timetable.ChildNodes
for ($i = 0; $i -lt $childNodes.Count; $i++) {
if ($childNodes[$i].Name -eq "ID"{
$idXmlNode = $childNodes[$i]
$nextNode = $idXmlNode.NextSibling
if ($nextNode) {
$timetable.InsertBefore($newUID, $nextNode)
} else {
$timetable.AppendChild($newUID)
}
break
}
}
}
}
}

# Save the updated XML
$xml.Save($xmlPath)

[System.Windows.Forms.MessageBox]::Show("All missing UIDs have been added.", "Done", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
} else {
[System.Windows.Forms.MessageBox]::Show("No file selected. Script will exit.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
}


Post has attachments. Log in to view them.
The train now standing on platform 2, should be on the rails
Last edited: Today at 00:15 by rfw
Reason: Wording of execution policy note was unclear

Log in to reply