Rename To {clip} with illegal characters

Hello, if I want to rename a file with a clipboard string which contains illegal characters [ : * ? " < > ], I can do this using "Rename Inline (F2)" and paste, and it will strip any of them out (Windows default behaviour), however if I use "Rename To {clip}", it causes an error. Please can you modify the "Rename To {clip}" function to do the same thing. I understand that slashes will create a folder tree and that's pretty cool.

Thank you.

Try {clip|cleanfilepath}.

Thanks for the suggestion lxp, but this code actually does substitution of the illegal characters...

: = ;

  • = #
    ? = !
    " = '
    < = (

= )

I would like them stripped out like Windows Explorer does. What's the best way to do that? Should I write a script to do this?

Cheers.

There is also MakeLegal() and RegEx(). These will get the job done.

Evaluator Functions

Thank you, I will check them out :+1:t3:

I cannot get the RegEx evaluator expression to work, let alone include a double quote in the character class.

Rename To "{=RegEx(Clip(),"[<>?:*|]+",""=}" IgnoreExt

I even tried much simpler:

Rename To {=RegEx("cats and dogs","dogs","frogs")=} IgnoreExt

... and I get left with "frogs", I would expect "cats and frogs"

Is it something to do with the quotes or escaping?

Unfortunately for me, MakeLegal() does the same substitution as {clip|cleanfilepath}.

Yes. Try

str="characters [ : * ? "" < > ], I can do ";
RegEx(str, "(.*)[\[:\*\?""<>\]]+(.*)#", "\1\2")

Hi lxp, thanks for looking into this.

I don't have enough brainpower left today, I may try tomorrow, but I think that perhaps, since rename from clipboard functionality is much like pasting into an inline rename, maybe you can try to replicate what Windows does and simply strip any illegal chatacters by default (or add a /switch) - they will never be accepted by the OS anyway.

That would stop people being able to paste wildcard and regular expressions into the Rename window, which would be quite a pain. :slight_smile:

We already provide a way to make the clipboard text safe. What's wrong with substituting characters with similar ones rather than removing them entirely? What happens if all the characters are illegal and you end up with an empty filename? (It also makes clashes with another file less likely, although those are still possible of course.)

FWIW, for similar context, here's what I use, having a dialog box to fine tune the destination name :

@nodeselect
@evalalways:=myNewName = RegExS(Clip(), "[:<>/\\\|\?\*]", " -"); Output("'"+myNewName+"'"); 
@evalalways:=myNewName = Trim(myNewName); Output("'"+myNewName+"'"); 
@evalalways:=myNewName = RegExS(myNewName, "[\r\n]+$", ""); Output("'"+myNewName+"'"); 
@Set targetName="{dlgstring|Target Name|{=myNewName=}}"

Rename PATTERN * TO {$targetName} IGNOREEXT

You could split the first RegExS into two separate ones, one like this and another one replacing with nothing (depending on the characters you want to remove vs. replace by something else).

Not using a rename window here, "Rename To" applies the string, in this case {clip}, Windows will, by default, strip out illegal characters.

Really, what I'd like is for "Rename To {clip}" to work the same as a simple Rename Inline, then paste.

I gave you my similar button code that has a dialog, you don't need to keep it if you don't want it :

@nodeselect
Rename PATTERN * TO "{=RegExS(Clip(), "[:<>/\\\|\?\*]", "")=}" IGNOREEXT

If you want to keep the ability to get subfolders with / in the clipboard, remove it from the regexp.
I did not try latest @lxp suggestion, but this should be pretty similar.

Thank you @PassThePeas, your suggestion is working perfectly.