Script Command to Remove Illegal File Name Characters ?

Discuss and share scripts and script files...
Post Reply
GreetingsFromPoland
Posts: 224
Joined: 29 Sep 2022 14:20
Location: Win10 @125%

Script Command to Remove Illegal File Name Characters ?

Post by GreetingsFromPoland »

hi! i've been experimenting with a rename script for certain files that i have. the name of the file isn't important, so i originally went with the HASH of the file using the hash() function - works great, especially when i shorten it to the first 7 like github format.

today i had the idea to use the digest hash using code i found here in another thread - it too works really well (uses runret with openssl). the problem i face now is there is sometimes a character that is illegal to use in a file name, for example, the / :

Code: Select all

DYXtPYGyGoFqoCr33g8SmNAMUKDaTUVM30DyNui6CV/FL8GxV5g8OkwXGue1/qtG
is there an XY SC that i can use to strip out these types of characters ? something to make it 100% "legal" as a file name.

i'm going through the different commands in the help file now but nothing has stood out. i may not end up using it and just the hash() results, but it would be nice to figure it out.

jupe
Posts: 2805
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Script Command to Remove Illegal File Name Characters ?

Post by jupe »

If you use rename "b" mode, you can use flag 4.

edit: actually you may have to test that because on second thought, that might cause issues with the fwd slash because b mode accepts / cmds, you could manually replace those first with replace(), otherwise just use replacelist or regexreplace to manually replace invalid chars.

highend
Posts: 13333
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Script Command to Remove Illegal File Name Characters ?

Post by highend »

$validFileName = regexreplace($fileName, "(<|>|:|""|/|\\|\||\?|\*)");
or alternatively: "[<>:""/\\|?*]"
One of my scripts helped you out? Please donate via Paypal

GreetingsFromPoland
Posts: 224
Joined: 29 Sep 2022 14:20
Location: Win10 @125%

Re: Script Command to Remove Illegal File Name Characters ?

Post by GreetingsFromPoland »

thank you both! i ended up using the regexreplace in my script as i was using renameitem instead of plain rename. only tested a ~100 files (jpgs) so far, but everything seems to be working as before it would have failed early on due to slashes, etc.

Post Reply