#21 2009-07-26 23:07

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

Re: Removing a simbol at last position

Good work krtek. smile  I have learned something from you. (BTW there is an typo in your script)



I have included InputBox()'s to make it more flexible if you don't mind?



//http://www.den4b.com/forum/viewtopic.php?pid=3454#p3454
// krtek (26 Feb 2009)
const

//SCRIPT CONTROLS:

//REGEX = '(.+) - (.+) - (.+)';  exchanged with InputBox
//CHANGE_CASE = '103';  

SKIP_EXTENTION = true;     //true or false
CASE_SENSITIVE = False;    //Should searching with regex be case sensitive? Default: False
FULL_MATCH_CHECK = True;   //Should the script warn you if regex didn't match the full filename? Default: True;
        

//Legend for CHANGE_CASE parameter:
//0. No changes
//1. Capitalize Every Word
//2. lowercase
//3. UPPERCASE
//4. iNVERT cASE
//5. First letter capital
//You may specify case for every capturing group (brackets) in regex as a string of appropriate digits.
//If the CHANGE_CASE string is shorten then number of capturing groups then the last digit is used till the end of regex.
//If the CHANGE_CASE string is longer, spare digits are ignored. 
//eg. '102' means that first matched brackets will be uppercased, second - won't be changed and third (and every brackets after it) - lowercased.



//END OF SCRIPT CONTROLS


BREAK_STRING = 'BREAK';
FOR_ALL_STRING = 'DO NOT ASK';
STANDARD_ANSWER = BREAK_STRING+' to exit, '+FOR_ALL_STRING+' to continue';



var
  Matches, SubPatterns : TStringsArray;
  TempFilename, FileNameProcessedPart : WideString;
  start_pos, end_pos, i: Integer;
  Initialized, UserAsket, TimeToExit, NotAFullMatch, CheckIfFullMatch : Boolean;
  CaseFlags : array of Integer;
  CaseFlag : Integer;
  Answer, REGEX, CHANGE_CASE, Info, SE, CS, FMC, myScriptName  : WideString;
  


procedure procGetUserInput;
begin

    myScriptName := 'krtek''s CaseChanger'; 
    
    If SKIP_EXTENTION = True Then
        SE := 'True'
     Else
        SE := 'False';

    If CASE_SENSITIVE = True Then
        CS := 'True'
     Else
        CS := 'False';

    If FULL_MATCH_CHECK = True Then
        FMC := 'True'
     Else
        FMC := 'False';

    Info := Info + #10 + 'Note: check script source for this settings:' +#10
    Info := Info + 'SKIP_EXTENTION: '   + #9 + SE +#10 
    Info := Info + 'CASE_SENSITIVE: '   + #9 + CS +#10 
    Info := Info + 'FULL_MATCH_CHECK: ' + #9 + FMC +#10 
  REGEX :=  WideInputBox(myScriptName, 'Match this RegEx on file name:' +#10 +Info, '(.+) - (.+) - (.+)');
  //If REGEX = False then exit;
  
  Info := '0. No changes' +#10
  Info := Info + '1. Capitalize Every Word' +#10
    Info := Info + '2. lowercase' +#10
    Info := Info + '3. UPPERCASE' +#10
    Info := Info + '4. iNVERT cASE' +#10
    Info := Info + '5. First letter capital' +#10 +#10
    Info := Info + 'F.ex.:' +#9 + 'use case type 1 for first match ( ),' +#10 +#9 
    Info := Info + '0 for second and 3 for third' +#10 +#9 + '=> 103' +#10 +#10
  CHANGE_CASE := WideInputBox(myScriptName, 'Chose case for every single match group ( )'+#10+'of your RegEx:' +#10 + REGEX +#10 +#10 + Info, '103' );
  //If CHANGE_CASE = False then exit;
  
  UserAsket := true;
end;

  

function SetCaseFlags(s : WideString) : array of Integer;
var
i : Integer;
LoopEnd : Integer;
CaseIDs : array of Integer;

begin

  SetLength(CaseIDs,Length(s));
 
  LoopEnd:=Length(s)-1;
  for i:=0 to LoopEnd do
    begin
      CaseIDs[i]:= StrToInt(WideCopy(s,1,1));
      s:=WideCopy(s,2,Length(s)-1);
    end;

  result:= CaseIDs;
end;


procedure Initialize;

begin
  Answer:=STANDARD_ANSWER;
  CheckIfFullMatch:=FULL_MATCH_CHECK;
  Initialized:=true;
  CaseFlags:=SetCaseFlags(CHANGE_CASE);
  FilePath := 'not used'
end;



begin
  
  
if not TimeToExit then
begin

    if not UserAsket then procGetUserInput;
  if not Initialized then Initialize;
  
  TempFilename:='';
  end_pos:=0;
  start_pos:=1;
  SetLength(SubPatterns,0);
  SetLength(Matches,0);
  NotAFullMatch:=false;
  
  if SKIP_EXTENTION then
    FileNameProcessedPart:=WideStripExtension(FileName)
  else 
    FileNameProcessedPart:=FileName;

  Matches:=MatchesRegEx(FileNameProcessedPart, REGEX, CASE_SENSITIVE);
  if Length(Matches) <= 0 then exit;
  if Matches[0] <> FileNameProcessedPart then NotAFullMatch:=true;
  SubPatterns:=SubMatchesRegEx(Matches[0],REGEX,CASE_SENSITIVE);
  if Length(SubPatterns) <=0 then exit;
  
  for i:=0 to Length(SubPatterns)-1 do
    if SubPatterns[i]<>'' then
    begin
      end_pos:=WidePos(SubPatterns[i], Matches[0])-1;
      TempFileName:=TempFileName+WideCopy(Matches[0], start_pos, end_pos-start_pos+1);
      start_pos:=end_pos+Length(SubPatterns[i])+1;
      
      if i < length(CaseFlags)  then
        CaseFlag:=CaseFlags[i]
      else 
        CaseFlag:=CaseFlags[Length(CaseFlags) -1];
        
      case CaseFlag of
        0 : TempFilename:=TempFileName+SubPatterns[i];
        1 : TempFilename:=TempFileName+WideCaseCapitalize(SubPatterns[i]);   
        2 : TempFilename:=TempFileName+WideLowerCase(SubPatterns[i]);
        3 : TempFilename:=TempFileName+WideUpperCase(SubPatterns[i]);
        4 : TempFilename:=TempFileName+WideCaseInvert(SubPatterns[i]);
        5 : TempFilename:=TempFileName+WideUpperCase(SubPatterns[i][1])+WideLowerCase(WideCopy(SubPatterns[i], 2, Length(SubPatterns[i])-1));
          
        else
          begin  
            ShowMessage('The value of CHANGE_CASE constant is invalid' +#10+'Check legend for info:' +#10 +#10 + Info);
            TimeToExit:=True;
            break;
          end;
     end;

   end;
  
  
  if start_pos < Length(FileNameProcessedPart) then
    TempFilename:=TempFileName+WideCopy(FileNameProcessedPart, start_pos, Length(FileNameProcessedPart)-start_pos+1);
 

  if NotAFullMatch then
    if CheckIfFullMatch then    
       begin
         if not WideInputQuery('RegEx didn''t match the full filename.', 'Old filename:'+#10+FileName+#10+#10+'Proposed new filename: '+#10+TempFileName+WideExtractFileExt(FileName)+#10+#10+'What should we do?'#10+#10+'Pressing OK button = Rename that file'+#10+'Pressing Cancek button = Skip that file'+#10+'Type '''+BREAK_STRING+''' in the text field to exit the script or '''+FOR_ALL_STRING+''' if you want to continue with all the files.'+#10, Answer) then
           exit
         else if WideSameText(WideLowerCase(Answer),BREAK_STRING) then 
           begin
             TimeToExit:=True;
             exit;
           end
         else if WideSameText(WideLowerCase(Answer),FOR_ALL_STRING) then 
           begin
             CheckIfFullMatch:=False;
           end
         else
           Answer:=STANDARD_ANSWER;
       end;
   

  if SKIP_EXTENTION then   
    FileName:=TempFileName+WideExtractFileExt(FileName)
  else FileName:=TempFileName;
  

end
else exit;
 
end.


----------------


BTW: the else case is not executed if i enter '7' or '99999'
I had no time to check this.

   case CaseFlag of
        0 : TempFilename:=TempFileName+SubPatterns[i];
        1 : TempFilename:=TempFileName+WideCaseCapitalize(SubPatterns[i]);   
        2 : TempFilename:=TempFileName+WideLowerCase(SubPatterns[i]);
        3 : TempFilen
          
        else
          begin  
            ShowMessage('T


---

Now who include an option to exchange the position of the matching ( ) parts for output file name ? big_smile

----


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

#22 2009-07-27 14:11

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Removing a simbol at last position

I'm not sure if I will use the version with InputBoxes (I like that I can see Regex and CaseChange strings in the rules area). But I must say it's nice.
I've just done some clean-up and exchanged InputBoxes with InputQueries.

//http://www.den4b.com/forum/viewtopic.php?pid=3454#p3454
// krtek (26 Feb 2009)
const

//SCRIPT CONTROLS:

SKIP_EXTENTION = true;     //true or false
CASE_SENSITIVE = False;    //Should searching with regex be case sensitive? Default: False
FULL_MATCH_CHECK = True;   //Should the script warn you if regex didn't match the full filename? Default: True;
        

DEFAULT_REGEX = '(.+) - (.+) - (.+)';  //exchanged with InputBox
DEFAULT_CHANGE_CASE = '103';  

//Legend for CHANGE_CASE parameter:
//0. No changes
//1. Capitalize Every Word
//2. lowercase
//3. UPPERCASE
//4. iNVERT cASE
//5. First letter capital
//You may specify case for every capturing group (brackets) in regex as a string of appropriate digits.
//If the CHANGE_CASE string is shorten then number of capturing groups then the last digit is used till the end of regex.
//If the CHANGE_CASE string is longer, spare digits are ignored. 
//eg. '102' means that first matched brackets will be uppercased, second - won't be changed and third (and every brackets after it) - lowercased.



//END OF SCRIPT CONTROLS


BREAK_STRING = 'BREAK';
FOR_ALL_STRING = 'DO NOT ASK';
STANDARD_ANSWER = BREAK_STRING+' to exit, '+FOR_ALL_STRING+' to continue';
SCRIPT_NAME = 'krtek''s CaseChanger';


var
  Matches, SubPatterns : TStringsArray;
  TempFilename, FileNameProcessedPart : WideString;
  start_pos, end_pos, i: Integer;
  Initialized, TimeToExit, NotAFullMatch, CheckIfFullMatch : Boolean;
  CaseFlags : array of Integer;
  CaseFlag : Integer;
  Answer, Regex, ChangeCase, Info  : WideString;
  


function BoolToStr(condition : boolean) : WideString;
begin
   If condition Then
        result := 'True'
     Else
        result := 'False';
end;


procedure procGetUserInput;
begin

    
    Info := Info + #10 + 'Note: check script source for this settings:' +#10
    Info := Info + 'SKIP_EXTENTION: '   + #9 + BoolToStr(SKIP_EXTENTION) +#10 
    Info := Info + 'CASE_SENSITIVE: '   + #9 + BoolToStr(CASE_SENSITIVE) +#10 
    Info := Info + 'FULL_MATCH_CHECK: ' + #9 + BoolToStr(FULL_MATCH_CHECK) +#10 
    Regex:=DEFAULT_REGEX;
    If not WideInputQuery(SCRIPT_NAME, 'Match this RegEx on file name:' +#10 +Info, Regex) then 
    begin
      TimeToExit := true;
      exit;
    end;
 
  
  Info := '0. No changes' +#10
  Info := Info + '1. Capitalize Every Word' +#10
    Info := Info + '2. lowercase' +#10
    Info := Info + '3. UPPERCASE' +#10
    Info := Info + '4. iNVERT cASE' +#10
    Info := Info + '5. First letter capital' +#10 +#10
    Info := Info + 'F.ex.:' +#9 + 'use case type 1 for first match ( ),' +#10 +#9 
    Info := Info + '0 for second and 3 for third' +#10 +#9 + '=> 103' +#10 +#10
    ChangeCase := DEFAULT_CHANGE_CASE;
  
  If not WideInputQuery(SCRIPT_NAME, 'Choose case for every single match group ( )'+#10+'of your RegEx:' +#10 + Regex +#10 +#10 + Info, ChangeCase ) then 
  begin
    TimeToExit := true;
    exit;
  end;
 

end;

  

function SetCaseFlags(s : WideString) : array of Integer;
var
i : Integer;
LoopEnd : Integer;
CaseIDs : array of Integer;

begin

  SetLength(CaseIDs,Length(s));
 
  LoopEnd:=Length(s)-1;
  for i:=0 to LoopEnd do
    begin
      CaseIDs[i]:= StrToInt(WideCopy(s,1,1));
      s:=WideCopy(s,2,Length(s)-1);
    end;

  result:= CaseIDs;
end;


procedure Initialize;

begin
  Answer:=STANDARD_ANSWER;
  CheckIfFullMatch:=FULL_MATCH_CHECK;
  FilePath := 'not used';   //just to shut up the compiler
  procGetUserInput;
  CaseFlags:=SetCaseFlags(ChangeCase);
  Initialized:=true;
end;



begin
  
  
if not TimeToExit then
begin

  if not Initialized then Initialize;
  
  TempFilename:='';
  end_pos:=0;
  start_pos:=1;
  SetLength(SubPatterns,0);
  SetLength(Matches,0);
  NotAFullMatch:=false;
  
  if SKIP_EXTENTION then
    FileNameProcessedPart:=WideStripExtension(FileName)
  else 
    FileNameProcessedPart:=FileName;

  Matches:=MatchesRegEx(FileNameProcessedPart, Regex, CASE_SENSITIVE);
  if Length(Matches) <= 0 then exit;
  if Matches[0] <> FileNameProcessedPart then NotAFullMatch:=true;
  SubPatterns:=SubMatchesRegEx(Matches[0],Regex,CASE_SENSITIVE);
  if Length(SubPatterns) <=0 then exit;
  
  for i:=0 to Length(SubPatterns)-1 do
    if SubPatterns[i]<>'' then
    begin
      end_pos:=WidePos(SubPatterns[i], Matches[0])-1;
      TempFileName:=TempFileName+WideCopy(Matches[0], start_pos, end_pos-start_pos+1);
      start_pos:=end_pos+Length(SubPatterns[i])+1;
      
      if i < length(CaseFlags)  then
        CaseFlag:=CaseFlags[i]
      else 
        CaseFlag:=CaseFlags[Length(CaseFlags) -1];
        
      case CaseFlag of
        0 : TempFilename:=TempFileName+SubPatterns[i];
        1 : TempFilename:=TempFileName+WideCaseCapitalize(SubPatterns[i]);   
        2 : TempFilename:=TempFileName+WideLowerCase(SubPatterns[i]);
        3 : TempFilename:=TempFileName+WideUpperCase(SubPatterns[i]);
        4 : TempFilename:=TempFileName+WideCaseInvert(SubPatterns[i]);
        5 : TempFilename:=TempFileName+WideUpperCase(SubPatterns[i][1])+WideLowerCase(WideCopy(SubPatterns[i], 2, Length(SubPatterns[i])-1));
          
        else
          begin  
            ShowMessage('The value of ChangeCase parameter is invalid.' +#10+'ChangeCase string cannot contain '+inttostr(CaseFlag)+ '.'+#10+'Check legend for info.' +#10 +#10 +'Script will be terminated.');
            TimeToExit:=True;
            break;
          end;
     end;

   end;
  
  
  if start_pos < Length(FileNameProcessedPart) then
    TempFilename:=TempFileName+WideCopy(FileNameProcessedPart, start_pos, Length(FileNameProcessedPart)-start_pos+1);
 

  if NotAFullMatch then
    if CheckIfFullMatch then    
       begin
         if not WideInputQuery('RegEx didn''t match the full filename.', 'Old filename:'+#10+FileName+#10+#10+'Proposed new filename: '+#10+TempFileName+WideExtractFileExt(FileName)+#10+#10+'What should we do?'#10+#10+'Pressing OK button = Rename that file'+#10+'Pressing Cancek button = Skip that file'+#10+'Type '''+BREAK_STRING+''' in the text field to exit the script or '''+FOR_ALL_STRING+''' if you want to continue with all the files.'+#10, Answer) then
           exit
         else if WideSameText(WideLowerCase(Answer),BREAK_STRING) then 
           begin
             TimeToExit:=True;
             exit;
           end
         else if WideSameText(WideLowerCase(Answer),FOR_ALL_STRING) then 
           begin
             CheckIfFullMatch:=False;
           end
         else
           Answer:=STANDARD_ANSWER;
       end;
   

  if SKIP_EXTENTION then   
    FileName:=TempFileName+WideExtractFileExt(FileName)
  else FileName:=TempFileName;
  

end
else exit;
 
end.
Stefan wrote:

(BTW there is an typo in your script)

Could you point it out? It might be helpful if I get back to the version without InputBox.

Stefan wrote:

BTW: the else case is not executed if i enter '7' or '99999'

It pop-ups an error message that ChangeCase string is invalid and terminates the script.

Stefan wrote:

Now who include an option to exchange the position of the matching ( ) parts for output file name ?

No way!!! big_smile
That's what RegEx rule is for!

Last edited by krtek (2009-07-27 21:41)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#23 2009-08-12 00:15

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Removing a simbol at last position

Just to note, I've added the script (in both versions) to the appropriate Wiki section.

Still waiting for the typo, Stefan. roll


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#24 2009-08-12 08:20

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

Re: Removing a simbol at last position

krtek wrote:

Still waiting for the typo, Stefan. roll

Ohh?

This script
==> http://www.den4b.com/forum/viewtopic.php?pid=3454#p3454

didn't run for me because of an typo in the var definition:
var
  ...
  CaseFlagss : array of Integer;
  CaseFlag : Integer;


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

#25 2009-08-12 11:28

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Removing a simbol at last position

Thanks, Stefan.
I've got no idea how it managed to get there :-)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

Board footer

Powered by FluxBB