Welcome to %s forums

BrainModular Users Forum

Login Register

[script] Removing the path from a string

I need help on a Patch
Post Reply
gthibert
Member
Posts: 46
Location: Chicoutimi, Québec
Contact:

Unread post by gthibert » 07 Mar 2011, 01:29

Anyone knows how I can remove the path from a filename string with a script ?

There's a lot of functions to manipulate strings in the script reference, but I can't figure out wich would allow me to extract a substring.
Guillaume Thibert
www.cemproduction.com

gthibert
Member
Posts: 46
Location: Chicoutimi, Québec
Contact:

Unread post by gthibert » 07 Mar 2011, 03:10

I've found. It could'nt be simpler.

extractFileName(pString);

http://www.delphibasics.co.uk/RTL.asp?N ... ctFileName
Guillaume Thibert
www.cemproduction.com

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 07 Mar 2011, 10:11

cool, i wondered too.

Ken29
Member
Posts: 116
Contact:

Unread post by Ken29 » 13 Mar 2011, 00:06

Hi guys,

I find this post very interesting, but I'm not familiar with scripts and I don't know how to use this information.

I tried to copy this in an empty script but I had a couple of errors. If someone can help me.

Thanks

Kenan

// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;

interface

uses
SysUtils, // Unit containing the ExtractFileName command
Forms, Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;

var
Form1: TForm1;

implementation
{$R *.dfm} // Include form definitions

procedure TForm1.FormCreate(Sender: TObject);
var
fullFileName : string;

begin
// Set up a full file name with drive and path
fullFileName := 'C:Program FilesBorlandDelphi7ProjectsUnit1.dcu';

// Show the component parts of this full name
ShowMessage('Drive = '+ExtractFileDrive (fullFileName));
ShowMessage('Dir = '+ExtractFileDir (fullFileName));
ShowMessage('Path = '+ExtractFilePath (fullFileName));
ShowMessage('Name = '+ExtractFileName (fullFileName));
ShowMessage('Ext = '+ExtractFileExt (fullFileName));
end;

end.

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 14 Mar 2011, 22:04

Hi, I suppose you found a Deplhi example somewhere? Usine is written (mostly?) in Delphi, and you can use quite a lot of the functions and procedures from Delphi within scripts, but far from all. I haven't really programmed in Delphi myself, but I'm pretty sure that the above example creates a simple window of sorts and displays the various parts of the pathfile string.

Here's an example script for Usine accepting one input - the pathfile name - and displays the extracted strings in the console every time the script is initiated or the file name input changes:

Code: Select all

VAR pFileName : tParameter;

PROCEDURE Init;
BEGIN
   pFileName := CreateParam('pathFile name', ptTextField); SetIsOutput(pFileName, FALSE);
END;

PROCEDURE Callback(n: Integer);
   VAR fileName : String;
BEGIN
   fileName := GetStringValue(pFileName);
   WriteLn('Input file name = ' + fileName);
   WriteLn('Drive = ' + ExtractFileDrive(fileName));
   WriteLn('Dir = ' + ExtractFileDir(fileName));
   WriteLn('Path = ' + ExtractFilePath(fileName));
   WriteLn('Name = ' + ExtractFileName(fileName));
   WriteLn('Ext = ' + ExtractFileExt(fileName));
END;
Paste it into an empty script module and compile. You can connect an Interface Control/Open Dialog module to input if you want, but you can also just enter text strings to see how the different extracts.
Bjørn S

Ken29
Member
Posts: 116
Contact:

Unread post by Ken29 » 20 Mar 2011, 10:38

Hi,

Thanks for your answer. Unfortunately it doesn't work as expected. The whole string is getting out of the script. I'll have to search in my archives books about Turbo Pascal and relearn this language...

Image

Thanks

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 21 Mar 2011, 09:19

If what you're after is the "stripped" file name as an output from the script, you have to create an output for it and assign a value to the output. WriteLn (along with sTrace, iTrace and fTrace) just writes to the console for showing messages, debugging etc.

Create another parameter, eg pFileNameOut. This should also be of type ptTextField. In Callback, you can do something like:

Code: Select all

PROCEDURE Callback(n : Integer);
BEGIN
  IF (n = pFileName) THEN
     SetStringValue(pFileNameOut, ExtractFileName(GetStringValue(pFileName)));
END;
Bjørn S

Ken29
Member
Posts: 116
Contact:

Unread post by Ken29 » 29 Mar 2011, 10:26

Hi,
It works perfectly now, thank you very much...

Kenan

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests