Solved

1 character in all words in givenname should be capitalised.

Posted on 2012-04-11
1
346 Views
Last Modified: 2012-04-11
I have a small task that I hope you guys can help me with.

I have a CSV file in the format of:

GivenName,Surname,Init,Dep,Title

Sometimes givenname is 2 names and the lastname is just 1.

I have done this (see code), but it is tease me.

If there is 1 givenname it is ok, but if there 2 givennames both names are all small.

Hope you can help me.

procedure TFrmMain.Button7Click(Sender: TObject);
var strListOrg, strLineList : TStringlist; I : integer; adComp : IADsContainer;  adUser : IADsUser;  tmp, givenname, lastname : string;
begin
  opendialog1.Filter := 'Comma Delimited (*.csv)|*.CSV';
  if opendialog1.Execute then
  begin
    if opendialog1.FileName <> '' then
    begin
      strListOrg := TStringlist.Create;
      strListOrg.LoadFromFile(OpenDialog1.FileName);
      try
        ADOQuery1.SQL.Clear;
        ADOQuery1.SQL.Text := 'SELECT sAMAccountName, mail FROM '+ Quotedstr('LDAP://OU=USERS,' + edit2.Text) + ' WHERE objectClass='+ Quotedstr('user') + ' ORDER by CN';
        ADOQuery1.Open;
        try
          for I := 0 to strListOrg.Count - 1 do
          begin
            strLineList := TStringList.Create;
            strLineList.Delimiter := ';';
            try
              strLineList.DelimitedText := stringreplace(strListOrg[i],' ','_', [rfReplaceAll]);
              if not ADOQuery1.Locate('sAMAccountName',copy(edit2.text,10,2)+copy(edit2.Text,4,2)+strLineList[2],[]) then
                if not ADOQuery1.Locate('mail', trim(lowercase(stringreplace(strLineList[0],'_',' ',[rfReplaceAll])) + '.' + lowercase(stringreplace(strLineList[1],'_',' ',[rfReplaceAll]))) + '@alfalaval.com',[]) then
                begin
                  tmp := lowercase(trim(stringreplace(strLineList[0],'_',' ',[rfReplaceAll])));
                  givenname := Uppercase(tmp[1]) + copy(tmp,2,length(tmp));
                  tmp := lowercase(trim(stringreplace(strLineList[1],'_',' ',[rfReplaceAll])));
                  lastname := Uppercase(tmp[1]) + copy(tmp,2,length(tmp));
                  tmp := Tmp ;
                end;
            finally
              strLineList.Free;
            end;
          end;
        finally
          ADOQuery1.Close;
        end;
      finally
        strListOrg.Free;
      end;
    end;
  end;
end;

Open in new window

0
Comment
Question by:QC20N
1 Comment
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 500 total points
ID: 37831939
Change your procedure as follows
I've added a function that capitalize the first char even if there're spaces
Note that the last tmp = Tmp is not required!
procedure TFrmMain.Button7Click(Sender: TObject);
  function CapitalizeFirstChar(Name: String): String;
  var
    x: Integer;
    NextUpper: Boolean;
  begin
    NextUpper := True;
    x := 0;
    while x < length(name) do
    begin
      inc(x);
      if NextUpper then
        result := result+Uppercase(name[x])
      else
        result := result+(name[x]);
      NextUpper := name[x] = ' ';
    end;
  end;

var
  strListOrg, strLineList: TStringList;
  I: Integer;
  adComp: IADsContainer;
  adUser: IADsUser;
  tmp, givenname, lastname: string;
begin
  opendialog1.Filter := 'Comma Delimited (*.csv)|*.CSV';
  if opendialog1.Execute then
  begin
    if opendialog1.FileName <> '' then
    begin
      strListOrg := TStringList.Create;
      strListOrg.LoadFromFile(opendialog1.FileName);
      try
        ADOQuery1.SQL.Clear;
        ADOQuery1.SQL.Text := 'SELECT sAMAccountName, mail FROM '+ Quotedstr('LDAP://OU=USERS,' + edit2.Text) + ' WHERE objectClass='+ Quotedstr
          ('user') + ' ORDER by CN';
        ADOQuery1.Open;
        try
          for I := 0 to strListOrg.Count - 1 do
          begin
            strLineList := TStringList.Create;
            strLineList.Delimiter := ';';
            try
              strLineList.DelimitedText := stringreplace(strListOrg[I], ' ', '_', [rfReplaceAll]);
              if not ADOQuery1.Locate('sAMAccountName', copy(edit2.Text, 10, 2)+copy(edit2.Text, 4, 2)+strLineList[2], []) then
                if not ADOQuery1.Locate('mail', trim(lowercase(stringreplace(strLineList[0], '_', ' ', [rfReplaceAll])) + '.' + lowercase
                      (stringreplace(strLineList[1], '_', ' ', [rfReplaceAll]))) + '@alfalaval.com', []) then
                begin
                  givenname := CapitalizeFirstChar(lowercase(trim(stringreplace(strLineList[0], '_', ' ', [rfReplaceAll]))));
                  lastname := CapitalizeFirstChar(lowercase(trim(stringreplace(strLineList[1], '_', ' ', [rfReplaceAll]))));
                  //tmp := tmp; this is not required!
                end;
            finally
              strLineList.free;
            end;
          end;
        finally
          ADOQuery1.Close;
        end;
      finally
        strListOrg.free;
      end;
    end;
  end;
end;

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 121
How to debug For loops? 3 49
can't find the executable in Simulator 1 90
Twebbrowser in Fire monkey android scrolling to bottom 1 30
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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