编辑忘了说:宏需要大约一分钟来处理我的 55 Mb 测试文件。
我找不到有关字典内存要求的任何信息。但是,由于“它会在我进行排序之前抛出错误”,因此字典处理方式的任何微小改进都会有所帮助。我的技术完全不同。
我首先生成了一个具有以下特征的测试文件:
- 50,000 名用户
- 200 个角色
- 用户名和角色名的长度在 15 到 25 个字符之间
- 每个用户 0 到 50 个权限
生成的文件接近 55 Mb,包含超过一百万个权限。我不打算创建这么大的文件,但没有充分考虑每个用户平均 25 个权限的含义。我应该承认该文件包含重复的权限。下面的宏允许此错误并跳过重复项。
我的代码有几个步骤:
- 删除之前运行宏创建的所有文件。
- 读取测试文件(安全日志)并输出三个单独的文件:User.txt、Roles.txt 和 Perms.txt。我刚刚注意到您问题的最后一行,您说这些部分不是固定顺序的。如果您喜欢我的其余代码,那将是一个简单的修复。
- 创建批处理文件以对 User.txt、Roles.txt 和 Perms.txt 进行排序。
- 使用 Shell 运行这些批处理文件。
- 循环直到所有批处理文件都完成。
- 将 SortedUsers.txt 和 SortedRoles.txt 读入数组。正是这些数组将限制处理的安全日志的大小。我在创建单独的文件时计算了行数,因此这些数组的大小正好合适,没有开销。如果您再次耗尽内存,可以逐行读取 SortedUsers.txt。
- 根据数组读取 SortedPerms.txt 匹配并将创建的行输出到 Report.txt。
我可以用 Excel 打开 Report.txt(21Mb)并整理格式。
下面有两个模块。第一个包含上述宏。第二个包含我在流程完成时使用的例程检查。
Option Explicit
Sub CreateReport()
Dim FileName As Variant
Dim FlIn As Object
Dim FlLine As String
Dim FlLinePart() As String
Dim FlOut As Object
Dim FlSysObj As Object
Dim Found As Boolean
Dim InxProc As Long
Dim NumPermissions As Long
Dim NumRoles As Long
Dim NumUsers As Long
Dim PathCrnt As String
Dim Process() As String
Dim Roles() As String
Dim RoleCrnt As Long
Dim RoleNameLast As String
Dim TimeNow As Double
Dim Users() As String
Dim UserCrnt As Long
Dim UserNameLast As String
Dim StartTime As Double
StartTime = Timer
' I find it convenient to have all files in the same folder as the workbook
' Change PathCrnt as required
PathCrnt = ActiveWorkbook.Path & "\"
' Delete any files left by previous run of macro
' Replace Report.txt by your name for output file
' =====================================================================================
For Each FileName In Array("Users.txt", "Roles.txt", "Perms.txt", _
"SortedUsers.txt", "SortedRoles.txt", "SortedPerms.txt", _
"SortUsers.bat", "SortRoles.bat", "SortPerms.bat", _
"Report.txt")
If Dir$(PathCrnt & FileName) <> "" Then
Kill PathCrnt & FileName
End If
Next
' Split security log into three separate files: Users.txt, Roles.txt and Perms.txt
' =====================================================================================
Set FlSysObj = CreateObject("Scripting.FileSystemObject")
Set FlIn = FlSysObj.OpenTextFile(PathCrnt & "testfile.txt", 1, False, 0)
FlLine = FlIn.ReadLine
Debug.Assert FlLine = "!Users"
NumUsers = 0
Set FlOut = FlSysObj.OpenTextFile(PathCrnt & "Users.txt", 2, True, 0)
Do While Not FlIn.AtEndOfStream
FlLine = FlIn.ReadLine
If FlLine <> "" Then
If FlLine = "!Roles" Then
Exit Do
End If
NumUsers = NumUsers + 1
FlOut.WriteLine FlLine
End If
Loop
FlOut.Close
Debug.Assert FlLine = "!Roles"
NumRoles = 0
Set FlOut = FlSysObj.OpenTextFile(PathCrnt & "Roles.txt", 2, True, 0)
Do While Not FlIn.AtEndOfStream
FlLine = FlIn.ReadLine
If FlLine <> "" Then
If FlLine = "!Permissions" Then
Exit Do
End If
NumRoles = NumRoles + 1
FlOut.WriteLine FlLine
End If
Loop
FlOut.Close
Debug.Assert FlLine = "!Permissions"
NumPermissions = 0
Set FlOut = FlSysObj.OpenTextFile(PathCrnt & "Perms.txt", 2, True, 0)
Do While Not FlIn.AtEndOfStream
FlLine = FlIn.ReadLine
If FlLine <> "" Then
NumPermissions = NumPermissions + 1
FlOut.WriteLine FlLine
End If
Loop
FlOut.Close
FlIn.Close
' Create batch files to sort Users.txt, Roles.txt and Perms.txt
' I have successfully used Shell with command line parameters but not tonight
' Decided not to waste time investigating my error
' ===============================================================================================
Set FlOut = FlSysObj.OpenTextFile(PathCrnt & "SortUsers.bat", 2, True, 0)
FlOut.Write "Sort <""" & PathCrnt & "Users.txt"" >""" & PathCrnt & "SortedUsers.txt"""
FlOut.Close
Set FlOut = FlSysObj.OpenTextFile(PathCrnt & "SortRoles.bat", 2, True, 0)
FlOut.Write "Sort <""" & PathCrnt & "Roles.txt"" >""" & PathCrnt & "SortedRoles.txt"""
FlOut.Close
Set FlOut = FlSysObj.OpenTextFile(PathCrnt & "SortPerms.bat", 2, True, 0)
FlOut.Write "Sort <""" & PathCrnt & "Perms.txt"" >""" & PathCrnt & "SortedPerms.txt"""
FlOut.Close
' Sort Users.txt, Roles.txt and Perms.txt to create sorted versions
' ===============================================================================================
Call Shell(PathCrnt & "SortUsers.bat")
Call Shell(PathCrnt & "SortRoles.bat")
Call Shell(PathCrnt & "SortPerms.bat")
' Loop until all the btach files have been completed
' ===============================================================================================
Do While True
Found = False
Call GetProcessList(Process)
For InxProc = 1 To UBound(Process)
If Process(InxProc) = "cmd.exe" Then
Found = True
Exit For
End If
Next
If Not Found Then
Exit Do
End If
TimeNow = Now()
' Wait 1 second
Application.Wait TimeSerial(Hour(TimeNow), Minute(TimeNow), Second(TimeNow) + 1)
Loop
' Read SortedUsers.txt and SortedRoles.txt into arrays
' ===============================================================================================
Set FlIn = FlSysObj.OpenTextFile(PathCrnt & "SortedUsers.txt", 1, False, 0)
ReDim Users(1 To NumUsers)
For UserCrnt = 1 To NumUsers
Users(UserCrnt) = FlIn.ReadLine
Next
FlIn.Close
Set FlIn = FlSysObj.OpenTextFile(PathCrnt & "SortedRoles.txt", 1, False, 0)
ReDim Roles(1 To NumRoles)
For RoleCrnt = 1 To NumRoles
Roles(RoleCrnt) = FlIn.ReadLine
Next
FlIn.Close
' Read SortedPerms.txt and generate Report.txt
' ===============================================================================================
Set FlIn = FlSysObj.OpenTextFile(PathCrnt & "SortedPerms.txt", 1, False, 0)
' Replace Report.txt" with your name for the output file
Set FlOut = FlSysObj.OpenTextFile(PathCrnt & "Report.txt", 2, True, 0)
' Create and output header row
FlLine = """User"""
For RoleCrnt = 1 To NumRoles
FlLine = FlLine & ",""" & Roles(RoleCrnt) & """"
Next
FlOut.WriteLine FlLine
UserCrnt = 0
RoleCrnt = 0
UserNameLast = ""
RoleNameLast = ""
FlLine = ""
' Output header row within do loop
Do While Not FlIn.AtEndOfStream
FlLinePart = Split(FlIn.ReadLine, "|")
Debug.Assert UBound(FlLinePart) = 1
If FlLinePart(0) = UserNameLast And FlLinePart(1) = RoleNameLast Then
' My test file contains some duplicate permissions
Else
' Process good permission
If FlLinePart(0) <> UserNameLast Then
' New user or first permission
If FlLine <> "" Then
' Output line for last user
If RoleCrnt = NumRoles Then
' Last role already output
Else
' Add Ns for remaining roles
FlLine = FlLine & Replace(String(NumRoles - RoleCrnt, "N"), "N", ",N")
End If
FlOut.WriteLine FlLine
End If
UserCrnt = UserCrnt + 1
FlLine = Users(UserCrnt) ' Initialise line for new user
RoleCrnt = 0
End If
Do While FlLinePart(0) > Users(UserCrnt)
' This user has no permissions. Output line of Ns for it
FlLine = FlLine & Replace(String(NumRoles, "N"), "N", ",N")
FlOut.WriteLine FlLine
UserCrnt = UserCrnt + 1
FlLine = Users(UserCrnt)
Loop
If FlLinePart(0) < Users(UserCrnt) Then
Debug.Assert False
' User for this permission does not appear in user list
' Assume this should not be possible.
' Output error message if it does
Else
' Have permission for current user
' Find entry in Roles() for permiisoin's role
Do While True
RoleCrnt = RoleCrnt + 1
If FlLinePart(1) > Roles(RoleCrnt) Then
' This user does not have this current role
FlLine = FlLine & ",N"
ElseIf FlLinePart(1) < Roles(RoleCrnt) Then
Debug.Assert False
' Role for this permission does not appear in role list
' Assume this should not be possible.
' Output error message if it does
Else
' This user has this permission
FlLine = FlLine & ",Y"
Exit Do
End If
Loop
End If
End If
UserNameLast = FlLinePart(0)
RoleNameLast = FlLinePart(1)
Loop ' For each permission
' Add Ns for remaining roles
FlLine = FlLine & Replace(String(NumRoles - RoleCrnt, "N"), "N", ",N")
FlOut.WriteLine FlLine ' Output final line
FlOut.Close
Debug.Print Format(Timer - StartTime, "#,##0.0")
End Sub
.
Option Explicit
' Source http://vbadud.blogspot.co.uk/2007/06/show-all-processes-using-vba.html
' Modified by Tony Dallimore
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or _
TH32CS_SNAPPROCESS Or _
TH32CS_SNAPTHREAD Or _
TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" _
(ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
' API Functions to get the processes
Private Declare Function Process32First Lib "kernel32" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Sub GetProcessList(Process() As String)
Dim hSnapShot As Long '* Handle
Dim uProcess As PROCESSENTRY32 '* Process
Dim lRet '* Return Val
Dim InxP As Long
Dim Pos As Long
ReDim Process(1 To 100)
InxP = 0 ' Array is empty
' On Error Resume Next
' Takes a snapshot of the running processes and the heaps, modules,
' and threads used by the processes
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
uProcess.dwSize = Len(uProcess)
' Retrieve information about the first process encountered in our system snapshot
' uProcess.szExeFile is a fixed length string of 260 characters. Each new process
' name is terminated with &H0 and overwrites the previous name. Hence the need to
' discard the first &H0 and any characters that follow.
' In the original code, the first process name was ignored. During my
' experimentation, the first name was always "[System Process]" which appears to be
' a header. I continue to discard the first process name
' In the original code, the final lRet was output before being tested to be true.
' This meant the last name was junk. I always test lRet before extracting the name.
lRet = Process32First(hSnapShot, uProcess) ' Ignore "[System]"
lRet = Process32Next(hSnapShot, uProcess)
' lRet is 0 or 1. 1 means uProcess has been loaded with another process.
Do While lRet
InxP = InxP + 1
If InxP > UBound(Process) Then
ReDim Preserve Process(1 To UBound(Process) + 100)
End If
Pos = InStr(1, uProcess.szExeFile, Chr$(0))
If Pos > 0 Then
Pos = Pos - 1
Else
Pos = 0
End If
Process(InxP) = Left$(uProcess.szExeFile, Pos)
lRet = Process32Next(hSnapShot, uProcess)
Loop
CloseHandle hSnapShot
' This ReDim assumes there is at least one process.
ReDim Preserve Process(1 To InxP) ' Discard empty entries
End Sub