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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Tundrabob
Posts: 4
Joined: 30 Jan 2011 11:21

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

#1 Post by Tundrabob » 08 Feb 2011 12:49

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.

Tundrabob
Posts: 4
Joined: 30 Jan 2011 11:21

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

#2 Post by Tundrabob » 08 Feb 2011 13:14

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")
)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#3 Post by aGerman » 08 Feb 2011 16:37

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

Post Reply