Renaming a Set of Files to 001, 002,

Renaming a set of files to 001, 002,

If I understand right, you have e.g. image_001.jpg, image_003.jpg, image_005.jpg, and you want to rename to image_001.jpg, image_002.jpg, image_003.jpg.

EDIT: This is modified to put the temp file in the current directory. As Stephan202 noted, this can make a significant difference if temp is on a different filesystem. To avoid hitting the temp file in the loop, it now goes through image*

i=1; temp=$(mktemp -p .); for file in image*
do
mv "$file" $temp;
mv $temp $(printf "image_%0.3d.jpg" $i)
i=$((i + 1))
done

Renaming a set of files to 001, 002, … php


$files = glob("*.jpg");
$num=count($files);
$i=1;
foreach ( $files as $filename) {
$n=str_pad($i, $num ,"0",STR_PAD_LEFT);
$newfile = $n.".jpg";
rename($filename,$newfile);
$i+=1;
}

Batch rename to numeric names with leading zeroes

The following would be my suggestion:

Please note however, that the example does not cater for any existing filenames in the directory which already match fb<num><num><num>.EPL, so those will also be renamed with possible new numbers too.

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "SourceDir=."
Set "FileGlob=*.EPL"
Set "Prefix=fb"
Set "FileNum=1000"
For /F Delims^= %%G In ('(Set PATHEXT^=^) ^& "%SystemRoot%\System32\where.exe"
/F "%SourceDir%":"%FileGlob%" 2^> NUL') Do (Set /A FileNum += 1
SetLocal EnableDelayedExpansion & Ren %%G "%Prefix%!FileNum:~-3!%%~xG"
EndLocal)
  • Insert your required source directory between the = and closing " on line 3, (I've used . for the current directory, as per your example, but you can use an absolute or other relative path as needed).
  • Insert your required file glob prefix string between the = and closing " on line 4, (I've used *.EPL to match your example).
  • Insert your required file name prefix string between the = and closing " on line 5, (I've used fb to match your example).

I've used 1000 as your starting file number, because Set /A uses integers, and sees multiple concurrent 0's as just 0. Adding the 1 will prevent that, and will be omitted, when the varible expansion, uses just the last three characters. I also adjusted it so that the file numbering begins with fb001.EPL, to match your question parameters, (your code was beginning the sequence with fb000.EPL).

I've enabled delayed expansion within the loop, because it is required, when both modifying and using a variable within the same parenthesized code block. It shouldn't normally be enabled for the entire script, because filenames and strings containing ! characters can be affected, (those characters will be omitted).

I've also gone 'belt and braces' with the file selection, by using where.exe. This utility is not affected by Windows using 8.3 naming, which, whilst it may not be an issue with your provided example, will match exactly extensions .EPL, not those which begin with .EPL. Standard For loops, (which you used), and the more commonly used Dir command are both affected by 8.3 naming.

For additional robustness, I've used the full path to where.exe with the system environment variable %SystemRoot%, to prevent reliance on the %Path% variable which is often broken, by its incorrect or accidental end user modification.


As a direct resolution to your own code without modification to the majority of it:

setlocal ENABLEDELAYEDEXPANSION
set /a fileNum = 1
set fileNum=1000%fileNum%

for %%f in (*.EPL) do (
ren "%%f" fb!fileNum:~-3!%%~xf
set /a fileNum += 1
)

I must add however, that's because your example code is using a standard for loop, you may have issues. The problem is that a your loop will pass the first file through to the do portion whilst it is still parsing the others under the all encompassing * glob. This means that fb001.EPL will be put back into the list for parsing, and could be picked up again for processing another time, this could continue for other files too!

Rename multiple files with a sequence of numbers in cmd and batch

This should work for you.

set /a Index=1

setlocal enabledelayedexpansion

for /r %%i in (*.txt) do (
rename "%%i" "!Index!.txt"
set /a Index+=1
)

Create a batch file with above code and run it in folder where your .txt files are available.

If you want to append "0" to make it 2 digits. you can try adding if else statement as below.

set /a Index=1

setlocal enabledelayedexpansion

for /r %%i in (*.txt) do (
rem if number is less than 10, append 9 to file name
if !Index! lss 10 (
rename "%%i" 0"!Index!.txt"
) else (
rename "%%i" "!Index!.txt"
)

set /a Index+=1
)

Multiple file renaming based on regex pattern + renaming within files based on new file names

This solved it for me:

  • File renaming e.g. as described here: https://superuser.com/questions/16007/how-can-i-mass-rename-files
  • Batch replacement with this VS Code plugin: https://marketplace.visualstudio.com/items?itemName=angelomollame.batch-replacer
  • since I had the said table (old vs. new name) prepared, a simple regex replacement of the table entries did the trick to meet the prerequisites for the plugin, i. e. the old names were replaced by replace "old_file_name" and the new names by with "new_file_name"; then, just copy & paste everything for this plugin as described there and all is replaced

Renaming multiple files with correct numerical order in Powershell

Use Sort-Object to sort them according to the numerical value in the name before you start renaming:

$i = 1
Get-ChildItem *.mp3 |Sort-Object {[int]($_.BaseName -replace '\D')} | Rename-Item ...

Although you may run into unexpected errors because $i++ does not have the effect you intend - the ++ operation creates a new local copy of $i without ever incrementing $i in the parent scope.

We can solve that by explicitly passing a reference to $i:

$i = 1
Get-ChildItem *.mp3 |Sort-Object {[int]($_.BaseName -replace '\D')} | Rename-Item -NewName {'{0:D4}.mp3' -f ([ref]$i).Value++}

Updating Sub folder and File renaming batch script


@ECHO OFF
SETLOCAL
rem The following setting for the source directory is a name which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"

PUSHD "%sourcedir%"

for /d %%B in ("*") do (
for %%E in ("%%B\*.jpg") do (
FOR /f "tokens=1,2delims=()" %%q IN ('dir /b /a-d "%%~dpnxB\(*)(*).txt" 2^>nul') DO (
rem files matching "(*)(*).txt only
REN "%%~dpnxB\(%%q)(%%r).txt" "(%%q) %%~nE (%%r).txt"
)
FOR /f "tokens=1*delims=()" %%q IN ('dir /b /a-d "%%~dpnxB\(*).txt" 2^>nul') DO IF /i "%%r" equ ".txt" (
rem files matching "(*).txt only
REN "%%~dpnxB\(%%q).txt" "(%%q) %%~nE.txt"
)
)
)

popd

GOTO :EOF

Caution : This batch is armed. It will rename files. Always verify against a test directory before applying to real data.

The outer loop on %%B gets the directory names. No surprise there.

The next loop on %%E gets the .jpg names. No surprise there.

The first loop on %%q looks at the .txt files that fit the pattern (*)(*).txt and re-assembles the parts as required for the rename.

The second loop on %%q looks at the .txt files that fit the pattern (*).txt which may include the just-renamed files matching (*)(*).txt now (*) jpgfilename (*).txt, so this time, %%r must be .txt to exclude these newly-renamed files.

I'll repeat

Caution : This batch is armed. It will rename files. Always verify against a test directory before applying to real data.



Related Topics



Leave a reply



Submit