Jump to content

Recommended Posts

Posted

I need help with creating a function that I can pass a path to and have it confirm whether it's the root of the drive or not.

Example:

$sDriveC = "C:"
$sDriveD = "D:"
$sDriveE = "E:"
$sDriveR = "R:"
$sNotRoot = "C:\Example"

If IsRootDrive($sDriveC) Then ConsoleWrite("This is root drive." & @CRLF)
If Not IsRootDrive($sNotRoot) Then ConsoleWrite("This is not a root drive." & @CRLF)

Func IsRootDrive($sDrive)
    ;
    ; something like:
    ; "*:"
    ; needs to confirm 2 characters
    ; first character being a single drive letter
    ; second character being a colon
    ; there is no backslash in this situation
    ;
    ; if root drive, Return True
    ; if not root, Return False
EndFunc

I hope that I explained it well enough within the code box. I have literally zero understanding of regex.

Thank you for your time. :)

  • Solution
Posted

I just noticed the _WinAPI_PathIsRoot() function which I may be able to work with.

My scenario does not provide the backslash which is required to return True with the _WinAPI_PathIsRoot() function. However, I can certainly append the backslash for this purpose or even modify the _WinAPI_PathIsRoot() slightly.

Posted

PathIsRoot is probably the better solution, but just for the exercise.

  • \A and \z matches the start and end of the string.
  • [A-Za-z]: matches any letter and the colon
  • \\? matches a backslash, ? means 0 or 1 times.
$sDriveC = "C:"
$sDriveD = "D:"
$sDriveE = "E:"
$sDriveR = "R:"
$sNotRoot = "C:\Example"

If IsRootDrive($sDriveC) Then ConsoleWrite("This is root drive." & @CRLF)
If Not IsRootDrive($sNotRoot) Then ConsoleWrite("This is not a root drive." & @CRLF)

Func IsRootDrive($sDrive)
    ;
    ; something like:
    ; "*:"
    ; needs to confirm 2 characters
    ; first character being a single drive letter
    ; second character being a colon
    ; there is no backslash in this situation
    ;
    ; if root drive, Return True
    ; if not root, Return False


;~  Return StringRegExp($sDrive, "\A[A-Za-z]:\\?\z") ;Optionally has backslash
    Return StringRegExp($sDrive, "\A[A-Za-z]:\z")
EndFunc

If you're interested and have the time, I believe there's a tutorial in the autoit help file on regular expressions. They can be a bit of a headache at times, but it's a powerful thing to have the kit bag :)

Posted

Warning that subst-ed drives may point to non-root, soft and hard links as well.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted
On 1/1/2026 at 8:04 PM, MattyD said:

If you're interested and have the time, I believe there's a tutorial in the autoit help file on regular expressions. They can be a bit of a headache at times, but it's a powerful thing to have the kit bag :)

Thank you for the script. It always blows my mind how small the regex is. Small yet powerful.

I think that you are right. I should learn at least some of the basics of regex. I don't think that I would quite need anything more advanced with it but the basics would be good to learn. I'll do some reading today.

49 minutes ago, jchd said:

Warning that subst-ed drives may point to non-root, soft and hard links as well.

Good point, thank you. I haven't quite gotten that far yet with the script but that is absolutely a solid point. 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...