Regex to validate a valid Windows folder/directory path?

Marcelo 1 Reputation point
2022-05-24T17:23:12.903+00:00

I need a regex to be used in windows application for valid FOLDER paths only.
I am using Javascript for this and I've been normally a developer on a MacOS machine. So I need some help here.

I have a based regex which you can test here => https://regex101.com/r/qLdxZM/1
As you see on that link, it is accepting things like => C:\Projects\apilibrary\apilibrary.sln which is a file path and not a folder path. That should not be accepted.
So I need to modify that regex to accept only local valid Windows folder paths.

The regex to be modified is => /^[a-zA-Z]:\(((?![<>:"/\|?]).)+((?<![ .])\)?)$/gm

Help me in this, please.

Thanks in advance.

Windows development Windows API - Win32
{count} votes

2 answers

Sort by: Most helpful
  1. Junjie Zhu - MSFT 21,646 Reputation points
    2022-05-25T06:46:37.397+00:00

    Hello, Marcelo
    Welcome to Microsoft Q&A!

    Please try this expression.

    ^([a-zA-Z]:\\)([-\u4e00-\u9fa5\w\s.()~!@#$%^&()\[\]{}+=]+\\)*$  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. FloM 1 Reputation point
    2022-05-24T18:43:41.657+00:00

    Hi,

    found this regex for Windows Folder Path => /^[a-zA-Z]:\(?:\w+\?)*$/

    cf. https://regexpattern.com/windows-folder-path/

    hope this will help


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.