Solved

string to acceptable filename

Posted on 1998-05-31
3
157 Views
Last Modified: 2010-04-06
I need a procedure RemoveInvalidFileCharacters(var Inputstring: string);
which substitutes invalid characters (that is for a Windows 95 filesystem)
by spaces.
e.g.   'TESTname' -> 'TESTname'
e.g.  'éééxamp^^^le' ->     '   xamp   le'
0
Comment
Question by:miauw
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
Edo082297 earned 100 total points
ID: 1349475
Hi Miauw,
  Here is your function. Note that Windows 95 will allow you to store filenames containing weird characters (other countries use these strange symbols; they even speak with them ;). You can also store filenames with special symbols like % and ^. You may not, however, use filenames that contain the following symbols: [\, /, :, *, ?, ", <, >, | ]. So, all we do is filter those out. If you want to block specific characters, you will have to add it to the filter list. Note that the function as it stands filters out some characters that are actually legal in a filename, but strange in the context of a filename (such as ^).

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit2.Text := RemoveInvalidFileCharacters(edit1.Text);
end;

function TForm1.RemoveInvalidFileCharacters(const AFileName : string): String;
var
  i : integer;
begin
  Result := AFileName;

  // access violation will occurr in first iteration of for loop with blank string
  if Length(AFileName) = 0 then
    Exit;

  for i := 0 to Length(AFileName) do
  begin
    if not (AFileName[i] in ['0'..'z'])
        or (AFileName[i] in ['\', '/', ':', '*', '?', '"', '<', '>', '|']) then
      Result[i] := ' '
  end;
end;

Regards,

Edo

0
 

Author Comment

by:miauw
ID: 1349476
Thanks, I was ready by now, but you answered the question before I could
delete it, so I will give you A: Excellent.
0
 
LVL 1

Expert Comment

by:Edo082297
ID: 1349477
Thanks, Miauw!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
add combobox item based on numbers 9 144
Create a path if not exists 7 78
How to build JSON File in Delphi 6 3 35
DBCtrlGrid, Delphi, Scroll 8 15
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question