Page 1 of 1

rename leading with random number. then re-randomize later.

Posted: 08 Feb 2011 12:49
by Tundrabob
So i have this so far.
SETLOCAL EnableDelayedExpansion
for /f "tokens=* delims=" %%a in ('dir /b *.mp3') do ren "!random:~-2! %%a"

it seams to work well. but, I'd like to be able to re-run it and have it re-randomized the files already randomized and include any new files I've put into the folder.

Re: rename leading with random number. then re-randomize lat

Posted: 08 Feb 2011 13:14
by Tundrabob
well i posted cause i didn't think i'd be able to figure it out but I got this so far and i think its solid. can anyone see any problems?

@echo off
SETLOCAL EnableDelayedExpansion
for /f "tokens=* delims=" %%a in ('dir /b *.mp3') do (
set string=%%a
set check1=!string:~0,2!
if !check1! lss 100 (ren "%%a" "!random:~-2! !string:~3,200!") else (ren "%%a" "!random:~-2! %%a")
)

Re: rename leading with random number. then re-randomize lat

Posted: 08 Feb 2011 16:37
by aGerman
You could try this:

Code: Select all

@echo off &setlocal

for /f "delims=" %%a in ('dir /b *.mp3') do (
  set "string=%%~a"
  call :proc
)

pause
goto :eof

:proc
SetLocal EnableDelayedExpansion
set "name=!string!"
echo(!string!|findstr /rbc:"[0-9][0-9] ." >nul &&(
  set "string=!string:~3!"
)
:loop
set "newname=%random:~-2% !string!"
if exist "!newname!" goto loop
ren "!name!" "!newname!"
EndLocal
goto :eof


Note that the code will end up in an infinite loop if you've got more than a hundred .mp3 files in the folder.

Regards
aGerman