Hi there. I'd like to use, the ExtractFileName function which extracts from FullFileName the file name substring and
use native windows characters. First of all, if you need to use some of these functions, e.g. ExtractFileDrive,
ExtractFileDir, ExtractFilePath, ExtractFileName or ExtractFileExt functions in your pas2js programs
- you must set some key variables correctly in your pas2js startup program, otherwise, those functions won't work as expected!
Pas2JS pascal source code
var
fullFileName : String = 'C:\Program Files\Borland\Delphi7\Projects\Unit1.dcu';
begin
// Set the variables correctly in the program startup, otherwise it will not work.
PathDelim : Char = '\';
AllowDirectorySeparators := ['\', '/'];
// 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));
end;
(* ----> output <------
Drive = C:
Dir = C:\Program Files\Borland\Delphi7\Projects
Path = C:\Program Files\Borland\Delphi7\Projects\
Name = Unit1.dcu
Ext = .dcu
------------------------ *)
(...)
this.FormatSettings = null;
this.CurrencyFormat = 0;
this.NegCurrFormat = 0;
this.CurrencyDecimals = 2;
this.CurrencyString = "$";
this.ExtractFilePath = function (FileName) {
var Result = "";
var i = 0;
var EndSep = {};
i = FileName.length;
EndSep = rtl.unionSet(pas.System.AllowDirectorySeparators,pas.System.AllowDriveSeparators);
while ((i > 0) && !$impl.CharInSet$1(FileName.charAt(i - 1),EndSep)) i -= 1;
if (i > 0) {
Result = pas.System.Copy(FileName,1,i)}
else Result = "";
return Result;
};
this.ExtractFileDrive = function (FileName) {
var Result = "";
var i = 0;
var l = 0;
Result = "";
l = FileName.length;
if (l < 2) return Result;
if ($impl.CharInSet$1(FileName.charAt(1),pas.System.AllowDriveSeparators)) {
Result = pas.System.Copy(FileName,1,2)}
else if ($impl.CharInSet$1(FileName.charAt(0),pas.System.AllowDirectorySeparators) && $impl.CharInSet$1(FileName.charAt(1),pas.System.AllowDirectorySeparators)) {
i = 2;
while ((i < l) && !$impl.CharInSet$1(FileName.charAt((i + 1) - 1),pas.System.AllowDirectorySeparators)) i += 1;
i += 1;
while ((i < l) && !$impl.CharInSet$1(FileName.charAt((i + 1) - 1),pas.System.AllowDirectorySeparators)) i += 1;
Result = pas.System.Copy(FileName,1,i);
};
return Result;
};
this.ExtractFileName = function (FileName) {
var Result = "";
var i = 0;
var EndSep = {};
i = FileName.length;
EndSep = rtl.unionSet(pas.System.AllowDirectorySeparators,pas.System.AllowDriveSeparators);
while ((i > 0) && !$impl.CharInSet$1(FileName.charAt(i - 1),EndSep)) i -= 1;
Result = pas.System.Copy(FileName,i + 1,2147483647);
return Result;
};
this.ExtractFileExt = function (FileName) {
var Result = "";
var i = 0;
var EndSep = {};
var SOF = false;
Result = "";
i = FileName.length;
EndSep = rtl.unionSet(rtl.unionSet(pas.System.AllowDirectorySeparators,pas.System.AllowDriveSeparators),rtl.createSet(pas.System.ExtensionSeparator.charCodeAt()));
while ((i > 0) && !$impl.CharInSet$1(FileName.charAt(i - 1),EndSep)) i -= 1;
if ((i > 0) && (FileName.charAt(i - 1) === pas.System.ExtensionSeparator)) {
SOF = (i === 1) || (FileName.charCodeAt(i - 1 - 1) in pas.System.AllowDirectorySeparators);
if (!SOF || pas.System.FirstDotAtFileNameStartIsExtension) Result = pas.System.Copy(FileName,i,2147483647);
} else Result = "";
return Result;
};
this.ExtractFileDir = function (FileName) {
var Result = "";
var i = 0;
var EndSep = {};
i = FileName.length;
EndSep = rtl.unionSet(pas.System.AllowDirectorySeparators,pas.System.AllowDriveSeparators);
while ((i > 0) && !$impl.CharInSet$1(FileName.charAt(i - 1),EndSep)) i -= 1;
if ((i > 1) && $impl.CharInSet$1(FileName.charAt(i - 1),pas.System.AllowDirectorySeparators) && !$impl.CharInSet$1(FileName.charAt(i - 1 - 1),EndSep)) i -= 1;
Result = pas.System.Copy(FileName,1,i);
return Result;
};
(...)
$mod.$main = function () {
pas.System.PathDelim = "\\";
pas.System.AllowDirectorySeparators = rtl.createSet(92,47);
window.console.log("Drive = " + pas.SysUtils.ExtractFileDrive($mod.fullFileName));
window.console.log("Dir = " + pas.SysUtils.ExtractFileDir($mod.fullFileName));
window.console.log("Path = " + pas.SysUtils.ExtractFilePath($mod.fullFileName));
window.console.log("Name = " + pas.SysUtils.ExtractFileName($mod.fullFileName));
window.console.log("Ext = " + pas.SysUtils.ExtractFileExt($mod.fullFileName));
};