3

Possible Duplicate:
How can I sanitize a string for use as a filename?

I need to create a folder from a user's text (unicode string type). Some characters are illegals for folder name under windows.

Do you know a Delphi function that check if the folder name is valid ?
Or best of best, to convert a string to a valid folder name (stripping or converting invalid characters) ?

Thx for your help !

EDIT : See the note below on illegal keywords.

Community
  • 1
  • 1
TridenT
  • 4,879
  • 1
  • 32
  • 56
  • 1
    Important note : it is not **just** a matter of forbiden characters, but also illegal words : 'CON', 'COM', 'LPT', 'NUL', 'PRN', 'AUX' ... must I manually check it ? – TridenT Feb 24 '11 at 14:44
  • @TridenT I'm not aware of a function that gives you those illegal names. Do you have documentation somewhere that calls them out? When checking remember to use case-insenstive `SameText`! – David Heffernan Feb 24 '11 at 14:51
  • @TridenT This question is a duplicate (see comment above), as it happens. Digger tracked down the original. The answer given there by Alexandre should do the job for you. – David Heffernan Feb 24 '11 at 17:33
  • @TridenT - You have missing requirements. Do you plan to open a file? Write? Verify if it exists? Under this set of requirements, CON, COM, LPT, etc. are still valid file names, you just can't use them to name files. Same for creating a file named, for example, "C:\" -- that's valid but you can't use it to name a file. Please be more specific. – Leonardo Herrera Feb 24 '11 at 18:39
  • @Leonardo As I wrote it : **create a folder from a string** . – TridenT Feb 24 '11 at 21:06
  • @TridenT - Oh, I see. What happens if it exists? Can it be on a shared drive? Do you want it to create the full path? – Leonardo Herrera Feb 24 '11 at 21:34
  • @Leonardo My issue is not the folder creation, but to have a **valid** folder name compatible with windows filesystem NTFS. – TridenT Feb 25 '11 at 19:01

3 Answers3

7

See the StackOverFlow answer for How can I sanitize a string for use as a filename?

Hope this helps and has what your are looking for!

EDIT: Removed About.com link by popular demand! If you want that link, please see comments

Community
  • 1
  • 1
AMadmanTriumphs
  • 4,888
  • 3
  • 28
  • 44
1

You can try this code

if CreateDir(DirectoryName) then
begin
  Showmessage('Valid directory name and directory created');
end
else
begin
  Showmessage('Failed');
end; 
Bharat
  • 6,828
  • 5
  • 35
  • 56
  • 3
    This is not a good solution since creating the directory could fail for other reasons (access rights for example). There is no way to distinguish that here. In addition to that the OP mentioned correcting the folder name. – jpfollenius Feb 24 '11 at 15:02
  • That is no motive for the downvote. The OP states that he wants to create the directory; failing because this cannot be done is actually expected, isn't it? – Leonardo Herrera Feb 24 '11 at 21:35
  • I did not downvote. Seems that some one voted this up anyway to make up for the downvote. – jpfollenius Feb 25 '11 at 07:52
0

You can easily do this on your own using ContainsStr and ReplaceStr from StrUtils unit. I don't know if there is an API method for that, maybe someone else can answer this.

jpfollenius
  • 16,456
  • 10
  • 90
  • 156