Page 1 of 1

Script Command to Remove Illegal File Name Characters ?

Posted: 22 Jan 2024 22:44
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.

Re: Script Command to Remove Illegal File Name Characters ?

Posted: 22 Jan 2024 22:51
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.

Re: Script Command to Remove Illegal File Name Characters ?

Posted: 22 Jan 2024 23:01
by highend
$validFileName = regexreplace($fileName, "(<|>|:|""|/|\\|\||\?|\*)");
or alternatively: "[<>:""/\\|?*]"

Re: Script Command to Remove Illegal File Name Characters ?

Posted: 22 Jan 2024 23:10
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.