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
// --- 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);
}
}
Javascript Error
ReferenceError: listFilesInFolder is not defined in : var files = listFilesInFolder("", false, false); // files in this folder only [Line:15 Start:12 End:13]# 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++
}