Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2019-07

ExtractFileName function

8 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

warleyalex

There are an issue on the ExtractFileName function | win64 platform

...an workaround would be:

function ExtractFileName(str: String): String;
begin
  asm
    Result = str.split('\\').pop().split('/').pop();
  end;
end;
function ExtractFileNameWithoutExt(str: String): String;
begin
  asm
    Result = str.split('\\').pop().split('/').pop().split('.')[0];
  end;
end;

Reply by email

Michael Van Canneyt
to warleyalex

Can you please tell us what the issue is, give a sample program that demonstrates it ?

These functions come from the FPC RTL, if there was an issue, it would probably have been reported a long time ago ?

Michael.

warleyalex wrote 27 lines
There are an issue on the ExtractFileName function | win64 platform ...an workaround would be: function ExtractFileName(str: String): String; begin asm Result = str.split('\\').pop().split('/').pop(); end; end; function ExtractFileNameWithoutExt(str: String): String; begin asm Result = str.split('\\').pop().split('/').pop().split('.')[0]; end; end; -- Sent from: http://pas2js.38893.n8.nabble.com/ _______________________________________________ Pas2js maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/pas2js

Reply by email

warleyalex
to Michael Van Canneyt
var
  fullFileName : string;
begin
  // Set up a full file name with drive and path
  fullFileName := 'C:\Program Files\Borland\Delphi7\Projects\Unit1.dcu';
// Show the component parts of this full name
  console.log('Drive = '+ExtractFileDrive (fullFileName));
  console.log('Dir   = '+ExtractFileDir   (fullFileName));
  console.log('Path  = '+ExtractFilePath  (fullFileName));
  console.log('Name  = '+extractfilename  (fullFileName));
  console.log('Ext   = '+ExtractFileExt   (fullFileName));
  console.log(ExtractFileName_(fullFileName));
  console.log(ExtractFileNameWithoutExt_(fullFileName));
(* outputs this:

Drive = C: // ---> OK
Dir = C: // ----> should be C:\Program
Files\Borland\Delphi7\Projects
Path = C: //----> should be C:\Program
Files\Borland\Delphi7\Projects\
Name = \Program Files\Borland\Delphi7\Projects\Unit1.dcu // ---> should be Unit1.dcu
Ext = .dcu // ----> OK

Unit1.dcu // ---> OK
Unit1 // ---> OK

*)

Reply by email

Michael Van Canneyt
to warleyalex

Hello,

Did you set the PathDelim and AllowDirectorySeparators variables correctly ?

By default they are set to URL separators:

   PathDelim : Char = '/';
   AllowDirectorySeparators : Set of Char = ['/'];

If you want to use the native windows characters you must set them in the startup of your program, like this:

   PathDelim : Char = '\';
   AllowDirectorySeparators : Set of Char = ['/','\'];

Otherwise it will not work.

Your proposed solution is wrong for unix or the web (URL), because you hardcode \ which is correct only on windows.

The nodejsapp unit could probably detect the correct setting and set the variables automatically.

Michael.

warleyalex wrote 44 lines
var fullFileName : string; begin // Set up a full file name with drive and path fullFileName := 'C:\Program Files\Borland\Delphi7\Projects\Unit1.dcu'; // Show the component parts of this full name console.log('Drive = '+ExtractFileDrive (fullFileName)); console.log('Dir = '+ExtractFileDir (fullFileName)); console.log('Path = '+ExtractFilePath (fullFileName)); console.log('Name = '+extractfilename (fullFileName)); console.log('Ext = '+ExtractFileExt (fullFileName)); console.log(ExtractFileName_(fullFileName)); console.log(ExtractFileNameWithoutExt_(fullFileName)); (* outputs this: Drive = C: // ---> OK Dir = C: // ----> should be C:\Program Files\Borland\Delphi7\Projects Path = C: //----> should be C:\Program Files\Borland\Delphi7\Projects\ Name = \Program Files\Borland\Delphi7\Projects\Unit1.dcu // ---> should be Unit1.dcu Ext = .dcu // ----> OK Unit1.dcu // ---> OK Unit1 // ---> OK *) -- Sent from: http://pas2js.38893.n8.nabble.com/ _______________________________________________ Pas2js maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/pas2js

Reply by email

warleyalex
to warleyalex: var fullFileName : string; begin // Set up a full file name with drive and path…

Windows uses the backslash (\) for the file system delimiter. For everything else the forward slash is used (/).

If I define the path using forward slash (/).

fullFileName : String = 'C:/Program
Files/Borland\Delphi7/Projects/Unit1.dcu';

outputs this:

Drive = C:
Dir = C:/Program Files/Borland\Delphi7/Projects
Path = C:/Program Files/Borland\Delphi7/Projects/ Name = Unit1.dcu
Ext = .dcu

Unit1.dcu
Unit1

Reply by email

Michael Van Canneyt
to warleyalex

See my other mail.

Michael.

warleyalex wrote 26 lines
Windows uses the backslash (\) for the file system delimiter. For everything else the forward slash is used (/). If I define the path using forward slash (/). fullFileName : String = 'C:/Program Files/Borland\Delphi7/Projects/Unit1.dcu'; outputs this: Drive = C: Dir = C:/Program Files/Borland\Delphi7/Projects Path = C:/Program Files/Borland\Delphi7/Projects/ Name = Unit1.dcu Ext = .dcu Unit1.dcu Unit1 -- Sent from: http://pas2js.38893.n8.nabble.com/ _______________________________________________ Pas2js maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/pas2js

Reply by email

warleyalex
to warleyalex: Windows uses the backslash (\) for the file system delimiter. For everything else the…
AllowDirectorySeparators := ['\'];
fullFileName : String = 'C:\Program
Files\Borland\Delphi7\Projects\Unit1.dcu';
// Show the component parts of this full name
  console.log('Drive = '+ExtractFileDrive (fullFileName));
  console.log('Dir   = '+ExtractFileDir   (fullFileName));
  console.log('Path  = '+ExtractFilePath  (fullFileName));
  console.log('Name  = '+extractfilename  (fullFileName));
  console.log('Ext   = '+ExtractFileExt   (fullFileName));

outputs correctly
===========
Drive = C:
Dir = C:\Program Files\Borland\Delphi7\Projects
Path = C:\Program Files\Borland\Delphi7\Projects\ Name = Unit1.dcu
Ext = .dcu

Reply by email