You are not logged in.
Hi
I would like to propose a new function for the application titled "TrimStart" and also "TrimEnd", I'm familiar to see this kind of functions in .NET, where you can specify the characters to trim at the start or at the end (or else both Start/End using the fucntion "Trim")
I think that would be a great "default" part for the application, but by the moment I need help to trim a string in that way.
My rename rules gets me this resulting string:
- Dj Paul - Bass! .
I need to trim the "-" and " " at the start, and the "." and " " at the end.
Someone could help me with this? maybe a pascal script?
thanks for read
Elektro.
Offline
Can't you use the Delete, Remove, Replace or RegEx rules? If it has to be done via PascalScript then WideDelete() can help you delete the first two characters ("- ") but not the last two.
Instead you can use something like ReplaceRegEx(). See the following code snippet:
const
str = '- Dj Paul - Bass! .';
begin
WideShowMessage('"' + str + '"');
WideShowMessage('"' + ReplaceRegEx(str, '\A(- )(.*?)( \.)\Z', '$2', false, true) + '"');
end.
If you don't understand it go through the RegEx primer on the wiki carefully.
BTW, here I'm directly using the WideString return value of ReplaceRegEx() in WideShowMessage(). If you want you can modify the string itself as follows:
var
str: WideString;
begin
str := '- Dj Paul - Bass! .';
WideShowMessage('"' + str + '"');
str := ReplaceRegEx(str, '\A(- )(.*?)( \.)\Z', '$2', false, true);
WideShowMessage('"' + str + '"');
end.
Last edited by Andrew (2014-09-03 23:18)
Offline
There is no good solution to solve this trimming issue right now.
I think, there should be a more general way to TrimLeft and TrimRight as a another CleanUp rule.
I have tried to make a TrimAround den4b ReNamer PascalScript for this task, please try with test files or Shift+A.
FROM:
-,, - Dj Paul - Bass! , ..ext
- Name - 0001_.txt
_ Name - 0002 -.txt
,Name - 0003, , - ___---,.txt
TO:
Dj Paul - Bass!.ext
Name - 0001.txt
Name - 0002.txt
Name - 0003.txt
Use this PascalScript:
((see the wiki for an how-to: http://www.den4b.com/wiki/ReNamer:Rules:PascalScript))
// den4b ReNamer PascalScript (Stefan, v0.001, 2014-09-04)
// to Trim a string at both ends from a collection of signs
// Example "- Dj Paul - Bass! ..ext" >> "Dj Paul - Bass!.ext"
//-----------------------------------------------------------------
// U S E R S E T T I N G S
//Collection of signs to remove.
//(RegEx-Syntax, so, if contains hyphen, put them at the very end!)
const
TrimLeft = ' ,._-'; //remove space,comma, dot, underscore, hyphen
TrimRight = ' ,._-';
// T H E C O D E, DON'T TOUCH
var
BaseName, Extension, Temp, Reverse: WideString;
I, Count: Integer;
begin
BaseName := WideExtractBaseName(FileName);
Extension := WideExtractFileExt(FileName);
//----------------------------------- Reverse (as RegEx greediness is a concern
//----------------------------------- and trim right first (but no matter about this order)
Reverse := '';
Count := Length(BaseName);
for I := 1 to Count do
Reverse := BaseName[i] + Reverse;
// +
//ReplaceRegEx(Input, Find, Replace, CaseSensitive, UseSubstitution)
Temp := ReplaceRegEx(Reverse, '['+TrimRight+']*(.+)', '$1' , false, true)
Temp := WideTrim(Temp); //just in case this was not part of the collection
//----------------------------------- Reverse to the right order and trim left now
BaseName := '';
Count := Length(Temp);
for I := 1 to Count do
BaseName := Temp[i] + BaseName;
// +
BaseName := ReplaceRegEx(BaseName, '['+TrimLeft+']*(.+)', '$1' , false, true)
BaseName := WideTrim(BaseName);
//----------------------------------- Bonus: trim doubled spaces, hyphens, underscore inside
BaseName := ReplaceRegEx(BaseName, '\s+' , ' ' ,false,true)
BaseName := ReplaceRegEx(BaseName, '-+' , '-' ,false,true)
BaseName := ReplaceRegEx(BaseName, '_+' , '_' ,false,true)
//----------------------------------- Compose new file name
FileName := BaseName + Extension;
end.
HTH?
If yes, please donate to Denis.
Last edited by Stefan (2014-09-04 08:14)
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
I reversed the string as I didn't found a good reg ex to match.
Normal I would use
TrimLeft+(.+)+TrimRight but this is to greedy.
TrimLeft+(.+?)+TrimRight is to lazy.
TrimLeft+(.+\w)+TrimRight will not work if last sign should be e.g. a '!' sign.
TrimLeft+(.+)([\w\d!])+TrimRight could work?
And shure enought, this works:
BaseName := ReplaceRegEx(BaseName, '['+TrimLeft+']*(.+[A-Za-z\d!])['+TrimRight+']*', '$1' , false, true)
Again, \w will not work because this includes the underscore, so I use A-Za-z
And, if need, you have to add more signs to the [A-Za-z\d!] collection. The current will match one char, digit or the exclamation mark.
My new PascalScript:
((see the wiki for an how-to: http://www.den4b.com/wiki/ReNamer:Rules:PascalScript))
// den4b ReNamer PascalScript (Stefan, v0.003, 2014-09-04)
// to Trim a string at both ends from a collection of signs
// Example "- Dj Paul - Bass! ..ext" >> "Dj Paul - Bass!.ext"
//-----------------------------------------------------------------
// U S E R S E T T I N G S
//Collection of signs to remove.
// (RegEx-Syntax Character Class, so, if contains hyphen, put them at the very end!)
// See http://www.regular-expressions.info/charclass.html
const
TrimLeft = ' ,._-'; //remove space,comma, dot, underscore, hyphen at the begin
LastCharOfFileName = 'A-Za-z\d!'; //match end of filename before signs to trim
TrimRight = ' ,._-'; //remove space,comma, dot, underscore, hyphen at the end
{
############################################
http://www.den4b.com/forum/viewtopic.php?pid=8678#p8678
FROM:
-,, - Dj Paul - Bass! , ..ext
- Name - 0001_.txt
_ Name --- 0002 -.txt
,Name __ 0003, , - ___---,.txt
TO:
Dj Paul - Bass!.ext
Name - 0001.txt
Name - 0002.txt
Name _ 0003.txt
############################################
}
// T H E C O D E, DON'T TOUCH
var
BaseName, Extension: WideString;
begin
BaseName := WideExtractBaseName(FileName);
Extension := WideExtractFileExt(FileName);
BaseName := ReplaceRegEx(BaseName, '['+TrimLeft+']*(.+['+LastCharOfFileName+'])['+TrimRight+']*', '$1' , false, true)
BaseName := WideTrim(BaseName);
//----------------------------------- Bonus: trim doubled spaces, hyphens, underscore inside
BaseName := ReplaceRegEx(BaseName, '\s+' , ' ' ,false,true)
BaseName := ReplaceRegEx(BaseName, '-+' , '-' ,false,true)
BaseName := ReplaceRegEx(BaseName, '_+' , '_' ,false,true)
//----------------------------------- Compose new file name
FileName := BaseName + Extension;
end.
See if it works or if I screw it up again ;-)
EDIT:
rises LastCharOfFileName up to User Setting level.
Last edited by Stefan (2014-09-04 18:26)
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
Thankyou both!, I didn't expected this kind of help.
The new script version works excellent at least with the filenames that i've tried.
Just a little question: I don't need to worry about escape any RegEx reserved character in the user-settings constant vars?
Thanks again guys.
Offline
Thanks for the feedback!
Just a little question: I don't need to worry about escape any RegEx reserved character in the user-settings constant vars?
Sure, you should take care and know the rules.
So I have for example put the hyphen at the very end (or at the very begin) to remove its special meaning.
My TrimXXX -expression should be taken as Regular Expression "Character Classes":
see e.g. http://www.regular-expressions.info/charclass.html
With a "character class", also called "character set", ..... match only one out of several characters.
.....place the characters .... between square brackets. If you want to match an "a" OR an "e", use [ae].In most regex flavors, the only special characters or metacharacters inside a character class
are the closing bracket (]), the backslash (\), the caret (^), and the hyphen (-).
Your regex will work fine if you escape the regular metacharacters inside a character class, ...
I have added this info in my script above >> http://www.den4b.com/forum/viewtopic.php?pid=8679#p8679
Last edited by Stefan (2014-09-04 15:28)
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
There is no good solution to solve this trimming issue right now.
I think, there should be a more general way to TrimLeft and TrimRight as a another CleanUp rule.
I think it would be great if WideDelete() is enhanced so that if index is negative, it'll delete count characters from the end of the string backwards.
Offline
Stefan wrote:There is no good solution to solve this trimming issue right now.
I think, there should be a more general way to TrimLeft and TrimRight as a another CleanUp rule.I think it would be great if WideDelete() is enhanced so that if index is negative, it'll delete count characters from the end of the string backwards.
Generally yes, always a good improvement to have this functions count from the right end too.
But for the issue in this very thread, how would you automatically determine the need amount for the count argument?
procedure WideDelete(var S: WideString; Index, Count: Integer);
Deletes Count characters from S, starting from the Index position.
http://www.den4b.com/wiki/ReNamer:Pasca … :Functions
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
Hey guys, listen to my idea, maybe could be absurd but surely will solve the problem at once.
Stefan, as the developer of this app you could consider to depend on a runtime optimized 3rd party (CLI) exe developed in VB.NET to solve this issue? this way you could implement the TRimming functions in your app in a managed way.
I'll explain it better:
As I've said at the start of this topic, the .NET class library provides 3 methods of trimming (well, are more, but 3 to mention here), the functions are "TrimStart", "TrimEnd", and "Trim", with its overloads of course, what's the point of all this?: well, you can specify various characters in the parameter that takes each function, this means that for example you can trim "_" and "," and "=" just with one call at this function, with the security that provides a Microsoft native function (I really appreciate your Scripts above, but a Regex or a Pascal procedure that needs to depend on RegEx can't be compared with the efficient and performance of a .NET framework methods).
Then, knowing what I've said above, my idea is, in case that you would, I can develop a CLI app that just takes the chars to trim as an argument, and returns the trimmed string, sounds good, right?.
So for example, calling the application from console could be like this syntax:
Trimmer.exe /TrimStart ";.-%/()" ";.-%/()The string to trim"
Trimmer.exe /TrimEnd ";.-%/()" "The string to trim;.-%/()"
Trimmer.exe /Trim ";.-%/()" ";.-%/()The string to trim;.-%/()"
You can specify me the syntax, or the exitcode values that my app should return when success, the targeting .NET framework, or any thing that you need to work with.
The implementation of this is very easy 'cause I don't need to hardcode anything, as I've said .NET framework provides those functions, so I just should design the UI, take the parameters and return the resulting string.
You know RegEx motor is very slow, I think that this app wouldn't disturb the performance of your app 'cause it avoids Regex instancing.
The problem I see is I don't know if implement the usage of an external exe through your application could be a problem.
Stefan, if you are interested in this kind of solution then please just reply to this post, I'm susbscribed via email notification and I would be happy to help with this program.
And if you need my email: ElektroStudios@ElHacker.Net
PS: Sorry for my English.
Thanks for read.
Elektro.
Last edited by Elektro (2014-09-08 09:59)
Offline
Elektro, I am good, but not that good. Denis is the developer of this apps at den4b.com
I would never ever have the patience to develop and support even a little tool for more than three days
Kudos to Denis!
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