[PowerShell] SFV生成与校验# Author: Icebird@cnblogs
[PowerShell] SFV生成与校验#
 Purpose: Check-SFV (csfv) and Make-SFV (msfv)
[PowerShell] SFV生成与校验

[PowerShell] SFV生成与校验
function Global:Check-SFV([string]$filename = "")
[PowerShell] SFV生成与校验{
[PowerShell] SFV生成与校验
trap
[PowerShell] SFV生成与校验{
[PowerShell] SFV生成与校验  
$_.InvocationInfo
[PowerShell] SFV生成与校验  write
-host ("{0,-17:S}{1} {2}" -f "Exception"":"$_.Exception.Message)
[PowerShell] SFV生成与校验  
break
[PowerShell] SFV生成与校验}
[PowerShell] SFV生成与校验
[PowerShell] SFV生成与校验    
if (($filename.Length -eq 0-or !($filename -match ".sfv$"))
[PowerShell] SFV生成与校验    {
[PowerShell] SFV生成与校验        
return "Usage: Check-SFV filename.sfv"
[PowerShell] SFV生成与校验    }
[PowerShell] SFV生成与校验    dir 
$filename > $null
[PowerShell] SFV生成与校验    
$sfv = type $filename | ? {$a = $_.Trim(); ($a.Length -gt 0-and !($a -match "^[#;]")}
[PowerShell] SFV生成与校验    [hashtable] 
$table = @{}
[PowerShell] SFV生成与校验    [regex] 
$regex = "^(.+)`\s+([0-9a-zA-Z]+)$"
[PowerShell] SFV生成与校验    
$sfv | % {
[PowerShell] SFV生成与校验        
$m = $regex.matches($_)
[PowerShell] SFV生成与校验        
if (($m.Count -gt 0-and $m[0].Success)
[PowerShell] SFV生成与校验        {
[PowerShell] SFV生成与校验            
$table.Add($m[0].Groups[1].Value, $m[0].Groups[2].Value)
[PowerShell] SFV生成与校验        }
[PowerShell] SFV生成与校验    }
[PowerShell] SFV生成与校验    
$path = split-path($filename)
[PowerShell] SFV生成与校验    
if ($path.Length -gt 0)
[PowerShell] SFV生成与校验    {
[PowerShell] SFV生成与校验        
$path += "\"
[PowerShell] SFV生成与校验    }
[PowerShell] SFV生成与校验    
$table.Keys | sort | % {
[PowerShell] SFV生成与校验        
$file = "$path$_"
[PowerShell] SFV生成与校验        
$crc = $table[$_].ToUpper()
[PowerShell] SFV生成与校验        
$regex = "bytes[)] is 0x(.{8,8})"
[PowerShell] SFV生成与校验        
$r = CRC "$file"
[PowerShell] SFV生成与校验        
$m = $regex.matches($r)
[PowerShell] SFV生成与校验        
if (($m.Count -gt 0-and $m[0].Success)
[PowerShell] SFV生成与校验        {
[PowerShell] SFV生成与校验            
if ($m[0].Groups[1].Value -eq $crc)
[PowerShell] SFV生成与校验            {
[PowerShell] SFV生成与校验                echo 
"$file is OK"
[PowerShell] SFV生成与校验            }
[PowerShell] SFV生成与校验            
else
[PowerShell] SFV生成与校验            {
[PowerShell] SFV生成与校验                echo 
"$file is damaged"
[PowerShell] SFV生成与校验            }
[PowerShell] SFV生成与校验        }
[PowerShell] SFV生成与校验        
else
[PowerShell] SFV生成与校验        {
[PowerShell] SFV生成与校验            echo 
"$file is missing"
[PowerShell] SFV生成与校验        }
[PowerShell] SFV生成与校验    }
[PowerShell] SFV生成与校验}
[PowerShell] SFV生成与校验
[PowerShell] SFV生成与校验
function Global:Make-SFV([string]$path = "",[string]$filename = "")
[PowerShell] SFV生成与校验{
[PowerShell] SFV生成与校验
trap
[PowerShell] SFV生成与校验{
[PowerShell] SFV生成与校验  
$_.InvocationInfo
[PowerShell] SFV生成与校验  write
-host ("{0,-17:S}{1} {2}" -f "Exception"":"$_.Exception.Message)
[PowerShell] SFV生成与校验  
break
[PowerShell] SFV生成与校验}
[PowerShell] SFV生成与校验    
if ($path.Length -eq 0)
[PowerShell] SFV生成与校验    {
[PowerShell] SFV生成与校验        
return "Usage: Make-SFV path [filename.sfv]"
[PowerShell] SFV生成与校验    }
[PowerShell] SFV生成与校验    
$files = (dir $path -exclude *.sfv | ? { $_ -is [System.IO.FileInfo] } | sort { $_.Name })
[PowerShell] SFV生成与校验    
if (($files -eq $null-or ($files.Count -eq 0))
[PowerShell] SFV生成与校验    {
[PowerShell] SFV生成与校验        
return
[PowerShell] SFV生成与校验    }
[PowerShell] SFV生成与校验    
if ($filename.Length -eq 0)
[PowerShell] SFV生成与校验    {
[PowerShell] SFV生成与校验        
if ($files -is [System.IO.FileInfo])
[PowerShell] SFV生成与校验        {
[PowerShell] SFV生成与校验            [regex] 
$regex = $files.Extension + "$"
[PowerShell] SFV生成与校验            
$filename = $files.FullName -replace $regex,""
[PowerShell] SFV生成与校验        }
[PowerShell] SFV生成与校验        
else
[PowerShell] SFV生成与校验        {
[PowerShell] SFV生成与校验            
$filename = $files[0].Directory.FullName + "\" + $files[0].Directory.Name
[PowerShell] SFV生成与校验        }
[PowerShell] SFV生成与校验    }
[PowerShell] SFV生成与校验    
if (!($filename -match "[.]sfv$"))
[PowerShell] SFV生成与校验    {
[PowerShell] SFV生成与校验        
$filename += ".sfv"
[PowerShell] SFV生成与校验    }
[PowerShell] SFV生成与校验    
$temp = "${env:Temp}\makesfv.tmp"
[PowerShell] SFV生成与校验    echo 
"; 由 PowerShell.MakeSFV 生成" > $temp
[PowerShell] SFV生成与校验    [string] 
$now = get-date
[PowerShell] SFV生成与校验    echo (
"; 使用 CRC Verification Utility v3.05 于 " + $now>> $temp
[PowerShell] SFV生成与校验    echo 
";" >> $temp
[PowerShell] SFV生成与校验    
$files | % {
[PowerShell] SFV生成与校验        
$r = CRC "$_"
[PowerShell] SFV生成与校验        [regex] 
$regex = "bytes[)] is 0x(.{8,8})"
[PowerShell] SFV生成与校验        
$m = $regex.matches($r)
[PowerShell] SFV生成与校验        
if (($m.Count -gt 0-and $m[0].Success)
[PowerShell] SFV生成与校验        {
[PowerShell] SFV生成与校验            echo (
$_.Name + " " + $m[0].Groups[1].Value) >> $temp
[PowerShell] SFV生成与校验        }
[PowerShell] SFV生成与校验    }
[PowerShell] SFV生成与校验    type 
"$temp" | out-file $filename -encoding default #转为ansi格式
[PowerShell] SFV生成与校验
    type "$filename"
[PowerShell] SFV生成与校验    del 
"$temp"
[PowerShell] SFV生成与校验}
[PowerShell] SFV生成与校验
[PowerShell] SFV生成与校验
if ((dir alias: | ? { $_.Name -eq "csfv"  }) -eq $null)
[PowerShell] SFV生成与校验{
[PowerShell] SFV生成与校验new
-alias csfv Check-SFV -scope global
[PowerShell] SFV生成与校验}
[PowerShell] SFV生成与校验
if ((dir alias: | ? { $_.Name -eq "msfv"  }) -eq $null)
[PowerShell] SFV生成与校验{
[PowerShell] SFV生成与校验new
-alias msfv Make-SFV -scope global
[PowerShell] SFV生成与校验}
[PowerShell] SFV生成与校验

这段脚本将增加两个全局函数Check-SFV (csfv) 和 Make-SFV (msfv),用途是生成SFV与校验SFV

注意:
1. CRC.EXE在搜索路径里的任意目录内


Usage Example:

msfv C:\book\*.rar
msfv C:\book\*.rar D:\book.sfv
csfv test.sfv
csfv d:\cd\cd.sfv

crc.exe下载:
https://files.cnblogs.com/Icebird/crc.rar

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-30
  • 2021-12-10
  • 2021-08-25
  • 2021-06-24
  • 2022-12-23
  • 2021-11-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案