Rename files with ampersand in the name


Dec 7, 2015
I'm trying to rename a bunch of mp3 files. The files are in the format XXX.Artist - Title.mp3. XXX is a sequential number and I'm trying simply to randomly reorder the files. I exchange each file with another one with a random order number. This is the code:

set NumMax=369
for /L %f in (1, 1, %NumMax) (
set R=%@random[1, %NumMax]
set N1=%@right[3,000%f]
set N2=%@right[3,000%R]
if %N1 NE %N2 (
for %g in (%N1*.mp3) (
for %h in (%N2*.mp3) (
set L1=%@eval[%@len[%g]-4]
set L2=%@eval[%@len[%h]-4]
set NN1=%N2.%@right[%L1,%g]
set NN2=%N1.%@right[%L2,%h]
echo ren "%g" "%NN1"
echo ren "%h" "%NN2"

The problem is, some filenames contain "&", for example "174.Katrina & The Waves - Walking on sunshine.mp3", and those name break the functionality. Without ampersands, it works ok:

ren "173.Kansas - Dust in the Wind.mp3" "053.Kansas - Dust in the Wind.mp3"
ren "053.Bruce Springsteen - The River.mp3" "173.Bruce Springsteen - The River.mp3"

but with ampersands:
TCC: C:\Selection\RandomRen.btm [18] Comando desconocido "The"
ren "174.Katrina & The Waves - Walking on sunshine.mp3" "050.Katrina"
ren "050.Bruce Springsteen - Hungry Heart.mp3" "174.Bruce Springsteen - Hungry Heart.mp3"

the problem is, the line:
set NN1=%N2.%@right[%L1,%g]
gets confused with the "&". I've tried using SETDOS /X-4 to disable nested variable expansion but there is no difference
Similar threads
Thread starter Title Forum Replies Date
C How to? rename files with spaces Support 8
Phileosophos WAD Can't rename files in subfolders Support 1
C How to rename files with "(" Support 14
A Rename Files in Chronological Order? Support 5
Joe Caverly Rename file that begins with Emoji Support 7
C Documentation Help Nit. REN / RENAME Support 0
R How to? Replace in Rename Support 9
M How to? File rename problem... Support 11
D Folders pane display problem after rename Support 0
fpefpe How to? Rename runs tree Support 10
JohnQSmith Slow network directory rename Support 8
JohnQSmith Folder pane right-click rename Support 2
samintz Regex Rename Support 2
nikbackm Rename TC tab from command line Support 0
MickeyF TCC crashing when copying multiple files (now resolved) Support 6
fpefpe How to? shortcuts and batch files Support 4
C v28 key files Support 2
J ffind does not find files Support 4
C duplicate files in multiple plugin sites Support 9
Dmitry L. Kobyakov Fixed The SETARRAY /R command works incorrectly with cyrillic files Support 2
S How to? Deinstalling TC/reinstating execution of bat files by cmd in Win7 Support 31
vefatica FFIND, temp files? ... not deleted? Support 0
R FUNCTION to count NUMBER of files matching a pattern and specific length/not length Support 4
vefatica Tmp files from in-process pipes not deleted Support 6
vefatica Finding files with alternate data streams? Support 4
rps Multi-column DIR /v not displaying all files. Support 5
D How to skip files in a fileset? Support 6
vefatica PIPEVIEW leaves TMP files Support 18
K "copy /z /w" command not deleting read-only files in destination Support 10
Jesse Heines "Taken" date for JPG and PNG files Support 4
O I cannot use links to .btm files Support 5
R How to? Append files in multiple subfolders using copy? Support 8
Joe Caverly "Functions" in cmd.exe batch files Support 6
S Batch files Calling Batch Files Support 2
vefatica Find Files/Text dialog: does it work? Support 0
Joe Caverly UNZIPping older .ZIP files Support 3
C Documentation default location for LOG files Support 21
P exit /b in batch files Support 7
P Labels in batch files: TCC vs CMD.exe Support 9
M ANSI is used to show files instead of OEM in version 17 to 19 ! Support 5
C 7Z files on forum? Support 0
x13 Problem listing repository files using DIR http(s)://... Support 8
B Can copy command concatenate multiple mp3 files Support 2
redwdc Copying files with spaces, commas, etc. Support 5
J New lexer for TCC batch files. Support 0
M How to? want to download a file from a website enumarate the links and download the files Support 2
keithg1964 TCC RT Install Files Support 1
P Fixed Rexx .cmd files no longer work in TCC 20 Support 3
thorntonpg Handling files with embedded spaces Support 1
WavSlave Fixed Unable to sort files by description in TCC v19 Support 2

Similar threads