[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

Replacing and trimming characters in filenames using a batch file (windoze)

Good morning,

I'm trying to rename some mp3 files and would like to achieve two goals:

1.  I want to replace underscores with spaces
2.  I want to strip off leading track numbers

For example I would like to take the following filename:

"01 - This_Is_The_Title.mp3"

And convert it to:

"01 - This Is The Title.mp3"

And then to:

"This Is The Title.mp3"

The biggest problem is with stripping numbers off because they are not consistent.  For example, I have files like the following:

01This_Is_The_Title.mp3

and

01 - This_Is_The_Title.mp3

I could group similarly named files in directories without too much trouble to solve this and then hand sanitize the rest.

What do you think?
0
ttist25
Asked:
ttist25
  • 2
  • 2
  • 2
2 Solutions
 
Steve KnightIT ConsultancyCommented:
You want to do something like this.  Works on the current directory

@echo off
REM save this as renfiles.cmd or something
for /f "tokens=*" %%a in ('dir /b /a-d') do call :process "%%a"
goto :eof
:process
set oldname=%~1
REM Replace and _ with space
set strippedname=%oldname:_= %
REM set newname to remove first 2 characters
set newname=%strippedname:~2%
echo rename "%oldname%" "%newname%"

In this case it strips specifically the first two characters -- if you can organise them like this.  Remove the echo command before the rename to actually have i do it once it looks OK (at the moment it just shows on the screen).

We could get more complex testing if each char is a number and removing it etc. but maybe this is enough for now?

Steve
0
 
ttist25Author Commented:
Hey Dragon,

Thanks for your response.  I think we're almost there.  

Is it possible to separate these into two batch files?  1 that removes the _ and one that strips the first characters?  

I think I would better be able to understand and modify the batch file that way.  

Also, it's renaming the batch file itself.  How would we call to a directory other than root?

Thanks again for your help!
0
 
QlemoC++ DeveloperCommented:
No need for for /F,  a simple for is enough. Also separated in parts, and manage the different formats you gave:

@echo off
setlocal EnableDelayedExpansion
REM underscore to space
for %%F in (C:\MP3s\*_*.mp3) do (
  set old=%%~F
  ren "!old!" "!old:_= !"
)
 
REM remove track with minus
for %%F in (C:\MP3s\* - *.mp3) do (
  set old=%%~F
  ren "!old!" "!old:*- =!"
)
 
REM remove track without minus
for %%F in (C:\MP3s\0*.mp3 C:\MP3s\1*.mp3 C:\MP3s\2*.mp3) do (
  set old=%%~F
  ren "!old!" "!old:~2!"
)

Open in new window

0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
ttist25Author Commented:
Thanks for your response Qlemo it is greatly appreciated.

I get an error "The syntax of the command is incorrect" when I run this.

I'm using Vista if that makes any difference.
0
 
QlemoC++ DeveloperCommented:
Remove the first line (@echo off), and try again. You should see the command causing that error message.

Above line 10 needs to be corrected, I forgot to quote because of the spaces:
for %%F in ("C:\MP3s\* - *.mp3") do (

0
 
Steve KnightIT ConsultancyCommented:
Hi guys,  sorry for abondoing.  I offered my suggestions originally then wasn't online for the rest of the day then away for a weekend in Wales where the mobile internet signal is still steam driven 2G GPRS (which is better than no signal at all where I went last time I suppose...)

Anyway please let us know if you still need some more help and will be answer.

Steve
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now