Indexing part of a string & Deleting characters from there

Javascript renaming examples. Javascript renaming is supported in version 3 or newer.

Indexing part of a string & Deleting characters from there

Postby drothman66 » Sat Jan 18, 2020 4:29 pm

I'm struggling with deleting characters from only part of a string.

For example, i'd like to change "2020-01-01_this-is a test.JPG" and delete the "-" from only the first 10 characters of the filename. Replace won't work because of the "-" that comes later inthe filename.

And how would it change if i wanted to replace the "-" rather than delete it?

thanks
drothman66
 
Posts: 5
Joined: Fri Jun 07, 2013 3:17 pm

Re: Indexing part of a string & Deleting characters from there

Postby therube » Sat Jan 18, 2020 6:05 pm

If your names are as you've shown above...


1:RegEx
Code: Select all
Match:  (\d\d\d\d)-(\d\d)-(\d\d)(.*)
Replace:  \1\2\3\4

Code: Select all
2020-01-01_this-is a test.JPG
20200101_this-is a test.JPG


If you want something else in between the numbers of the "date" (4 digits, dash, 2 digits, dash, 2 digits), then include that between \1 & \2 and \2 & \3.
Code: Select all
Replace:  \1^^\2^^\3\4

Code: Select all
2020-01-01_this-is a test.JPG
2020^^01^^01_this-is a test.JPG
therube
 
Posts: 889
Joined: Mon Jan 18, 2016 6:23 pm

Re: Indexing part of a string & Deleting characters from there

Postby drothman66 » Sat Jan 18, 2020 9:09 pm

thanks. what about the case where the location of the dashes aren't fixed?

IOW, replace all the dashes within the first 10 characters no matter where they are? (and if there aren't any dashes, leave it alone...
drothman66
 
Posts: 5
Joined: Fri Jun 07, 2013 3:17 pm

Re: Indexing part of a string & Deleting characters from there

Postby therube » Sun Jan 19, 2020 5:02 pm

You could do that with JavaScript renaming.


You may ? be able to do it with RegEx, but if so, I'm not sure offhand?
therube
 
Posts: 889
Joined: Mon Jan 18, 2016 6:23 pm

Re: Indexing part of a string & Deleting characters from there

Postby Admin » Mon Jan 20, 2020 4:26 am

Can be done with Javascript:

Code: Select all
ind = 10; // up to position
str_repl = "-"; // text to replace
str_with = ""; // replace with

firstPart = name.substring(0, ind);
secondPart = name.substring(ind);
firstPart = firstPart.replace(new RegExp(str_repl, 'gi'), str_with);

newName = firstPart + secondPart;
Admin
Site Admin
 
Posts: 1848
Joined: Tue Mar 08, 2005 8:39 pm

Re: Indexing part of a string & Deleting characters from there

Postby drothman66 » Thu Jan 23, 2020 6:26 pm

i was hoping there could be a regex expression for something like this. i dont code in javascript, but i can write some code to do in perl or whatever. thx again
drothman66
 
Posts: 5
Joined: Fri Jun 07, 2013 3:17 pm

Re: Indexing part of a string & Deleting characters from there

Postby RegexNinja » Sat Feb 22, 2020 3:41 am

IF your files always start with a date-format (2-digit months/days), you could use something like:
^(\d{2,4})-(\d{1,2})-(\d{2,4}[^\d].*)
\1...\2...\3
2020-01-01_this-is-1-test ---> 2020...01...01_this-is-1-test
01-01-2020_this-is-1-test ---> 01...01...2020_this-is-1-test


If you need to remove/replace dashes within any 1st-10 chars, the easiest way is probably:
^(.{0,9})-(.*)
\1...\2
It removes only-the-last dash from within the 1st-10 chars, so you'd have to click rename once for each dash.

Another thing you can do is throw the match into the #12Filter with Regex checked, then hit F5/refresh.
That would only show names with - somewhere within the 1st-10 chars.
Cheers.
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Indexing part of a string & Deleting characters from there

Postby trm2 » Mon Mar 16, 2020 9:56 am

Thanks to Admin for the JavaScript code because alternatives should always be presented because it can
always help others reading the forum even if the original poster does not request the code. Keep it up!
trm2
 
Posts: 155
Joined: Wed Jan 15, 2020 12:47 pm


Return to Javascript Renaming