justinmassiot
5/15/2017 - 12:11 PM

SVN replace file extensions

Replace all file extensions with an other one while keeping files in sync with SVN.

@echo off

REM directory in which to work, without a trailing (back)slash
set workdir=%1
REM file extension to be replaced, without the "." (dot)
set filext1=%2
REM new file extension, without the "." (dot)
set filext2=%3
REM wether to erase existing files or not (0 or 1)
set force_rename=%4

REM SVN rename files of the current directory that have the extension "filext1" into files with extension "filext2"
REM See http://www.robvanderwoude.com/ntfor.php for some help
REM %%~na is the file name without extension and %%~xa is the file extension
for /f %%a in ('dir %workdir% /b') do (
	if /i %%~xa==.%filext1% (
		if not exist %workdir%\\"%%~na.%filext2%" (
			svn mv %workdir%\\"%%a" %workdir%\\"%%~na.%filext2%"
		) else (
			if %force_rename%==1 (
				svn rm %workdir%\\"%%~na.%filext2%" && svn mv %workdir%\\"%%a" %workdir%\\"%%~na.%filext2%"
			) else (
				echo The file %%~na.%filext2% already exists: %%a has not been renamed. Use the "force" parameter to overwrite.
			)
		)
	)
)