Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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}]
- # 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]
- Param ([String]$InputFilewithpath, [String]$destination)
- $NP = $InputFilewithpath
- $DL = $destination
- $DL = "$DL\"
- $Ff = ".wtv"
- [array]$MC = 1,21,27,159,210,273,278,281 #Array used for verifing File details, Used to check File copy
- function Log-File {
- $a = Get-Date
- if ( $a.day -lt 10) {$d = "0"+$a.day} else {$d = $a.day}
- if ( $a.month -lt 10) {$m = "0"+$a.month} else {$m = $a.month}
- if ( $a.hour -lt 10) {$h = "0"+$a.hour} else {$h = $a.hour}
- if ( $a.minute -lt 10) {$mi = "0"+$a.minute} else {$mi = $a.minute}
- if ( $a.second -lt 10) {$s = "0"+$a.second} else {$s = $a.Second}
- $y = $a.Year
- $Fn = "$y$m$d$h$mi$s"
- return $Fn
- } #Prevention of overrighting other log files, 1 per file.
- function Char-Rep {
- $CR = $args[0] -replace "\:", ','
- $CR = $CR -replace '[\*\/\\]', '-'
- $CR = $CR -replace "\?", ''
- return $CR
- } #Removes Forbidden Windows Characters for names
- function Copy-File {
- param( [string]$from, [string]$to)
- $ffile = [io.file]::OpenRead($from)
- $tofile = [io.file]::OpenWrite($to)
- Write-Progress -Activity "Copying file" -status "$from -> $to" -PercentComplete 0
- try {
- [byte[]]$buff = new-object byte[] 4096
- [int]$total = [int]$count = 0
- do {
- $count = $ffile.Read($buff, 0, $buff.Length)
- $tofile.Write($buff, 0, $count)
- $total += $count
- if ($total % 1mb -eq 0) {
- Write-Progress -Activity "Copying file" -status "$from -> $to" `
- -PercentComplete ([int]($total/$ffile.Length* 100))
- }
- } while ($count -gt 0)
- }
- finally {
- $ffile.Dispose()
- $tofile.Dispose()
- Write-Progress -Activity "Copying file" -Status "Ready" -Completed
- }
- } #Copy file with Inputs, using progress bar
- function Get-Meta {
- Param([String]$NamePass, [String]$MetaNumber, [String]$IsDate)
- $FullName = $NamePass
- $Folder = Split-Path $FullName
- $File = Split-Path $FullName -Leaf
- $objShell = New-Object -ComObject Shell.Application
- $objFolder = $objShell.namespace($Folder)
- $Item = $objFolder.items().item($File)
- $MD = $objFolder.GetDetailsOf($Item, $MetaNumber)
- if ($IsDate -eq 'Y') {
- $MD = $MD.substring(0,$MD.length-11)
- } #Date Check & modification, prevention of error
- $IsDate = 0
- $MD = Char-Rep $MD
- return $MD
- } #Gets Metadata, open to all formating, Geared for WTV Files (WMC TS formating)
- function Meta-name {
- Param([int]$MN)
- $MDv = 0
- if ($MN -eq 1) {$MD = 'File Size';$mDv = 1}
- if ($MN -eq 10) {$MD = 'Owner of file';$mDv = 1}
- if ($MN -eq 16) {$MD = 'Genre of Show';$mDv = 1}
- if ($MN -eq 21) {$MD = 'Name of Show';$mDv = 1}
- if ($MN -eq 27) {$MD = 'Play length';$mDv = 1}
- if ($MN -eq 159) {$MD = 'File extension';$mDv = 1}
- if ($MN -eq 160) {$MD = 'File Name';$mDv = 1}
- if ($MN -eq 185) {$MD = 'Parent Folder name';$mDv = 1}
- if ($MN -eq 187) {$MD = 'Path to Folder';$mDv = 1}
- if ($MN -eq 189) {$MD = 'Full path to file';$mDv = 1}
- if ($MN -eq 210) {$MD = 'Episode Name';$mDv = 1}
- if ($MN -eq 273) {$MD = 'Channel Recorded on';$mDv = 1}
- if ($MN -eq 274) {$MD = 'Episode Name';$mDv = 1}
- if ($MN -eq 278) {$MD = 'Original Air Date';$mDv = 1}
- if ($MN -eq 279) {$MD = 'Show/Episode Details';$mDv = 1}
- if ($MN -eq 281) {$MD = 'Channel Callsign';$mDv = 1}
- if ($MDv -eq 0) {$MD = 'Unnamed Metadata Number';$mDv = 1}
- $MDo = "$MN - $MD"
- return $MDo
- } #Used to retrieve Metadata Name for the Numbers. Geared for WTV Files (WMC TS formating)
- $SH = Get-Meta $NP 21 N
- $EP = Get-Meta $NP 210 N
- $OA = Get-Meta $NP 278 Y
- $DF = "$DL$SH-$EP=$OA$Ff"
- if ( -not($EP ='')) {
- Copy-File $NP $DF
- $NoMeta = 0
- } #Test if File already exists that matches Meta-data format, Copies with user friendly name from meta data
- Else {
- $fold = Log-File
- $file = "$SH-$EP=$OA$Ff"
- $DL = "$DL$Fold-nometa\"
- $DF = "$DL$file"
- New-item -path "$DL" -ItemType directory
- Copy-File "$NP" "$DF"
- $NoMeta = 1
- } #Used for Files without proper Episode Name Metadata, Changes Destinations to preserve files/prevent overwriting.
- if ( Test-Path "$DF" -PathType Leaf) {
- $CFc = 0
- $date = Get-Date
- $Logname = Log-File
- Write-Output "$DF" > "$DL$Logname-$SH-$EP=$OA.txt"
- foreach ($i in $MC) {
- $dc = 'N'
- if ($i -eq 278 -and $NoMeta -eq 0) {$dc = 'Y'}
- $MS = Get-Meta $NP $i $dc
- $MD = Get-Meta $DF $i $dc
- $meta = Meta-name $i
- if ( -not($MS -eq $MD)) {Write-OutPut "[$meta] Source- $MS || $MD -Destination (Failed Check)" >> "$DL$Logname-$SH-$EP=$OA.txt";$CFc = $CFc +1}
- if ($MS -eq $MD) {Write-OutPut "[$meta] $MS (Pass Check)" >> "$DL$Logname-$SH-$EP=$OA.txt"}
- 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"}
- }#Verification Loop, Destination file deleted in failure/Mismatch
- } #Test for File tampering/failure to copy
- else
- {
- $date = Get-Date
- $desktop = [Environment]::GetFolderPath("Desktop")
- $Logname = Log-File
- Write-Output "$DF Missing after Copy from $NP, attempt made $date" > $desktop\$Logname-$SH-$EP~$OA.txt
- }#incase File or folder is not where expected, defaults to desktop for log file
- if ( Test-Path "$DF" -PathType Leaf) {
- Remove-Item $NP
- } #Deletes Source File after completion, Runs after checks for failure/non-existence
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement