Skip to main content
AllDevToolsHub
Back to all patterns

Windows File Path Validation

File & Path

Validates Windows absolute file paths like C:\Users\name\file.txt.

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

How it works

This pattern validates Windows absolute paths starting with a drive letter (A-Z) followed by a colon and backslash. Path segments cannot contain the characters Windows forbids in filenames: \ / : * ? " < > |. The final segment (filename) is optional.

Test Cases

Should Match

  • C:\Users\john\Documents\file.txt
  • D:\Projects\app\src\index.js

Should NOT Match

  • /usr/local/bin
  • C:\invalid<name>\file
  • relative\path

Quick Summary

Validates Windows absolute file paths (C:\folder\file.txt). Enforces drive letter prefix and rejects characters forbidden by Windows in filenames: \ / : * ? " < > |.

Key Takeaways

Key Takeaways

  • Requires a drive letter (A-Z) followed by :\ to be an absolute path
  • Rejects Windows-forbidden filename characters: \ / : * ? " < > |
  • Does not validate that the path actually exists on disk
  • UNC paths (\\server\share) require a different pattern
Use Cases

When to use it

  • Validating file path inputs in Windows desktop applications
  • Sanitizing file paths in cross-platform CLI tools
  • Checking configuration file paths in Windows environments
Watch out

Common Mistakes

  • Using this for Unix paths, Unix uses forward slashes and has different forbidden characters
  • Not handling UNC paths (\\server\share) if your app runs in a network environment
FAQ

Windows File Path Validation, Frequently Asked

How do I validate Unix/Linux file paths?

Unix paths start with / and allow most characters except null bytes. Pattern: ^(\/[^\/\0]+)+\/?$

How do I handle both Windows and Unix paths?

Use Node.js path.isAbsolute() which handles both platforms correctly without regex.