0

I have multiple mp3 file like this:

Track [number of track].mp3 eg. Track 1.mp3 - Track 11.mp3

I want to rename them to:

T00[number of track] eg. T001.mp3 - T011.mp3

I have tried to rename 1-9 tracks with this:

ren "Track ?.mp3" "T00   ?.mp3"

but I got space in filename (T00 1.mp3)

Update: I'm trying to write the code to remove space this is my code

@echo off
setlocal enabledelayedexpansion

for /r %%G in ("T00   ?.mp3") do (

pause

echo -----------------------In loop----------------------------
echo Working with "%%G"

pause

set str=%%G
echo str is = "%str%"

pause

set str=%str:   =0% //replace "   " with 0 
echo 2str is = "%str%"
pause

ren "%%~G" "%str%"

)

Message I got is:

-----------------------In loop----------------------------

working with "C:\test\T00   1.mp3"

str = ""

2str = ""

A duplicate file name exists, or the file
cannot be found.


.
.
.

I don't know what I wrong this is my fist time I using Batch Script

5
  • This may help dostips.com/DtTipsStringManipulation.php#Snippets.Replace You should be able to replace the whitespace with nothing
    – Dave
    Mar 7, 2014 at 11:02
  • @DaveRook I don't know how to write bat file would you write it for me please?
    – Zen3515
    Mar 7, 2014 at 12:42
  • 3
    No, I'm not here to do your work! I've given you a link which shows how to do string manipulation which should help
    – Dave
    Mar 7, 2014 at 12:43
  • I don’t know why there was a downvote. The question is interesting and surely a common problem. But I have no idea how to solve it in windows. In Linux no problem.
    – erik
    Mar 7, 2014 at 14:36
  • There are plenty of resources for learning how to write “BAT” files on the Internet (and I suspect some of them are useful); you can find thousands of examples just here on Super User. I’ll give you a hint: type help setlocal (or setlocal /?) and try to understand what setlocal enabledelayedexpansion means. Then figure out whether it makes sense to use !str! instead of %str%. A more general bit of advice is that you shouldn’t let a script do anything until you have the logic debugged, so you know it’s going to do the right thing. For example, replace ren … with echo ren …. Nov 12, 2014 at 18:24

3 Answers 3

2

Using PowerShell with Regex replace and Format string

$strPath = "C:\my\folder\*.mp3"

Get-Childitem $strPath | ForEach-Object { 
    $onlyNumbers = $_.Basename -replace ".*\D+",""  
    $WithZeros = "{0:D3}" -f [int]$onlyNumbers
    $fileName = "T$WithZeros.mp3"
    Rename-Item $_ $fileName
    }

The key parts are -replace "^\D+","" which searches all non-digits from the beginning of the filename (but without the extension) and removes them. Second, the script appends as many zeros as needed with "{0:D3}" -f. Rest is easy stuff.

Read both linked sources if you want to know more.

Old name          New name
Track 1.mp3       T001.mp3
Track 11.mp3      T011.mp3
Track 111.mp3     T111.mp3

Edit: Fixed small RegEx mistake. Before 99 ballons 01.mp3 becomes T9901.mp3.
Now it's 01 » 001 » T001.mp3

0

If you want to use Linux or Mac OSX to solve your problem, you could do:

count=1
for i in $(ls -v); do
  number=$(printf "%05d" $count)
  ((count++))
  mv $i T00$number.mp3
done

I have tested it on 25 files that I have previously created with this command

for ((i=1; i<=25; i++)); do touch "Track $i.mp3"; done
0

If this helps Total Commander (windows) has great multi-rename tool (select desired files then Ctrl+M).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .