Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function createSymbolicLinks ($source, $destination, $hash, [switch]$overwrite) {
- $i = 0
- Write-Host "Creating symbolic links of $source at $destination..."
- if (!(Test-Path $destination)) {
- New-Item -ItemType Directory -Force -Path $destination > $null
- }
- $processedChild = @()
- Get-ChildItem -path $source -recurse | % {
- if (!$_.PSIsContainer){
- $matched = 0
- $destPath = $_.Fullname -replace [regex]::Escape($source), $destination
- $parentFolderPath = Split-Path $destpath -Parent
- $parentFolder = Split-Path $parentFolderPath -Leaf
- #MESS folder names match the hash XML file names
- $platform = Split-Path $parentFolder -Leaf
- #test for valid XML file
- if (Test-Path -LiteralPath $hash$platform.xml){
- #read the XML file, get the record matching the basename (filename without ext)
- $MameXML = [xml](Get-Content $hash$platform.xml -encoding UTF8)}
- $sourceRom = $_.basename
- $sourceXML = $MameXML.softwarelist.software | Where-Object {$_.name -eq "$sourceRom"}
- #american parents preferred
- if (!($sourceXML.cloneof.length -gt 0) -and ($sourceXML.description -match '\(.*?USA.*?\)')) {
- $matched = 1
- $processedChild += $sourceRom
- } elseif (!($sourceXML.cloneof.length -gt 0)) {
- #if you are a parent but have no american children, you are our next preference
- $romChildrenUSA = $MameXML.softwarelist.software | Where-Object {(($_.cloneof -eq "$sourceRom") -and !($sourceXML.description -match '\(.*?USA.*?\)'))}
- if ($romChildrenUSA.count -eq 0){
- $matched = 1
- $processedChild += $sourceRom
- }
- } 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)) {
- $romParentUSA = $MameXML.softwarelist.software | Where-Object {(($_.name -eq "$sourceXML.cloneof") -and !($sourceXML.description -match '\(.*?USA.*?\)'))}
- #american non demo/proto/debug/rip child with non US parent that has not had any other siblings processed
- if ($romParentUSA.count -eq 0) {
- $matched = 1
- $processedChild += $sourceXML.cloneof
- }
- }
- if ($matched -eq 1){
- $destDesc = $sourceXML.description
- $illegalchars = [string]::join('',([System.IO.Path]::GetInvalidFileNameChars())) -replace '\\','\\'
- $destDesc = $destDesc -replace "[$illegalchars]",''
- $sourceExtension = $_.extension
- $destPath = "$parentFolderPath\$destDesc$sourceExtension"
- }
- if (!(Test-Path -LiteralPath $parentFolderPath)) {
- New-Item -ItemType Directory -Force -Path $parentFolderPath > $null
- }
- #overwrite dest files, if requested and needed
- if((Test-Path -LiteralPath $destpath) -and $overwrite -and $matched) {
- Remove-Item $destpath > $null
- cmd /c mklink `"$destpath`" `"$($_.FullName)`" > $null
- if ($LastExitCode -eq 0){
- Write-Host -NoNewline "."
- $i += 1
- } else {
- Write-Host "Error creating link source:$(_.FullName) dest:$destpath"
- }
- } elseif (!(Test-Path -LiteralPath $destpath) -and $matched) {
- cmd /c mklink `"$destpath`" `"$($_.FullName)`" > $null
- if ($LastExitCode -eq 0){
- Write-Host -NoNewline "."
- $i += 1
- } else {
- Write-Host "Error creating link source:$(_.FullName) dest:$destpath"
- }
- }
- }
- }
- write-host "`n"
- Write-Host "Created $i symbolic links at $destination."
- }
- #NB! Destination must be a LOCAL NTFS volume. -overwrite overwrites. Trailing slashes are important. MKLINK needs admin.
- createSymbolicLinks -source "Z:\Mess\Roms\" -hash "D:\MAME\0.180\hash\" -destination "D:\Roms\Links\Mess\"
- write-host "`n"
- Write-Host "All done. Press any key to exit ..."
- $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement