; Joe Winograd 10-Apr-2023 Version 1
#Warn,UseUnsetLocal ; warning on uninitialized variables
#NoEnv ; avoid checking empty variables to see if they are environment variables
#SingleInstance Force ; replace old instance immediately
SetBatchLines,-1 ; run at maximum speed
SplitPath,A_ScriptName,,,,ScriptName ; get name of script without path or extension
ReportFile:=A_AppData . "\" . ScriptName . ".txt" ; *** change this to some other full path/name if you want the report file elsewhere
FormatTime,BeginTime,,yyyy-MM-dd_HH.mm.ss
RegRead,ThouSep,HKEY_CURRENT_USER,Control Panel\International,sThousand ; get thousands separator
FileDelete,%ReportFile%
DriveGet,AllDrives,List
DriveInfo:=BeginTime . " Script started - All drive letters: " . AllDrives . " Computer: " . A_ComputerName . " User: " . A_UserName . "`n`n"
DriveArray:=StrSplit(AllDrives)
NumDrives:=DriveArray.Length()
Loop,%NumDrives%
{
NextDrive:=DriveArray[A_Index] . ":"
NextDriveInfo:=GetDriveInfo(NextDrive)
DriveInfo:=DriveInfo . NextDriveInfo . "`n"
}
FormatTime,EndTime,,yyyy-MM-dd_HH.mm.ss
DriveInfo:=DriveInfo . EndTime . " Script ended "
WriteReportFile(DriveInfo)
Try Run,%ReportFile%
ExitApp
GetDriveInfo(Letter)
{
global ThouSep
DriveGet,AllDrives,List
DriveGet,Capacity,Capacity,%Letter%
DriveSpaceFree,SpaceFree,%Letter%
DriveGet,FileSystem,FileSystem,%Letter%
DriveGet,Label,Label,%Letter%
DriveGet,Serial,Serial,%Letter%
DriveGet,Type,Type,%Letter%
DriveGet,Status,Status,%Letter%
Capacity:=Round(Capacity/1024) ; convert from MB to GB
SpaceFree:=Round(SpaceFree/1024) ; convert from MB to GB
Capacity:=RegExReplace(Capacity,"(\d)(?=(?:\d{3})+(?:\.|$))","$1"ThouSep) ; add thousands separator (such as comma) for readability
SpaceFree:=RegExReplace(SpaceFree,"(\d)(?=(?:\d{3})+(?:\.|$))","$1"ThouSep) ; add thousands separator (such as comma) for readability
DriveInfo:="Drive: " . Letter . "`n"
. "Type: " . Type . "`n"
. "Status: " . Status . "`n"
. "Capacity: " . Capacity . " GB`n"
. "Free Space: " . SpaceFree . " GB`n"
. "File System: " . FileSystem . "`n"
. "Volume Label: " . Label . "`n"
. "Serial Number: " . Serial . "`n"
Return DriveInfo
}
WriteReportFile(Record)
{
global ReportFile
If (ReportFile="")
Return
; Make 10 attempts with 100ms between them to write the file before declaring it a fatal error.
; This is to solve the (rare) problem with a cloud or network server that cannot process the writes fast enough.
; FileAppend gets ErrorLevel=1 when doing the write.
Loop,10
{
Try FileAppend,%Record%`n,%ReportFile%
Catch ; treat exception same as ErrorLevel not zero - try again
{
Sleep,100
Continue
}
If (ErrorLevel=0)
Return
Sleep,100
}
; here when all 10 attempts failed
MsgBox,262160,Fatal Error,Error trying to write %ReportFile% - this should never happen.
ExitApp
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
A big thanks to Joe!
Author
Commented: