Auto-Increment File Numbers in BRU Without Overwriting

Bulk Rename Utility How-To's

Auto-Increment File Numbers in BRU Without Overwriting

Postby Jakov » Fri Jan 30, 2026 4:49 am

Hi,
Please how to add sequence number to selected files, but if the number already present, start with subsequent number automatically?
Is this possible with BRU?
Intelligent sequential numbering for selected files?
Thanks
Jakov
 
Posts: 63
Joined: Sat May 23, 2020 1:29 am

Re: Auto-Increment File Numbers in BRU Without Overwriting

Postby Admin » Fri Jan 30, 2026 5:36 am

Hi, what do you mean by already present? Could you give example? Thanks
Admin
Site Admin
 
Posts: 3141
Joined: Tue Mar 08, 2005 8:39 pm

Re: Auto-Increment File Numbers in BRU Without Overwriting

Postby Jakov » Fri Jan 30, 2026 7:53 am

Thanks for reply,
Sometimes I organize my files with numbering by putting them in a folder, then a new files should be added to the same folder.
suppose I have 4 files numbered with with this format (Number. FileName) in a folder, then I add a new 5 files (not numbered), now I want to number the new files, so without putting the start number in BRU to be 5, I want the BRU to look for the max number in current folder ( in current case =4), so it start numbering with 5, without putting 5 in start field in BRU. In other words, the start field may be set to "Auto". In think it is complicated, but may be done with BRU or an option in the future release.
Thanks
Jakov
 
Posts: 63
Joined: Sat May 23, 2020 1:29 am

Re: Auto-Increment File Numbers in BRU Without Overwriting

Postby Admin » Fri Jan 30, 2026 8:46 am

Yes - not directly via the Numbering (10) GUI option, but you can do it with the Javascript renaming panel. The script can scan the current folder for existing numbered files, find the highest number, then start numbering the selected (or all) new files from max+1 automatically.

Below is a ready-to-use JavaScript you can paste into the Javascript Renaming panel (Special (14) _ Javascript Renaming). It:
- Scans the parent folder for filenames that start with "N. " or "N." where N is digits (adjust the regex to match your exact existing format).
- Finds the maximum N.
- Produces new names for the current items using max + counter.
- Honors padding and separator settings you can change at the top of the script.

Example script (edit the settings section as needed):

Code: Select all
// --- SETTINGS: adjust to your naming style ---
var numberRegex = /^(\d+)[\.\s-_]+/;   // matches "123. ", "123.", "123 -", "123_" etc.
var padWidth = 3;                      // pad to 3 digits => 001, 002 ... set 0 for no padding
var sepAfterNumber = ". ";             // what goes after the number in the new name
var includeExtension = true;           // keep extension (true) or change/remove (false)
var onlyIfNotNumberedAlready = true;   // skip files already starting with a number
// ------------------------------------------------

function pad(n, width) {
    if (!width || width <= 0) return String(n);
    return padNum(n, width); // built-in helper in BRU JS environment
}

// find max existing leading number in the parent folder
var files = listFilesInFolder("", false, false); // files in this folder only
var maxNum = 0;
for (var i = 0; i < files.length; ++i) {
    var f = files[i];
    var m = f.match(numberRegex);
    if (m) {
        var v = parseInt(m[1], 10);
        if (!isNaN(v) && v > maxNum) maxNum = v;
    }
}

// counter is a built-in integer starting at 1 for each rename iteration
// We will set newName for each file using (maxNum + counter)
if (onlyIfNotNumberedAlready && name.match(numberRegex)) {
    // item already numbered - keep it unchanged
    newName = name;
} else {
    var n = maxNum + counter;                 // counter starts at 1
    var fmt = pad(n, padWidth) + sepAfterNumber + name;
    newName = fmt;
    if (!includeExtension) {
        // remove extension from newName if you want names without ext in JS (rare)
        // note: normally newName should NOT include the extension; BRU will add ext.
        // If you want to change extension use newExt variable instead.
        // newName = removeExt(fmt);
    }
}


How to use it (step-by-step):
1. In BRU, select the folder containing both the 1..4 files and the new unnumbered files (or just select the new files).
2. Disable the Numbering (10) GUI panel (to avoid double-numbering).
3. Open Special (14) _ Javascript Renaming and paste the script above.
4. Adjust the regex and settings to match your current filename pattern (e.g., if your files are "4. FileName" you can keep the default regex; if they are "4 - FileName" adjust numberRegex).
5. Preview the New Name column. If OK, click Rename.

Notes & tips:
- The script scans only the same folder (listFilesInFolder("",...)). If you need to include subfolders change the call (or adapt the script).
- If your existing numbering format is different (e.g., "Number) FileName" or "Number - FileName"), tweak numberRegex to match.
- padNum is available in BRU JS and used above for padding; you can also implement custom padding if desired.
- Test on a copy or use Preview first - creating/moving files cannot always be undone.
- If you want BRU's built-in Numbering (10) GUI to start automatically at max+1, that's not supported currently; the JavaScript route is the recommended workaround.

If you paste your actual filename samples (a few real names before and after), I can adapt the script's regex + settings precisely for your case.
Admin
Site Admin
 
Posts: 3141
Joined: Tue Mar 08, 2005 8:39 pm

Re: Auto-Increment File Numbers in BRU Without Overwriting

Postby Jakov » Fri Jan 30, 2026 7:48 pm

Thanks for your code
I tried it on my PC (BRU v4) and it worked well, but when I tried it on another PC (v3), it failed and showed this message
Code: Select all
Javascript Error
ReferenceError: listFilesInFolder is not defined in : var files = listFilesInFolder("", false, false); // files in this folder only [Line:15 Start:12 End:13]

Is BRU version affect?
Really, I was using a batch script to do this function before your code, here is
Code: Select all
# Get the files dropped onto the script
param([string[]]$filePaths)

# If no files selected, exit
if ($filePaths.Count -eq 0) { return }

# Get the folder where the files are located
$firstFile = Get-Item -LiteralPath $filePaths[0]
$dir = $firstFile.DirectoryName

# --- 1. Find the highest sequence number in the folder ---
$max = 0
Get-ChildItem -Path $dir -File | ForEach-Object {
    # Look for files that start with "Number. " (e.g., "1. Video.mp4")
    # The regex '^(\d+)\. ' means: Start -> Digits -> Dot -> Space
    if ($_.Name -match '^(\d+)\. ') {
        $num = [int]$matches[1]
        if ($num -gt $max) { $max = $num }
    }
}

# Start counting from the next number
$counter = $max + 1

# --- 2. Rename the new files ---
foreach ($path in $filePaths) {
    $file = Get-Item -LiteralPath $path
   
    # Check if file is already numbered (e.g. "5. Video.mp4") to avoid "6. 5. Video.mp4"
    if ($file.Name -match "^\d+\. ") {
        Write-Host "Skipping $($file.Name) (Already numbered)"
        continue
    }

    # NEW FORMAT: "Number. OriginalName"
    # Example: "1. test.mp4"
    $newName = "{0}. {1}" -f $counter, $file.Name
   
    try {
        Rename-Item -LiteralPath $file.FullName -NewName $newName -ErrorAction Stop
        Write-Host "Renamed to: $newName"
    }
    catch {
        Write-Host "Error renaming $($file.Name): $($_.Exception.Message)"
    }
    $counter++
}

Thanks
Jakov
 
Posts: 63
Joined: Sat May 23, 2020 1:29 am

Re: Auto-Increment File Numbers in BRU Without Overwriting

Postby Admin » Fri Jan 30, 2026 11:23 pm

Hi, yes, you need the latest version for that js to work.
Admin
Site Admin
 
Posts: 3141
Joined: Tue Mar 08, 2005 8:39 pm


Return to How-To