Created
March 21, 2014 04:51
tren - text edit rename batch file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
if "%1" == "/?" ( | |
goto usage | |
) | |
set myname=%~nx0 | |
REM "Output the name of the temporary file" | |
set rfpath=\rename.tmp | |
set bfpath=\basename.tmp | |
if "%1" == "" ( | |
set inputpath="%~dp0%" | |
) else ( | |
set inputpath="%~f1%" | |
) | |
if "%2" == "" ( | |
set ext=\* | |
) else ( | |
set ext="\%2%" | |
) | |
if not exist %inputpath% ( | |
echo Directory not found. | |
goto end | |
) | |
type nul > %TEMP%%rfpath% | |
type nul > %TEMP%%bfpath% | |
if "%ext:~-1%" == "*" ( | |
for /D %%f in (%inputpath%%ext%) do ( | |
echo %%~nf%%~xf>>%TEMP%%rfpath% | |
echo %%~nf%%~xf>>%TEMP%%bfpath% | |
) | |
) | |
for %%f in (%inputpath%%ext%) do ( | |
if NOT "%myname%" == "%%~nf%%~xf" ( | |
echo %%~nf%%~xf>>%TEMP%%rfpath% | |
echo %%~nf%%~xf>>%TEMP%%bfpath% | |
) | |
) | |
REM "If you want to change the editor, edit this line" | |
notepad %TEMP%%rfpath% | |
set /a skp=0 | |
for /f "usebackq delims=" %%f in ("%TEMP%%rfpath%") do ( | |
set toname=%%f | |
call :sub | |
set /a skp+=1 | |
) | |
del %TEMP%%rfpath% | |
del %TEMP%%bfpath% | |
goto end | |
:usage | |
echo usage : tren [path] [filename] | |
goto end | |
:sub | |
if "%skp%" == "0" ( | |
for /f "usebackq tokens=* delims=" %%i in ("%TEMP%%bfpath%") do ( | |
if "%%i" == "%toname%" ( | |
goto end | |
) | |
echo from "%%i" to "%toname%" | |
ren "%inputpath%\%%i" "%toname%" | |
goto end | |
) | |
) else ( | |
for /f "usebackq skip=%skp% tokens=* delims=" %%i in ("%TEMP%%bfpath%") do ( | |
if "%%i" == "%toname%" ( | |
goto end | |
) | |
echo from "%%i" to "%toname%" | |
ren "%inputpath%\%%i" "%toname%" | |
goto end | |
) | |
) | |
goto end | |
:end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment