Help with simple enhancement to the replace script

If you made a script you can offer it to the others here, or ask help to improve it. You can also report here bugs & problems with existing scripts.
Post Reply
scottdw
Posts: 23
Joined: 2004-01-13 19:44:57

Help with simple enhancement to the replace script

Post by scottdw »

Here is the replace script that I use and it works:

Code: Select all

    program FindReplace;

const
  WholeWord = False;
  Search = '\\tower\\Movies\\';
  Replace = 'c:\Movies\';
  Field = fieldURL;

var
  s: string;
begin
  s := GetField(Field);
  if WholeWord then
  begin
    if s = Search then
      SetField(Field, Replace);
  end
  else
    SetField(Field, StringReplace(s, Search, Replace));
end.
I am going to be putting my movies each in their own folder named after the movie file:
Before: c:\Movies\Die Hard.mkv
After: c:\Movies\Die Hard\Die Hard.mkv

I want to name the folder after the Original Title field and though I could use this:

Code: Select all

    program FindReplace;

const
  WholeWord = False;
  Search = '\\tower\\Movies\\';
 [b] Replace = 'c:\Movies\' + MovieName + '\";[/b]
  Field = fieldURL;

var
  s: string;
begin
  s := GetField(Field);
  if WholeWord then
  begin
    if s = Search then
      SetField(Field, Replace);
  end
  else
    SetField(Field, StringReplace(s, Search, Replace));
end.
but I need help getting the info for MovieName from the "Original Title" field.

I tried what I could understand from looking at other scripts but could not get it.

Thanks,
Scott
FinderX
Posts: 29
Joined: 2010-05-03 09:30:44

Post by FinderX »

use GetField(fieldOriginalTitle) and store in a variable, for example

MovieName := GetField(fieldOriginalTitle);

If you have problem with MovieName, perhaps is for the declaration of Replace in CONST.

Or the problem is another?
[/code]
antp
Site Admin
Posts: 9629
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

As you do not want a static value as replace string, but something built from another info, it has to be done differently.
Remove "remplace" from the "const" group and put it in the "var" group (like "s") :

Code: Select all

Replace: string;
then just after the "s := GetField..." put:

Code: Select all

Replace := 'c:\Movies\' + GetField(fieldOriginaltTitle) + '\';
scottdw
Posts: 23
Joined: 2004-01-13 19:44:57

Post by scottdw »

Thanks for the help but it is still not working for me but probably a mistake on my part.
I made the changes per antp to this:

Code: Select all

    program FindReplace;

const
  WholeWord = False;
  Search = '\\tower\\Movies\\';
  Field = fieldURL;

var
  s: string;
Replace: string;
begin
  s := GetField(Field);
  Replace := 'c:\Movies\' + GetField(fieldOriginalTitle) + '\';
  if WholeWord then
  begin
    if s = Search then
      SetField(Field, Replace);
  end
  else
    SetField(Field, StringReplace(s, Search, Replace));
end.
Did I mess up somewhere??

It did not make any changes...it just kept the path at the \\tower\movies.
antp
Site Admin
Posts: 9629
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

you say that the path stays as \\tower\movies but you assigned to Search constant the value '\\tower\\Movies\\' ... that's normal that it does not find (and replace) it, since it is different :D
Assign the real value to Search then.
Or if you want to process all records, not just those with a particular value, you can just do this:

Code: Select all

program NewScript;
var
  s: string;
begin
  s := 'c:\Movies\' + GetField(fieldOriginalTitle) + '\';
  SetField(fieldUrl, s);
end.
this will set the URL field of all selected movies
scottdw
Posts: 23
Joined: 2004-01-13 19:44:57

Post by scottdw »

Doh....thanks that worked!!!!
scottdw
Posts: 23
Joined: 2004-01-13 19:44:57

Post by scottdw »

Not to be pain here but I have another request that may be easy. Ultimately I want to change the URL field to \\tower\Movies\MovieTitle\MovieTitle.extention
Example: \\tower\Movies\rush hour.mkv
to
\\tower\Movies\rush hour\rush hour.mkv

the problem I am having is the extention...they all don't have the same extention. Is there a way to look at what is currently inthe URL field to get the extention then apply the above code and have the correct extention?


Thanks,
Scott
antp
Site Admin
Posts: 9629
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Also keep current file name? Or just the extension?
Maybe it is easier to do the replacement as originally planned then...

Code: Select all

program NewScript;
var
  s: string;
begin
  s := StringReplace(GetField(fieldUrl), 'c:\Movies\', 'c:\Movies\' + GetField(fieldOriginalTitle) + '\');
  SetField(fieldUrl, s);
end.
It will replace c:\Movies\ by that + the title, keeping the current file name and extension at the end
But if you run it more than once on the same list, it will each time add a folder level.
scottdw
Posts: 23
Joined: 2004-01-13 19:44:57

Post by scottdw »

That's perfect...THANKS!!!!!


I just wanted to keep the extension....but this works just fine.


Thanks again!!!!
scottdw
Posts: 23
Joined: 2004-01-13 19:44:57

Post by scottdw »

Ok I am asking ALOT so if you just want to ignore this post...feel free :hihi:

Some of my "OriginalTitle" fields don't have the name I want my folder to be.

Is it possible to pull out just the filename in the URL field, save it as a var...call it VAR1 for example. Then take VAR1 and strip the extension off and call it VAR2 and rename the URL field to: c:\Movies\VAR2\VAR1
Example:
Current URL: c:\Movies\Rush Hour.mkv
Make VAR1=Rush Hour.mkv
Make VAR2=Rush Hour
Which would make the URL field: c:\Movies\Rush Hour\Rush Hour.mkv

REALLY sorry to be a pain and your help is GREATLY appreciated!!!!!
antp
Site Admin
Posts: 9629
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Using LastPos function of StringUtils1 unit you can find the position of last \ then copy the remaining text in a variable.
Same for copying part of name before the dot.
scottdw
Posts: 23
Joined: 2004-01-13 19:44:57

Post by scottdw »

Totally can't believe I figured it out!!!!!!

Thanks for the help.

Here is what I used:

Code: Select all

    program Replacename;
uses
  StringUtils1; 
var
  s: string;
  Value: string;
  Value2: string;
begin
Value := TextAfter(GetField(fieldUrl), '\\tower\\Movies\\');
Value2 := TextBefore(Value, '.', '');
  s := StringReplace(GetField(fieldUrl), '\\tower\\Movies\\', '\\tower\Movies\' + Value2 + '\');
  SetField(fieldURL, s);
end.
Don't know if it's the right way or ideal but it works perfectly :D

Thanks for all the help!
Post Reply