Advertisement
DePhoegon

WTV - User Friendly Rename

Nov 27th, 2017
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #For use with Batch file, Accepts one File at a time. Geared for WTV Files (WMC TS formatted files) No Quotes around Destination drive. [as example powershell.exe "C:\Folder\File.wtv" C:\Folder\SubFolder   {" Marks around the Destination will cause failures}]
  2. # It does not matter if you put a \ or not at the end of the Destination input. [powershell removes it anyways when taken as an argument]
  3. Param ([String]$InputFilewithpath, [String]$destination)
  4. $NP = $InputFilewithpath
  5. $DL = $destination
  6. $DL = "$DL\"
  7. $Ff = ".wtv"
  8. [array]$MC = 1,21,27,159,210,273,278,281 #Array used for verifing File details, Used to check File copy
  9. function Log-File {
  10. $a = Get-Date
  11. if ( $a.day -lt 10) {$d = "0"+$a.day} else {$d = $a.day}
  12. if ( $a.month -lt 10) {$m = "0"+$a.month} else {$m = $a.month}
  13. if ( $a.hour -lt 10) {$h = "0"+$a.hour} else {$h = $a.hour}
  14. if ( $a.minute -lt 10) {$mi = "0"+$a.minute} else {$mi = $a.minute}
  15. if ( $a.second -lt 10) {$s = "0"+$a.second} else {$s = $a.Second}
  16. $y = $a.Year
  17. $Fn = "$y$m$d$h$mi$s"
  18. return $Fn
  19. } #Prevention of overrighting other log files, 1 per file.
  20. function Char-Rep {
  21. $CR = $args[0] -replace "\:", ','
  22. $CR = $CR -replace '[\*\/\\]', '-'
  23. $CR = $CR -replace "\?", ''
  24. return $CR
  25. } #Removes Forbidden Windows Characters for names
  26. function Copy-File {
  27.     param( [string]$from, [string]$to)
  28.     $ffile = [io.file]::OpenRead($from)
  29.     $tofile = [io.file]::OpenWrite($to)
  30.     Write-Progress -Activity "Copying file" -status "$from -> $to" -PercentComplete 0
  31.     try {
  32.         [byte[]]$buff = new-object byte[] 4096
  33.         [int]$total = [int]$count = 0
  34.         do {
  35.             $count = $ffile.Read($buff, 0, $buff.Length)
  36.             $tofile.Write($buff, 0, $count)
  37.             $total += $count
  38.             if ($total % 1mb -eq 0) {
  39.                 Write-Progress -Activity "Copying file" -status "$from -> $to" `
  40.                    -PercentComplete ([int]($total/$ffile.Length* 100))
  41.             }
  42.         } while ($count -gt 0)
  43.     }
  44.     finally {
  45.         $ffile.Dispose()
  46.         $tofile.Dispose()
  47.         Write-Progress -Activity "Copying file" -Status "Ready" -Completed
  48.     }
  49. } #Copy file with Inputs, using progress bar
  50. function Get-Meta {
  51. Param([String]$NamePass, [String]$MetaNumber, [String]$IsDate)
  52. $FullName = $NamePass
  53. $Folder = Split-Path $FullName
  54. $File = Split-Path $FullName -Leaf
  55. $objShell = New-Object -ComObject Shell.Application
  56. $objFolder = $objShell.namespace($Folder)
  57. $Item = $objFolder.items().item($File)
  58. $MD = $objFolder.GetDetailsOf($Item, $MetaNumber)
  59. if ($IsDate -eq 'Y') {
  60. $MD = $MD.substring(0,$MD.length-11)
  61. } #Date Check & modification, prevention of error
  62. $IsDate = 0
  63. $MD = Char-Rep $MD
  64. return $MD
  65. } #Gets Metadata, open to all formating, Geared for WTV Files (WMC TS formating)
  66. function Meta-name {
  67. Param([int]$MN)
  68. $MDv = 0
  69. if ($MN -eq 1) {$MD = 'File Size';$mDv = 1}
  70. if ($MN -eq 10) {$MD = 'Owner of file';$mDv = 1}
  71. if ($MN -eq 16) {$MD = 'Genre of Show';$mDv = 1}
  72. if ($MN -eq 21) {$MD = 'Name of Show';$mDv = 1}
  73. if ($MN -eq 27) {$MD = 'Play length';$mDv = 1}
  74. if ($MN -eq 159) {$MD = 'File extension';$mDv = 1}
  75. if ($MN -eq 160) {$MD = 'File Name';$mDv = 1}
  76. if ($MN -eq 185) {$MD = 'Parent Folder name';$mDv = 1}
  77. if ($MN -eq 187) {$MD = 'Path to Folder';$mDv = 1}
  78. if ($MN -eq 189) {$MD = 'Full path to file';$mDv = 1}
  79. if ($MN -eq 210) {$MD = 'Episode Name';$mDv = 1}
  80. if ($MN -eq 273) {$MD = 'Channel Recorded on';$mDv = 1}
  81. if ($MN -eq 274) {$MD = 'Episode Name';$mDv = 1}
  82. if ($MN -eq 278) {$MD = 'Original Air Date';$mDv = 1}
  83. if ($MN -eq 279) {$MD = 'Show/Episode Details';$mDv = 1}
  84. if ($MN -eq 281) {$MD = 'Channel Callsign';$mDv = 1}
  85. if ($MDv -eq 0) {$MD = 'Unnamed Metadata Number';$mDv = 1}
  86. $MDo = "$MN - $MD"
  87. return $MDo
  88. } #Used to retrieve Metadata Name for the Numbers. Geared for WTV Files (WMC TS formating)
  89. $SH = Get-Meta $NP 21 N
  90. $EP = Get-Meta $NP 210 N
  91. $OA = Get-Meta $NP 278 Y
  92. $DF = "$DL$SH-$EP=$OA$Ff"
  93. if ( -not($EP ='')) {
  94. Copy-File $NP $DF
  95. $NoMeta = 0
  96. } #Test if File already exists that matches Meta-data format, Copies with user friendly name from meta data
  97. Else {
  98. $fold = Log-File
  99. $file = "$SH-$EP=$OA$Ff"
  100. $DL = "$DL$Fold-nometa\"
  101. $DF = "$DL$file"
  102. New-item -path "$DL" -ItemType directory
  103. Copy-File "$NP" "$DF"
  104. $NoMeta = 1
  105. } #Used for Files without proper Episode Name Metadata, Changes Destinations to preserve files/prevent overwriting.
  106. if ( Test-Path "$DF" -PathType Leaf) {
  107. $CFc = 0
  108. $date = Get-Date
  109. $Logname = Log-File
  110. Write-Output "$DF" > "$DL$Logname-$SH-$EP=$OA.txt"
  111. foreach ($i in $MC) {
  112. $dc = 'N'
  113. if ($i -eq 278 -and $NoMeta -eq 0) {$dc = 'Y'}
  114. $MS = Get-Meta $NP $i $dc
  115. $MD = Get-Meta $DF $i $dc
  116. $meta = Meta-name $i
  117. if ( -not($MS -eq $MD)) {Write-OutPut "[$meta] Source- $MS || $MD -Destination (Failed Check)" >> "$DL$Logname-$SH-$EP=$OA.txt";$CFc = $CFc +1}
  118. if ($MS -eq $MD) {Write-OutPut "[$meta] $MS (Pass Check)" >> "$DL$Logname-$SH-$EP=$OA.txt"}
  119. if ($CFc -gt 0) {Write-Output "File Copy Failure, Something did not work. $DF deleted" >> "$DL$Logname-$SH-$EP~$OA.txt"; Remove-Item $DF; Rename-item "$DL$Logname-$SH-$EP~$OA.txt" "$DL$Logname-FAIL-$SH-$EP~$OA.txt"}
  120. }#Verification Loop, Destination file deleted in failure/Mismatch
  121. } #Test for File tampering/failure to copy
  122.  else
  123.  {
  124. $date = Get-Date
  125. $desktop = [Environment]::GetFolderPath("Desktop")
  126. $Logname = Log-File
  127. Write-Output "$DF Missing after Copy from $NP, attempt made $date" > $desktop\$Logname-$SH-$EP~$OA.txt
  128. }#incase File or folder is not where expected, defaults to desktop for log file
  129. if ( Test-Path "$DF" -PathType Leaf) {
  130. Remove-Item $NP
  131. } #Deletes Source File after completion, Runs after checks for failure/non-existence
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement