Bulk file rename using regular expressions on Windows

This article describes a few methods of bulk renaming of files using Regular Expressions. The first two run in batch mode from the Windows console, the third is an interactive application.

Method 1: scripting sed using a bat file


@echo off
============ ========================= ===================== =================== =======
rem Copyright: Owen Duffy, 2015. All rights reserved.
rem A batch file for RE based file rename.
rem Uses sed, see http://gnuwin32.sourceforge.net/packages/sed.htm .

rem test for command extensions
if "~x0"=="%~x0" goto EXTERROR 
if "%%~x0"=="%~x0" goto EXTERROR
if not CmdExtVersion 2 goto EXTERROR

rem start
set SED=sed

rem deal with options
if not #%1==#-n goto getsedexpr
set noaction=y
echo Trial run only, rename disabled.

rem get the sedexpr string
set sedexpr=%1
if #%sedexpr%==# goto usage

for %%f in (%1 %2 %3 %4 %5 %6 %7 %8 %9) do call :dosed "%%f"
goto cleanup

rem extract old filename+ext
for %%f in (%1) do set oldname="%%~nf%%~xf"
rem extract rename arguments
for /f "usebackq tokens=*" %%n in (`echo %oldname% ^| %SED% -r -e %sedexpr%`) do call :doren %1 %%n %oldname%
exit /b

if %2==%3 exit /b
echo rename %1 %2
if not #%noaction%==#y rename %1 %2
exit /b

echo cannot rename %1 as %2% as %2 exists.
exit /b

echo Usage: srename [-n] sedexpr [filenames]
echo sedexpr is a sed s/// command using extended RE.
echo Example: srename s/\"(.*)\.(.*)\"/"\2.\1"/ *.*
goto cleanup

set sedexpr=
set SED=
set noaction=
goto eof

echo:This script requires command extensions v2 or later!>&2
verify error 2>NUL
goto eof



A Windows port of sed can be downloaded from http://gnuwin32.sourceforge.net/packages/sed.htm .

Method 2: scripting PERL using a bat file

The rename.pl script derived from Larry's Wall's original is now installed in the Debian distribution. If you have PERL installed on Windows, then you need a bat file wrapper to perform the file globbing that is done by the shell on unix but not done by the Windows console command processor.



Method 3: RegexRenamer

RegexRenamer is a dedicated GUI app that allows the user to interactively construct the regular expressions whilst viewing the effect on the file list window.

Screenshot - 08_09_2015 , 17_59_15

Above is an example screenshot.