|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
最近试着写了一段代码,实现了VBA调用Powershell提取指定文件的哈希码(SHA1、MD5算法可选),经测试可用。
Powershell Windows7自带,XP下没有,而且Powershell要4.0以上版本支持 Get-FileHash 命令。
代码简单写了一下,纯粹是测试用的,如果改一下,应该会更好用,这里做个抛砖引玉,大家来共同交流。
代码如下:
- Private Sub CommandButton1_Click()
- Dim wsh As Object, oExec As Object
- Dim fFile As String, strAll As String
- Dim myFile As String, i As Long
-
- On Error GoTo errHandle
- Set wsh = CreateObject("WScript.Shell")
-
- myFile = Dir([A1] & "\*.*")
-
- i = 1
- Do While myFile <> ""
- i = i + 1
- strAll = ""
- ' fFile = [A1] & " \ " & myFile
- fFile = """" & [A1] & "" & myFile & """"
- Set oExec = wsh.Exec("POWERSHELL Get-FileHash -Path " & fFile & " -Algorithm " & [B1]) '调用POWERSHELL(4.0版本以上版本支持 Get-FileHash)脚本,Windows 7 支持
-
- strAll = oExec.StdOut.ReadAll '提取脚本输出的信息
- strAll = Mid(strAll, InStr(1, strAll, [B1]))
- Cells(i, 1).Value = Trim(Left(strAll, InStr(1, strAll, Chr(13)) - 1))
-
- myFile = Dir()
- Loop
- If i = 1 Then MsgBox "未找到 A1单元格指定文件夹 的文件!"
-
- Set oExec = Nothing
- Set wsh = Nothing
- Exit Sub
- errHandle:
- MsgBox "错误!"
- End Sub
复制代码
VBA调用powershell提取指定文件的哈希码.rar
(19.05 KB, 下载次数: 62)
|
|