Batch Scripting Windows

Renaming Files Using Batch

i often have to change the file names of music i have imported from CD this little script allows me to remove the track number from the file name.


@echo off
setlocal DisableDelayedExpansion
for /f "usebackq delims=" %%N in (`dir /s /b`) do (
  set var=%%~nxN
  setlocal EnableDelayedExpansion
  set "org=!var!"
  set "var=!var:0 =!"
  set "var=!var:1 =!"
  set "var=!var:2 =!"
  set "var=!var:3 =!"
  set "var=!var:4 =!"
  set "var=!var:5 =!"
  set "var=!var:6 =!"
  set "var=!var:7 =!"
  set "var=!var:8 =!"
  set "var=!var:9 =!"
  set "var=!var:0=!"
  set "var=!var:1=!"
  set "var=!var:2=!"
  set "var=!var:3=!"
  set "var=!var:4=!"
  set "var=!var:5=!"
  set "var=!var:6=!"
  set "var=!var:7=!"
  set "var=!var:8=!"
  set "var=!var:9=!"
  set "var=!var:.mp=.mp3!"

  if not "!var!"=="!org!" (
    if not exist "%%~dpN!var!" (
      echo "!org!" --^> "!var!"
      ren "!org!" "!var!"
    ) else (
      echo File "!var!" ^(from !org!^) already exists.
    )
  )
  endlocal
)

Leave a Reply

Your email address will not be published. Required fields are marked *