Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I am trying to rename a complete directory of
files to remove the first three characters. All
the filenames have spaces throughout them
and the first several characters look like this:"01 Filename hour 1.mp3"
"02 Filename hour 2.mp3"
"03 Filename hour 3.mp3"I want to remove the initial numbers (ex: "01 ")
This is the code I have so far:
@echo off setlocal cd "c:\iTunes\Podcasts\R24\" for %%a in (*.mp3) do call :process "%%a" goto :eof :process set fileName=%1 ren "%fileName%" "%fileName:~4%"It works great with one exception: the first file
processed is not renamed correctly.Here is what was echoed to the command
prompt when the first file was processed:ren "01 Fri, August 21st, 2009 Hour 1.mp3"
"Fri, August 21st, 2009 Hour 1.mp3"The actual name the file ended up with was:
", August 21st, 2009 Hour 1.mp3"
I tried to remove the weekday, comma, and
space to give all files names like
"August 21st, 2009 Hour 1.mp3" instead
(changed the 4 to a 9 in the batch script). In
that case the first file processed ended up with
the following name:"1st, 2009 Hour 1.mp3"
I also tried to remove the August 21st Hour 1
file to see if that particular file was the
problem. The first file continued to be the only
one named incorrectly.
If anyone can help I'd appreciate it. I'm also
curious why using a 1 instead of a 4 in the
rename command (such as %fileName:~1%)
does not remove a single character from any
filename.Thanks.

There seems to be some sort of race condition when using the standard version of FOR. The work around is to use its text parsing feature and DIR.
for /f "delims=" %%a in ('dir /b *.mp3') do call :process "%%a"

Thanks Razor.
"delims=" ... does that effectively turn parsing off by setting
the delimiter to nothing? I've seen that syntax a few times
now but I'm not clear on what it's doing.Regards,
techphets

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |