Back to Home

File Path Validation Regex (Windows)

CronOS Team
regexfile-pathvalidationtutorialwindows

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern

File Path Validation Regex (Windows)

Validate Windows file paths with drive letter, backslashes, and proper filename characters using regex pattern.

Pattern Breakdown

regex
^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$

Components

ComponentDescriptionMatches
^Start anchorEnsures match from string start
[a-zA-Z]Drive letterSingle letter (A-Z, case-insensitive)
:Drive separatorLiteral colon
\\Root backslashLiteral backslash (escaped)
`(?:[^\/:*?"<>\r\n]+\)*`Directory segments
`[^\/:*?"<>\r\n]*`Filename
$End anchorEnsures match to string end

Character Classes

  • [a-zA-Z] - Drive letter: A-Z (case-insensitive)
  • [^\\/:*?"&lt;&gt;|\r\n] - Valid path characters (negated class):
    • \ - Not backslash
    • / - Not forward slash
    • : - Not colon (except drive letter)
    • * - Not asterisk
    • ? - Not question mark
    • " - Not double quote
    • < - Not less than
    • > - Not greater than
    • | - Not pipe
    • \r\n - Not carriage return or newline

Examples

Valid:

  • C:\Users\Documents\file.txt
  • D:\Program Files\App\config.json
  • C:\Windows\System32\
  • E:\data\folder\
  • A:\test.txt

Invalid:

  • C:Users\Documents\file.txt (missing root backslash)
  • C:\Users\Documents\file*.txt (asterisk not allowed)
  • C:\Users\Documents\file?.txt (question mark not allowed)
  • C:\Users\Documents\file<name>.txt (angle brackets not allowed)
  • C:\Users\Documents\file|name.txt (pipe not allowed)
  • /Users/Documents/file.txt (Unix-style path)
  • C:/Users/Documents/file.txt (forward slashes)

Implementation

JavaScript

javascript
const windowsPathRegex = /^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$/;
windowsPathRegex.test('C:\\Users\\Documents\\file.txt'); // true
windowsPathRegex.test('D:\\Program Files\\App\\config.json'); // true
windowsPathRegex.test('C:\\Users\\Documents\\file*.txt'); // false (invalid char)
windowsPathRegex.test('C:Users\\Documents\\file.txt'); // false (missing backslash)

Python

python
import re
windows_path_regex = r'^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$'
bool(re.match(windows_path_regex, r'C:\Users\Documents\file.txt'))  # True
bool(re.match(windows_path_regex, r'D:\Program Files\App\config.json'))  # True
bool(re.match(windows_path_regex, r'C:\Users\Documents\file*.txt'))  # False (invalid char)

Go

go
windowsPathRegex := regexp.MustCompile(`^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$`)
windowsPathRegex.MatchString(`C:\Users\Documents\file.txt`) // true
windowsPathRegex.MatchString(`D:\Program Files\App\config.json`) // true
windowsPathRegex.MatchString(`C:\Users\Documents\file*.txt`) // false (invalid char)

Limitations

  1. No UNC paths: Doesn't support \\server\share\path format
  2. No relative paths: Only absolute paths with drive letter
  3. No long path support: Doesn't handle \\?\ long path prefix
  4. No reserved names: Doesn't check for reserved names like CON, PRN, AUX
  5. Format only: Validates format, not actual file existence

When to Use

  • Windows file path format validation
  • File upload validation on Windows systems
  • Path sanitization
  • When you need to ensure valid Windows path format
  • File system operations on Windows

For production, consider:

  • Supporting UNC paths if needed
  • Supporting relative paths
  • Checking for reserved Windows filenames
  • Validating path length limits (MAX_PATH = 260 chars)
  • Using OS-specific path validation libraries

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern