FalconFour.com - Projects - Blog / Personal - Code -- Comments System -- Navigation Structure -- SFV checker -- Stats Sig/Av -- 32-character file rename batch file -- Simple Message Storage |
So late one night (4am to be exact), I was putting music on my phone's MicroSD card, and I realized, hey, I need to rename these files to under 32 characters (total) in order for the phone to play them! Hmm... (stupid restriction, I know)
I hadn't made any real batch files before, so this was a shot in the dark. If I could find a way to extract a substring from a variable (i.e. the filename, passed from FOR), I could build a little structure around that! So that I did.
In my effort to make it bulletproof and postable online, I added a small menu system and had it check for filenames that don't require a quote (if it doesn't get the filename in quotes, the extension-length calculation will be off). The extensions also have to be exactly 3 characters long, too (mp3, wma, wav, etc). Anyway, here's the code. Or codes, rather...
rename.cmd
@echo off
echo This script will rename ALL FILES in this folder to 32 characters or less.
echo.
echo Choose a mode:
echo a) Display changes to filenames
echo b) Permanently rename files (after previewing with A!)
echo c) Exit
echo.
set /p _choice=Select a mode and press enter:
if %_choice%==a set _choice=D
if %_choice%==b set _choice=R
if %_choice%==c break
for %%x in (*.*) do (
call rename2 "%%x"
)
pause
|
rename2.cmd
@echo off
if not defined %%1 (
echo Do not run this file directly. Run rename.cmd.
pause
goto :eof
)
REM import the variable...
set _name=%1
REM strip the quotes if they're there...
set _name=%_name:"=%
REM grab the full base name sans extension...
set _base=%_name:~0,-4%
set _ext=%_name:~-4%
REM format the output.
set _output=%_base:~0,28%%_ext%
REM here, we re-add the quotes.
echo "%_name%" - "%_output%"
if %_choice%==R ren "%_name%" "%_output%"
|
Tadaa. Double click rename.cmd and it'll present you with a menu of what to do.
Thanks to this little site for the incredible explanation of NT(/2K/XP)'s command line.
|