AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Convert text string to acceptable filename format

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
kiwijunglist



Joined: 26 May 2009
Posts: 8

PostPosted: Tue Dec 22, 2009 11:11 pm    Post subject: Convert text string to acceptable filename format Reply with quote

Hi

Is there an existing function / script that will convert a text string containing extended characters into a text string that complies with windows filename formating standards?

(I wasn't able to find one by searching the forums)

Thanks
Mike
Back to top
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 460

PostPosted: Wed Dec 23, 2009 9:06 am    Post subject: Reply with quote

One thing I can think of that is really simple is
Code:
;replace any chars that are not "a-zA-Z0-9_" with underscore
txt := RegExReplace(txt, "\W", "_")
If your text contains only extended chars, you might want another solution, though, because there won't be anything left but underscores!
_________________
Back to top
View user's profile Send private message
kiwijunglist



Joined: 26 May 2009
Posts: 8

PostPosted: Wed Dec 23, 2009 12:53 pm    Post subject: Reply with quote

Thanks Micahs.

What about replacing an extended character?

eg. Replace all â with a

txt := RegExReplace(txt, "â", "a")
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Wed Dec 23, 2009 1:01 pm    Post subject: Reply with quote

Code:
txt=âáä
MsgBox % RegExReplace(txt, "â|á|ä", "a") ; or use [âáä]

; Or use a loop
txt=âáäéë
replace=âa|áa|äa|ée|ëe
Loop, Parse, Replace, |
   {
    StringSplit, Part, A_LoopField
    StringReplace, txt, txt, %Part1%, %Part2%, All
   }
MsgBox % txt

_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
kiwijunglist



Joined: 26 May 2009
Posts: 8

PostPosted: Wed Dec 23, 2009 1:08 pm    Post subject: Reply with quote

Thank you both very much.

So I can use the Loop method, then use the "/W" to replace anything i have missed.

So the final code would be

Code:

txt=mïchâël©

;replace ïâë -> iae
replace=ïi|âa|ëe
Loop, Parse, Replace, |
   {
    StringSplit, Part, A_LoopField
    StringReplace, txt, txt, %Part1%, %Part2%, All
   }

;replace any chars that are not "a-zA-Z0-9_" with underscore
txt := RegExReplace(txt, "\W", "_")
Back to top
View user's profile Send private message
Guest






PostPosted: Wed Dec 23, 2009 10:50 pm    Post subject: Reply with quote

Quote:
Not sure what OS you guys are using, but this is the official Microsoft answer:

Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

* The following reserved characters: < > : " / \ | ? *
* Integer value zero, sometimes referred to as the ASCII NUL character.
* Characters whose integer representations are in the range from 1 through 31, except for streams where these characters are allowed.
* Any other character that the target file system does not allow.

The only obvious conversion into filename problem are the :, / and ? chars that are used in certain titles (have never seen any other of the reserved ones, unless somebody can point them out, actually scratch that, I just remembered movie "Bat*21").

So "Star Wars; Episode III - Revenge of the Sith [2005].mkv", "Æon Flux [2005].mkv" and "Vi på Väddö [1958].avi" are perfect valid filenames. The only reason I sometimes do not use those weird characters is when the auto-approve rate declines from it.

My personal replacement list is as follows:

* : = ;
* / = _
* ? = (nothing, just remove)
* * = ¤ (ALT+0164)
* " = '' (two single quotes)
* < = [ or (
* > = ] or )
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group