ReNamer:Scripts:RegEx Case Convertion
Jump to navigation
Jump to search
The script compensates for the lack of the ability of ReNamer's RegEx engine to change case of groups captured by RegEx.
User needs to provide REGEX and CHANGE_CASE parameters to the script controls. The exact form of the CHANGE_CASE string is explained in the legend below the main controls.
Tested
- ReNamer 5.50
Code
Author: krtek. Date: 26 July 2009.
const
//SCRIPT CONTROLS:
REGEX = '(.+) - (.+) - (.+)';
CHANGE_CASE = '102';
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:
//1. Capitalize Every Word
//2. lowercase
//3. UPPERCASE
//4. iNVERT cASE
//5. First letter capital
//0. No changes
//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, j : Integer;
Initialized, TimeToExit, NotAFullMatch, CheckIfFullMatch : Boolean;
CaseFlags : array of Integer;
CaseFlag : Integer;
Answer : WideString;
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);
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 CHANGE_CASE constant is invalid. Check legend for 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.
Thanks to the Stefan's involvement there exists also a version of the script that uses dialog boxes to ask user for required data so no direct changes to the script are needed.
Code 2
Author: krtek & Stefan. Date: 27 July 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 = '102';
//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.