Create Report with Information on All Drives with Letters - AutoHotkey Script

Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
EE FELLOW 2017 — first ever recipient of Fellow award
MVE 2015,2016,2018
CERTIFIED GOLD EXPERT
DISTINGUISHED EXPERT
Published:
This article presents an AutoHotkey (V1) script that creates a plain text report with information about all drives in a system that have a drive letter, such as C, D, E, etc. The information in the report is Type of drive, Status, Capacity, Free Space, File System, Volume Label, and Serial Number.
One of the terrific aspects of Question & Answer threads here at Experts Exchange is that they can segue into different issues. This happened at an EE Q&A thread that began with the requirement for a script to detect the insertion and removal of USB drives, then segued into the need for a script that provided information on drives. I wrote an EE article — Detect USB Drive Insertion and Deletion - AutoHotkey Script — as a solution to the first question. This article presents a solution to the follow-up question by providing the full source code for a script written in Version 1 of the AutoHotkey language. If you are not familiar with AutoHotkey, my Experts Exchange AutoHotkey - Getting Started article will get you going on this excellent Windows scripting/programming language.

The script presented in this article has the following features:

• You may specify the location of the report by changing the value in a variable (ReportFile). The posted code puts it in the user's AppData folder.

• When the user runs the script, it makes an entry in the report like this:

2023-04-09_23.36.17 Script started - All drive letters: CDERZ  Computer: PC11  User: Ed

• When the script is done, it makes an entry in the report like this:

2023-04-09_23.36.19 Script ended

In between those two are entries for every drive letter in the system. Here are examples from an actual run of the script (except that I changed the serial numbers for privacy/security reasons):

Drive: C:
Type: Fixed
Status: Ready
Capacity: 381 GB
Free Space: 264 GB
File System: NTFS
Volume Label: C-860EVO-1TB
Serial Number: 1234567890

Drive: D:
Type: Fixed
Status: Ready
Capacity: 4,658 GB
Free Space: 2,862 GB
File System: NTFS
Volume Label: ST-Black-5TB
Serial Number: 2345678901

Drive: E:
Type: Removable
Status: Ready
Capacity: 233 GB
Free Space: 82 GB
File System: exFAT
Volume Label: Lexar256
Serial Number: 3456789012

Drive: R:
Type: CDROM
Status: NotReady
Capacity: 0 GB
Free Space: 0 GB
File System:
Volume Label:
Serial Number:

Drive: Z:
Type: Network
Status: Ready
Capacity: 3,663 GB
Free Space: 2,155 GB
File System: NTFS
Volume Label: NAS
Serial Number: 4567890123

Here is the script in a code block (it is also attached for easy downloading):

; 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
}

I tested the script on Windows 7, 8.1, 10, and 11. It worked perfectly on all systems and was very fast — took zero to two seconds. If anyone tests it on a system earlier that W7, please post your results.

If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe

DriveReport.ahk
1
877 Views
Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
EE FELLOW 2017 — first ever recipient of Fellow award
MVE 2015,2016,2018
CERTIFIED GOLD EXPERT
DISTINGUISHED EXPERT

Comments (2)

Luis DiazIT consultant

Commented:
Extremely useful AutoHotkey script! My backup strategy is to backup through a windows batch script in 5 external hard drives once a month. By using this AutoHotkey script I can properly monitor this strategy as the listing of Hard drive information ensure me a deep organization of each one.
A big thanks to Joe!
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
You.re very welcome, Luis, and thanks back to you for endorsing the article...much appreciated!

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.