Replace & in Folder Names

September 21, 2009 at 05:33:33
Specs: Windows XP
I have been using the code below to rename a folder character.

The problem is the folder contains the "&" (Ampersand) I have used the caret ^ symbol in a part of the code to try and over come this problem.

setLocal EnableDelayedExpansion
Set Source=C:\Test
CD %Source%
for /f "tokens=* delims= " %%a in ('dir/b/s/ad ^| find "&"') do (
set str=%%~NXa
set str=!str:^&=x!
echo rename %%a !str!
)

The Code appears to have changed "&" to "x" when it is echoed back, although when you check the folder name, it was unsuccessful.

Is anyone able to help?


See More: Replace & in Folder Names

Report •


#1
September 21, 2009 at 06:00:32
It works on w2k, which is more like XP than anything I know of.


=====================================
Helping others achieve escape felicity

M2


Report •

#2
September 21, 2009 at 06:12:55
The code does work on my XP system as I have replaced other folder characters fine. It just will not work with "&" (Possibly other batch reserved symbols too.)

Any Ideas?


Report •

#3
September 21, 2009 at 07:13:28
Try enclosing the arguments in quotes:

echo rename "%%a" "!str!"


Report •

Related Solutions

#4
September 21, 2009 at 07:17:16
Carefully re-read #1.


=====================================
Helping others achieve escape felicity

M2


Report •

#5
September 22, 2009 at 02:35:51
Why is there an "echo" before the "rename"?
Is that just for display?
Try taking it out.

Also, place ["] before and after the Variables.
(Rename doesn't like spaces in names.)


If I use this on XP Pro, it changes [Me & You] to [Me x You]:

setLocal EnableDelayedExpansion
Set Source=C:\Test
CD %Source%
for /f "tokens=* delims= " %%a in ('dir/b/s/ad ^| find "&"') do (
set str=%%~NXa
set str=!str:^&=x!
rename "%%a" "!str!"
)


Report •

#6
September 23, 2009 at 00:12:45
Thanks for your Help. The "Quotes" Definately did it.

Left the Echo in on the last line by mistake when deleting a line, this stopped the hole thing from working.

All good now!


Report •


Ask Question