Filters (12): Parent Folder Name

Would you like Bulk Rename Utility to offer new functionality? Post your comments here!

Re: Filters (12): Parent Folder Name

Postby TheGhost78 » Tue Nov 26, 2024 4:49 am

Yes.

Also, please look into the bug when certain brackets are in the folder name.
TheGhost78
 
Posts: 181
Joined: Fri Jul 19, 2024 11:25 am

Re: Filters (12): Parent Folder Name

Postby Admin » Tue Nov 26, 2024 7:22 am

Code: Select all
function insertAfterText(inputString, searchText, insertText) {
    // Find the index of the searchText
    const index = inputString.indexOf(searchText);

    // If searchText is found (index is not -1)
    if (index !== -1) {
        // Insert the insertText after the searchText
        return inputString.slice(0, index + searchText.length) + insertText + inputString.slice(index + searchText.length);
    }

    // Return the original string if searchText is not found
    return inputString;
}

originalString = name;
searchFor = object("container");
textToInsert = " - ";

newName = insertAfterText(originalString, searchFor, textToInsert);
Admin
Site Admin
 
Posts: 2923
Joined: Tue Mar 08, 2005 8:39 pm

Re: Filters (12): Parent Folder Name

Postby Admin » Tue Nov 26, 2024 7:34 am

TheGhost78 wrote:If the parent folder contains [, ( or ) then it comes up with a syntax error using the three formulas below. It doesn't happen with ], { or } in the folder name.

name.search(object('container')) == -1
name.toLowerCase().search(object('container').toLowerCase()) == -1
!name.match(object('folder').replace(/.*\\(.+)\\$/,'$1'))


This formula works fine:

!name.startsWith(object('folder').replace(/.*\\(.+)\\$/,'$1'))



The expression name.search('[') == -1 gives a syntax error because the character [ is a special character in regular expressions, and the search() method interprets its argument as a regular expression.

The Problem:
The [ character is part of a character class in regular expressions and must be escaped with a backslash (\\) to be used literally.
Without escaping, the [ is invalid syntax in the regular expression.

Solution:
Escape the [ character by writing \\[.


To avoid a syntax error when using name.search(text) == -1, you need to ensure that text is properly escaped if it contains special characters that are interpreted as part of a regular expression.

JavaScript provides a way to escape special characters in strings used as regular expressions by using the String.replace() method with a regular expression to escape all such characters.

Escape Function
You can create a function to escape special characters in the text:

Code: Select all
function escapeRegExp(text) {
    return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}


Correct Usage
Now you can use the escaped version of text in the search() method:

Code: Select all
name.search(escapeRegExp(object('container'))) == -1


Explanation
/[.*+?^${}()|[\]\\]/g: This regular expression matches any special character used in regular expressions, such as ., *, +, ?, etc.
\\$&: This replaces each matched special character with its escaped version.
Escaped text: The search() method treats the escaped text as a literal string, avoiding syntax errors.
Admin
Site Admin
 
Posts: 2923
Joined: Tue Mar 08, 2005 8:39 pm

Re: Filters (12): Parent Folder Name

Postby Admin » Tue Nov 26, 2024 7:41 am

Inline for Filters (12) Javascript Condition:

Code: Select all
name.search(object('container').replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) == -1
Admin
Site Admin
 
Posts: 2923
Joined: Tue Mar 08, 2005 8:39 pm

Previous

Return to Suggestions