复制代码
- @echo off
- @rem MD5_16 16
- @rem MD5 32
- @rem SHA1 40
- @rem SHA256 64
- @rem SHA384 96
- @rem SHA512 128
- setlocal enabledelayedexpansion
- @rem 显示指令状态
- echo;正在执行:&&echo;"%~0" "%~1" &&echo;
- @rem 初始化扩展名支持列表
- for /f "delims==" %%a in ('set ext_[ 2^>nul') do set "%%a="
- set "ext_[.MD5]=True"
- set "ext_[.SHA]=True"
- set "ext_[.SHA1]=True"
- set "ext_[.SHA128]=True"
- set "ext_[.SHA256]=True"
- set "ext_[.SHA384]=True"
- set "ext_[.SHA512]=True"
- set "ext_[.MD5SUM]=True"
- set "ext_[.MD5SUMS]=True"
- set "ext_[.SHA1SUMS]=True"
- set "ext_[.SHA256SUMS]=True"
- set "ext_[.SHA512SUMS]=True"
- @rem 检查数据文件类型是否为本脚本所支持
- call set "ext_Code=%%ext_[%~x1]%%"
- if not defined ext_Code (
- echo;
- echo;不支持的数据文件扩展名:%~x1 (脚本只支持扩展名为以.sha或.md5开头的数据文件,如:"d:\test\test.SHA128")
- goto :ExitBatch
- )
- @rem 初始化本脚本支持的Hash类型
- for /f "delims==" %%a in ('set sha_[ 2^>nul') do set "%%a="
- set "sha_[16]=MD5"
- set "sha_[32]=MD5"
- set "sha_[40]=SHA1"
- set "sha_[64]=SHA256"
- set "sha_[96]=SHA384"
- set "sha_[128]=SHA512"
- @rem 初始化计数器
- set /a "FilesNo=0"
- set /a "File1No=0"
- set /a "FilesEr=0"
- set /a "File1Er=0"
- set /a "Hash0Er=0"
- set /a "Hash1Er=0"
- set /a "Folders=0"
- @rem 初始化strlen函数内敛到变量_strlen中,用于测试字符串长度(0~8K);入口参数#1,返回变量##
- set "_strlen=set $=^!#1^!#&set ##=&(for %%z in (4096 2048 1024 512 256 128 64 32 16)do if ^!$:~%%z^!. NEQ . set/a##+=%%z&set $=^!$:~%%z^!)&set $=^!$^!fedcba9876543210&set/a##+=0x^!$:~16,1^!"
- @rem 初始化校验有效数据的输出状态:存在参数2(%2)则不输出,反之则输出
- if "[%~2]"=="[]" (set "Ef_Disp_ID=True") else (set "Ef_Disp_ID=")
- @rem 切换for指令的默认工作文件夹
- cd /d %~dp1
- @rem 用for指令读取数据:忽略"#"开头的行,每行只切为2段,其分隔符为"* "(放置在最后可实现"空格与星号任意组合")。
- for /f "usebackq eol=# tokens=1,* delims=* " %%a in ("%~1") do (
- set /a FilesNo+=1
- set "ListNo= !FilesNo!."
- set "ListNo=!ListNo:~-5!
- set "Dir_a=%%~ab"
- if not defined Dir_a (
- echo;!ListNo!"%%~fb" 文件不存在(×)
- set /a FilesEr+=1
- ) else if "[!Dir_a:~0,1!]"=="[d]" (
- echo;!ListNo!"%%~fb" 是文件夹,忽略(×)
- set /a Folders+=1
- ) else (
- set #1=%%~a
- (%_strlen%)
- @rem 获取Hash类型
- call set "sha_Code=%%sha_[!##!]%%"
- if defined sha_Code for /f "usebackq tokens=*" %%z in (`certutil -hashfile "%%~fb" !sha_Code!`) do (
- set "Hash_Code=%%z"
- if /i "[!Hash_Code:~0,26!]"=="[CertUtil: -hashfile 失败: 0x]" (
- echo;!ListNo!"%%~fb" 在执行 `certutil -hashfile "%%~nxb" !sha_Code: =!` 时出错(×)
- set /a File1Er+=1
- )
- if "[!Hash_Code!]"=="[!Hash_Code::=!]" (
- set "Hash_Code=%%z"
- set "Hash_Code=!Hash_Code: =!"
- if "[!##!]"=="[16]" (
- set "Hash_Code=!Hash_Code:~8,16!"
- set "Check_code=MD5_16"
- ) else (
- set "Check_code=!sha_Code!"
- )
- if /i "[!Hash_Code!]"=="[%%~a]" (
- if defined Ef_Disp_ID echo;!ListNo!"%%~fb" (!Check_code!)有效,列示如下:!Hash_Code!
- set /a File1No+=1
- ) else (
- echo;!ListNo!"%%~fb" (!Check_code!)无效(×),实测如下:!Hash_Code!(%%~a)
- set /a Hash1Er+=1
- )
- )
- ) else (
- echo;!ListNo!"%%~a" (!##!位)HasH码长度不符(16、32、40、64、96、128)(×)
- set /a Hash0Er+=1
- )
- )
- )
- echo;
- echo;共扫描Hash码 %FilesNo% 个
- echo;其中有 %File1No% 个文件HasH码校验有效(√)
- if %Hash1Er% GTR 0 echo; 有 %Hash1Er% 个HasH码校验无效(×)
- if %Hash0Er% GTR 0 echo; 有 %Hash0Er% 个HasH码长度不符(×)
- if %FilesEr% GTR 0 echo; 有 %FilesEr% 个文件不存在(×)
- if %File1Er% GTR 0 echo; 有 %File1Er% 个文件HasH校验过程出错(×)
- if %Folders% GTR 0 echo; 有 %Folders% 行被忽略(×)
- :ExitBatch
- endlocal
- echo;
- echo;脚本执行结束,请按任意键退出
- pause>nul
- exit /b
欢迎光临 批处理之家 (http://www.bathome.net/) | Powered by Discuz! 7.2 |