#1 2007-11-05 18:05

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

PascalScript: Split / Tokenize / Wrap string into parts

How can i split FILENAME into parts at an given sign with PascalScript?

i.e.
we have FILENAME "some file name.txt"

and i want to split it with something like      x := SPlit(FileName, ' ')

to get

x[0] some
x[1] file
x[2] name



Pascals 'Wrap' give error in PascalScript.

I searched and found some code but nothing work with PascalScript.


// from  http://www.delphipraxis.net/topic27196_splitfunktion.html
function Split(const fText: String; const fSep: Char; fTrim: Boolean=false; fQuotes: Boolean=false):TStringList;
xxxxyyyyzzzz
end;

//========

var
  part: array of string;
begin

part := Split(FileName, ' ');
  
  FileName := part[2];
end

Last edited by Stefan (2007-11-06 19:08)


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#2 2007-11-06 13:07

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,440

Re: PascalScript: Split / Tokenize / Wrap string into parts

Have a look at the Help document for PascalScript rule. It lists all of the available functions/procedures and types (excluding basic data types like integers, strings, etc). I didn't register any classes like TStringList, because it means redeclaring all these classes/units for PascalScript. I prefer to keep all of the functionality to functions for several reasons, e.g. safer, more consistent, much less overhead, etc.

Anyway, you need to use this function below:
function WideSplitString(const Input, Delimiter: WideString): TStringsArray;

var
  I: Integer;
  Parts: TStringsArray;
begin
  Parts := WideSplitString(FilePath, '\');
  for I:=0 to Length(Parts)-1 do
  begin
    // access each part via Parts[i]
  end;
end.

Offline

#3 2007-11-06 17:50

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: PascalScript: Split / Tokenize / Wrap string into parts

> Have a look at the Help document for PascalScript rule.
I swear i have used the search function big_smile But obviously i am not able to do an correct search. roll Today i see it too.

> I didn't register any classes like TStringList,
I even not know what TStringList is, i have copied just the code.

> I prefer to keep all of the functionality to functions
So you wrote functions in Pascal and provide this us inside the ReNamer.exe?

> Anyway, you need to use this function below:
lol Thank you.


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#4 2007-11-06 19:06

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: PascalScript: Split / Tokenize / Wrap string into parts

Thanks tongue it works:

// Swaps FileName-parts at minus-sign.pas

const
  // sign where to split the file name:
  // f. ex. "Artist - Song.mp2" split at ' - '
  SplitSign = ' - ';

var
  Parts: TStringsArray;

begin

  // split the filename at '-' into parts:
    Parts := WideSplitString(WideExtractBaseName(FileName), SplitSign);

  // Note: parts are numbered from 0 on: 0, 1, 2, 3...

  // build your new name: 
  // Here: second part[1]  first, then SpaceMinusSpace, then the first part[0]:
  FileName := Parts[1] + ' - ' + Parts[0] + WideExtractFileExt(FileName);
  
end.

Spelling and grammar corrections allowed.


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#5 2007-11-12 12:20

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,440

Re: PascalScript: Split / Tokenize / Wrap string into parts

For this task, you can also use a simple RegEx rule...

Expression: (.*) - (.*)
Replace: $2 - $1

big_smile

Offline

#6 2007-11-12 12:57

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: PascalScript: Split / Tokenize / Wrap string into parts

I know and i know that you know tongue
But average user can't remember RegEx so they could just select an Script to do the same (and i imaging more to do with it)


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

Board footer

Powered by FluxBB