Home >Backend Development >C++ >How Can I Validate a String as a Legal Windows Filename?

How Can I Validate a String as a Legal Windows Filename?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-24 16:37:11852browse

How Can I Validate a String as a Legal Windows Filename?

Ensure Windows file names are valid

When integrating filename renaming functionality into your application, always ensure that the entered target file pattern complies with Windows naming conventions. While basic regular expressions can capture alphanumeric characters, they cannot accommodate language-specific characters.

Windows rules for valid file names:

According to Microsoft's documentation, valid Windows file names follow the following convention:

  • Any character in the current code page (Unicode/ANSI greater than 127) is valid except the following characters:
    • : " / | ? *
    • Integers represent characters between 0 and 31 (except ASCII spaces)
    • Characters not allowed by the target file system (e.g., trailing periods or spaces)
    • Reserved DOS names (e.g. CON, PRN, AUX)
  • Filenames consisting only of periods are invalid.

Optional Notes:

  • Maximum file path length (including file name): 260 characters (excluding ? prefix)
  • Maximum Unicode file path length (including filename) using ? prefix: 32,000 characters

The above is the detailed content of How Can I Validate a String as a Legal Windows Filename?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn