No internet connection
  1. Home
  2. How to

Batch Rename, Remove everything before _ ? Regex?

By Jack Green @Jack_Green
    2024-02-15 14:19:07.363Z

    Hi,

    this might not be a sound flow question but given the level of coding workflow gods that hang out here I figured someone might know!

    Trying to remove everything that appears before "_" using batch rename.There could be any number of characters that appear before the _. Im 90% sure this is possible using regular expressions but I after quite a bit of googling I'm getting nowhere.

    Thanks in advance

    Solved in post #6, click to view
    • 5 replies
    1. @Jack_Green, this can be done in several ways but let's start with this simple pattern: .*_
      This will match anything before the last underscore including the underscore itself.
      When used in Batch Rename with regex turned on, a track named songName_GTR will become just GTR.

      Hopefully, that does it but let me know if it mishaves.

      Btw, I like using the following website for testing regex in a visual way: https://regexr.com/

      1. This should also do it, and even more immediately:

        
        for(let oldTrackName of Array.from(sf.ui.proTools.selectedTrackNames))
        {
            let newTrackName = oldTrackName.split('_')[0];
            
            sf.app.proTools.renameTrack({
                oldName: oldTrackName,
                newName: newTrackName,
            });
        }
        
        1. Here it is as a regex:

          
          for(let oldTrackName of Array.from(sf.ui.proTools.selectedTrackNames))
          {
              let newTrackName = oldTrackName.match(/^(.*)_/)[1]; //[1] means take the first captured group - the group is designated by the parentheses
              
              sf.app.proTools.renameTrack({
                  oldName: oldTrackName,
                  newName: newTrackName,
              });
          }
          
          1. In reply tochrscheuer:
            Jack Green @Jack_Green
              2024-02-16 11:44:37.532Z

              Thanks a bunch.

              This actually works great even without using batch rename.

              I changed the [0] to [1] as it was keeping the stuff before the underscore rather than the stuff after.

              1 small issue is if there are 2 underscores it keeps what is between them. Is there a way to say keep [1] + [2] + however many there are? in other words just remove the [0] element?

              Thanks again for all your help :)

              1. Ah, I'm sorry I misunderstood - I thought you wanted to keep everything before the '_'.

                Here's another version that keeps everything after the first underscore:

                for(let oldTrackName of Array.from(sf.ui.proTools.selectedTrackNames))
                {
                    let newTrackName = oldTrackName.split('_').slice(1).join('_');
                    
                    sf.app.proTools.renameTrack({
                        oldName: oldTrackName,
                        newName: newTrackName,
                    });
                }
                
                Reply1 LikeSolution