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