0

I am using ExtractFileName of a WideString that equals to 'STF:宋体'. The result is '??'.

It seems that ExtractFileName does not handle unicode well.

Is there an equivalent method for unicode?

Edit:

This is the line i use

NameStr := ExtractFileName(Name);

the types are:

NameStr:String;

Name:PWideChar
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Erik Sapir
  • 23,209
  • 28
  • 81
  • 141
  • 4
    ExtractFileName works perfectly with a Unicode Delphi. Which version are you using? – David Heffernan Apr 05 '11 at 07:13
  • Tested in Delphi XE and got `宋体` when running `ShowMessage(ExtractFileName('STF:宋体'));` – Mikael Eriksson Apr 05 '11 at 07:13
  • Not had any problems using ExtractFileName with Unicode file names. You do know that WideString <> UnicodeString (which is the Delphi string default)? Please show your code and especially the variable declarations, or we will just be stabbing in the dark. – Marjan Venema Apr 05 '11 at 07:14
  • 1
    The question marks make me believe you're using a non-unicode string variable somewhere along the call (you get question marks for characters that can't be represented as AnsiString). You should step throw your code (using F7 - step into) and look at the code; You're probably not calling the `ExtractFileName` you expect! – Cosmin Prund Apr 05 '11 at 07:19
  • added the code and types i use – Erik Sapir Apr 05 '11 at 07:23

4 Answers4

4

You can also use TntSysUtils.WideExtractFileName from TNT.

Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
1

You appear to be using an old version of Delphi without support for Unicode. The solution is to upgrade Delphi.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • You are correct - i am using Delphi 2006. Upgrading the version is not an option at this point (can't take the risk). Is there a way to solve this issue without upgrade? – Erik Sapir Apr 05 '11 at 07:37
  • You'll have to roll your own versions of all string manipulation routines. If there are a lot then an upgrade would be the less risky. If there are only a handful then you might be alright. You'll need to stop using string (which is AnsiString) and stick to WideString. – David Heffernan Apr 05 '11 at 07:59
0
Function ExtractFileNameW(const FN: widestring): widestring;
 begin
  Result := UTF8Decode(ExtractFileName(UTF8Encode(FN)));
 end;
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
0

You can use a home-cooked version of ExtractFileName, like this:

function ExtractFileNameW(FullPath:WideString):WideString;
var i,pos:Integer;
begin
  // Find the last path separator
  pos := -1;
  for i:=Length(FullPath) downto 1 do
    if (FullPath[i] = '/') or (FullPath[i] = '\') then
    begin
      pos := i;
      Break;
    end;
  if pos = -1 then
    Result := FullPath
  else
    begin
  Result := '';
      SetLength(Result, Length(FullPath)  - pos);
      System.Move(FullPath[pos+1], Result[1], (Length(FullPath) - pos) * SizeOf(WideChar));
    end;
end;

... but if you really need to deal with Unicode characters you should upgrade to XE. It really makes a lot of difference.

Cosmin Prund
  • 25,498
  • 2
  • 60
  • 104