Advertisement
Guest User

Mame short names to Long Symlinks

a guest
Dec 12th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createSymbolicLinks ($source, $destination, $hash, [switch]$overwrite) {
  2.     $i = 0
  3.     Write-Host "Creating symbolic links of $source at $destination..."
  4.     if (!(Test-Path $destination)) {
  5.         New-Item -ItemType Directory -Force -Path $destination > $null
  6.     }
  7.     $processedChild = @()
  8.     Get-ChildItem -path $source -recurse | % {
  9.         if (!$_.PSIsContainer){
  10.             $matched = 0
  11.             $destPath = $_.Fullname -replace [regex]::Escape($source), $destination
  12.             $parentFolderPath = Split-Path $destpath -Parent
  13.             $parentFolder = Split-Path $parentFolderPath -Leaf
  14.             #MESS folder names match the hash XML file names
  15.             $platform = Split-Path $parentFolder -Leaf
  16.             #test for valid XML file
  17.             if (Test-Path -LiteralPath $hash$platform.xml){
  18.             #read the XML file, get the record matching the basename (filename without ext)
  19.             $MameXML = [xml](Get-Content $hash$platform.xml -encoding UTF8)}
  20.             $sourceRom = $_.basename
  21.             $sourceXML = $MameXML.softwarelist.software | Where-Object {$_.name -eq "$sourceRom"}
  22.             #american parents preferred
  23.             if (!($sourceXML.cloneof.length -gt 0) -and ($sourceXML.description -match '\(.*?USA.*?\)')) {
  24.                 $matched = 1
  25.                 $processedChild += $sourceRom
  26.             } elseif (!($sourceXML.cloneof.length -gt 0)) {
  27.                 #if you are a parent but have no american children, you are our next preference
  28.                 $romChildrenUSA = $MameXML.softwarelist.software | Where-Object {(($_.cloneof -eq "$sourceRom") -and !($sourceXML.description -match '\(.*?USA.*?\)'))}
  29.                 if ($romChildrenUSA.count -eq 0){
  30.                     $matched = 1
  31.                     $processedChild += $sourceRom
  32.                 }              
  33.             } elseif (($sourceXML.cloneof.length -gt 0) -and ($sourceXML.description -match '\(.*?USA.*?\)') -and !($sourceXML.description -match '\(.*?[dD]emo.*?\)|\(.*?[pP]rototype.*?\)|\(.*?[dD]ebug.*?\)|\(.*?[rR]ipped.*?\)') -and ($processedChild -notcontains $sourceXML.cloneof)) {
  34.             $romParentUSA = $MameXML.softwarelist.software | Where-Object {(($_.name -eq "$sourceXML.cloneof") -and !($sourceXML.description -match '\(.*?USA.*?\)'))}
  35.             #american non demo/proto/debug/rip child with non US parent that has not had any other siblings processed
  36.                 if ($romParentUSA.count -eq 0) {
  37.                     $matched = 1
  38.                     $processedChild += $sourceXML.cloneof
  39.                 }
  40.             }
  41.             if ($matched -eq 1){
  42.                 $destDesc = $sourceXML.description
  43.                 $illegalchars = [string]::join('',([System.IO.Path]::GetInvalidFileNameChars())) -replace '\\','\\'
  44.                 $destDesc = $destDesc -replace "[$illegalchars]",''
  45.                 $sourceExtension = $_.extension
  46.                 $destPath = "$parentFolderPath\$destDesc$sourceExtension"
  47.             }
  48.             if (!(Test-Path -LiteralPath $parentFolderPath)) {
  49.                 New-Item -ItemType Directory -Force -Path $parentFolderPath > $null
  50.             }
  51.             #overwrite dest files, if requested and needed
  52.             if((Test-Path -LiteralPath $destpath) -and $overwrite -and $matched) {
  53.                 Remove-Item $destpath > $null
  54.                 cmd /c mklink `"$destpath`" `"$($_.FullName)`" > $null
  55.                 if ($LastExitCode -eq 0){
  56.                     Write-Host -NoNewline "."
  57.                     $i += 1
  58.                 } else {
  59.                     Write-Host "Error creating link source:$(_.FullName) dest:$destpath"
  60.                 }
  61.             } elseif (!(Test-Path -LiteralPath $destpath) -and $matched) {
  62.                cmd /c mklink `"$destpath`" `"$($_.FullName)`" > $null
  63.                 if ($LastExitCode -eq 0){
  64.                     Write-Host -NoNewline "."
  65.                     $i += 1
  66.                 } else {
  67.                     Write-Host "Error creating link source:$(_.FullName) dest:$destpath"
  68.                 }
  69.            }
  70.         }
  71.     }
  72.     write-host "`n"
  73.    Write-Host "Created $i symbolic links at $destination."
  74. }
  75. #NB! Destination must be a LOCAL NTFS volume. -overwrite overwrites. Trailing slashes are important. MKLINK needs admin.
  76. createSymbolicLinks -source "Z:\Mess\Roms\" -hash "D:\MAME\0.180\hash\" -destination "D:\Roms\Links\Mess\"
  77. write-host "`n"
  78. Write-Host "All done. Press any key to exit ..."
  79. $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement