Renaming a directory of files with spaces

August 23, 2009 at 09:14:13
Specs: Windows XP
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.


See More: Renaming a directory of files with spaces

Report •


#1
August 23, 2009 at 16:04:44
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"


Report •

#2
August 23, 2009 at 18:47:12
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


Report •

#3
August 23, 2009 at 19:19:16
does that effectively turn parsing off by setting the delimiter to nothing?
Pretty much, yeah.

Report •

Related Solutions

#4
August 24, 2009 at 17:32:06
Excellent. Works perfectly. Thanks much!


Report •


Ask Question