Batch Rename Files

windows batch file rename


@echo off
pushd "pathToYourFolder" || exit /b
for /f "eol=: delims=" %%F in ('dir /b /a-d *_*.jpg') do (
for /f "tokens=1* eol=_ delims=_" %%A in ("%%~nF") do ren "%%F" "%%~nB_%%A%%~xF"
)
popd

Note: The name is split at the first occurrence of _. If a file is named "part1_part2_part3.jpg", then it will be renamed to "part2_part3_part1.jpg"

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

How to batch rename files in PowerShell starting with padded first numbers

You can use -match and by doing so capture the number behind the L in $matches[1], which can be formatted like you want:

Get-ChildItem -Path 'D:\Test' -Filter '*.mp3' -File | 
# apply better filename filter and capture the number after the 'L' in $matches[1]
Where-Object { $_.BaseName -match '^\w+_S\dL(\d+)_\d+_\w+' } |
Rename-Item -NewName { ('{0:D2}-lesson{1}' -f [int]$matches[1], $_.Extension) }

How to batch rename files with two extensions?

The * wildcard has a fair bit of compatibility handling related to DOS 8.3 names and whatnot. How did copying and renaming with wildcards work in MS-DOS?

One way to get around it is to use full names without wildcards in the rename operation:

@echo off

REM test file:
echo.>> "file1.foo.bar"

REM remove foo extension:
for %%A in (*.foo.bar) do for /F "eol=? delims=" %%B in ("%%~dpnA") do @ren "%%~fA" "%%~nB%%~xA"

(Change %% to % if not in a batch file)

Batch: Renaming files numeric (Count up every time)

It will be simpler to create an empty folder, move the files into the new folder whilst renaming them, and then move them back to the old one. That way, you won't have any possible issues with renaming existing files, or iterating through files you've already renamed.

My original solution:

@echo off
setlocal enabledelayedexpansion
set folder=C:\path\to\existing\folder
md %folder%.TEMP
set /a n = 0
for %%f in ("%folder%\*") do (
set /a n += 1
move "%%f" "%newfolder%\!n!%%~xf"
)
move "%folder%.TEMP\*" "%folder%"
rd %folder%.TEMP

Updated with @Compo's comments, numbering the files with leading zeros in order of time created (change /TC to /TW to use last modification date instead):

@echo off
setlocal enabledelayedexpansion
set folder=C:\path\to\existing\folder
md %folder%.TEMP
set /a n = 0
for /f "delims=" %%f in ('dir/b/a-d-s-l/od/tc "%folder%"') do (
set /a n += 1
set zn=0000000!n!
set zn=!zn:~-8!
move "%%f" "%newfolder%\!zn!%%~xf"
)
move "%folder%.TEMP\*" "%folder%"
rd %folder%.TEMP


Related Topics



Leave a reply



Submit