Computing.Net > Forums > Programming > Renaming a directory of files with spaces

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Renaming a directory of files with spaces

Reply to Message Icon

Name: techphets
Date: August 23, 2009 at 09:14:13 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: August 23, 2009 at 16:04:44 Pacific
Reply:

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"


0

Response Number 2
Name: techphets
Date: August 23, 2009 at 18:47:12 Pacific
Reply:

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


0

Response Number 3
Name: Razor2.3
Date: August 23, 2009 at 19:19:16 Pacific
Reply:

does that effectively turn parsing off by setting the delimiter to nothing?
Pretty much, yeah.


0

Response Number 4
Name: techphets
Date: August 24, 2009 at 17:32:06 Pacific
Reply:

Excellent. Works perfectly. Thanks much!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Renaming a directory of files with spaces

batch rename files with a part of.. www.computing.net/answers/programming/batch-rename-files-with-a-part-of/16747.html

Compare two text files with spaces www.computing.net/answers/programming/compare-two-text-files-with-spaces/19886.html

moving files with a .bat www.computing.net/answers/programming/moving-files-with-a-bat/17109.html