ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: gtol

VBA获取文件MD5值

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-12-16 16:42 | 显示全部楼层
victor888 发表于 2024-12-15 10:37
刚刚遇到了和楼上一样的问题,目前还不会解决,等我查查国外是怎么弄的,有方案了再回贴。

Vb没有就调用js,怎么省事怎么来呗
微信图片_20241202092739.png

TA的精华主题

TA的得分主题

发表于 2024-12-16 18:23 来自手机 | 显示全部楼层
tspliu 发表于 2024-12-16 16:42
Vb没有就调用js,怎么省事怎么来呗

不想用js就 用net3.5
https://club.excelhome.net/forum.php?mod=viewthread&tid=1657233&extra=&m
obile=2&_dsign=16591c64

https://club.excelhome.net/thread-1657233-1-1.html

TA的精华主题

TA的得分主题

发表于 2024-12-17 22:00 | 显示全部楼层
shanxiren2004 发表于 2024-12-16 16:03
下载是0字节文件,怎么回事?

我试了  可以下载的啊

TA的精华主题

TA的得分主题

发表于 2024-12-18 03:11 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Sub ListFilesWithMD5()
  2.     Dim fso As Object
  3.     Dim folder As Object
  4.     Dim file As Object
  5.     Dim row As Long
  6.     Dim sFileName As String
  7.     Dim sMD5 As String
  8.    
  9.     '引用FileSystemObject
  10.     Set fso = CreateObject("Scripting.FileSystemObject")
  11.    
  12.     '选择文件夹路径 - 这里是一个示例路径,请替换为您的真实路径
  13.     Set folder = fso.GetFolder("C:\YourFolderPath")
  14.    
  15.     '设置Excel工作表和开始写入的行
  16.     row = 1
  17.    
  18.     '遍历文件夹中的文件
  19.     For Each file In folder.Files
  20.         '将文件名写入A列
  21.         Cells(row, 1).Value = file.Name
  22.         
  23.         '计算MD5值
  24.         sMD5 = GetFileMD5(file.Path)
  25.         
  26.         '将MD5值写入B列
  27.         Cells(row, 2).Value = sMD5
  28.         
  29.         row = row + 1
  30.     Next file
  31.    
  32.     '清理
  33.     Set file = Nothing
  34.     Set folder = Nothing
  35.     Set fso = Nothing
  36. End Sub

  37. Function GetFileMD5(filePath As String) As String
  38.     Dim handle As Long, hHash As Long
  39.     Dim hash(0 To 15) As Byte
  40.     Dim hashHex As String
  41.     Dim i As Integer
  42.    
  43.     '使用Windows API来生成MD5
  44.     handle = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  45.     If handle <> -1 Then
  46.         If CryptAcquireContext(hHash, vbNullString, vbNullString, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) Then
  47.             If CryptCreateHash(hHash, CALG_MD5, 0, 0, hHash) Then
  48.                 If CryptHashData(hHash, handle, 0, 0) Then
  49.                     If CryptGetHashParam(hHash, HP_HASHVAL, hash(0), 16, 0) Then
  50.                         For i = 0 To 15
  51.                             hashHex = hashHex & Right$("0" & Hex$(hash(i)), 2)
  52.                         Next i
  53.                     End If
  54.                 End If
  55.                 CryptDestroyHash hHash
  56.             End If
  57.             CryptReleaseContext hHash, 0
  58.         End If
  59.         CloseHandle handle
  60.     End If
  61.    
  62.     GetFileMD5 = LCase$(hashHex)
  63. End Function

  64. ' API声明
  65. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
  66. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  67. Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
  68. Private Declare Function CryptCreateHash Lib "advapi32.dll" (ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, ByVal dwFlags As Long, phHash As Long) As Long
  69. Private Declare Function CryptHashData Lib "advapi32.dll" (ByVal hHash As Long, ByVal pbData As Long, ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long
  70. Private Declare Function CryptGetHashParam Lib "advapi32.dll" (ByVal hHash As Long, ByVal dwParam As Long, pbData As Byte, pdwDataLen As Long, ByVal dwFlags As Long) As Long
  71. Private Declare Function CryptDestroyHash Lib "advapi32.dll" (ByVal hHash As Long) As Long
  72. Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByVal hProv As Long, ByVal dwFlags As Long) As Long

  73. '常量
  74. Const GENERIC_READ = &H80000000
  75. Const OPEN_EXISTING = 3
  76. Const FILE_SHARE_READ = &H1
  77. Const FILE_ATTRIBUTE_NORMAL = &H80
  78. Const PROV_RSA_FULL = 1
  79. Const CRYPT_VERIFYCONTEXT = &HF0000000
  80. Const CALG_MD5 = 32771
  81. Const HP_HASHVAL = 2
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-12-25 16:19 , Processed in 0.031532 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表