Skip to content

Instantly share code, notes, and snippets.

@adrianmgg
Created November 30, 2023 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianmgg/0dbd8830b2ffdc13dfb79d266a2e572d to your computer and use it in GitHub Desktop.
Save adrianmgg/0dbd8830b2ffdc13dfb79d266a2e572d to your computer and use it in GitHub Desktop.
# powershell one-liner to generate a regex that rejects filenames containing invalid characters
# NOTE: currently this only handles invalid *characters* - reserved names e.g. COM1 won't be matched by this.
# TODO would be good to handle that too though - either just hardcode it or try one of these
# https://stackoverflow.com/a/70794363/8762161 https://stackoverflow.com/a/54643724/8762161
$valid_filename_pattern = [regex]::new(([System.IO.Path]::GetInvalidFileNameChars() | ForEach-Object {[regex]::Escape($_)} | Join-String -Separator '' -op '^[^' -os ']*$'), 'Compiled, CultureInvariant')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment