Computing.Net > Forums > Programming > Replace & in Folder Names

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.

Replace & in Folder Names

Reply to Message Icon

Name: Darth Sidious
Date: September 21, 2009 at 05:33:33 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: September 21, 2009 at 06:00:32 Pacific
Reply:

It works on w2k, which is more like XP than anything I know of.


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

M2


0

Response Number 2
Name: Darth Sidious
Date: September 21, 2009 at 06:12:55 Pacific
Reply:

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?


0

Response Number 3
Name: klint
Date: September 21, 2009 at 07:13:28 Pacific
Reply:

Try enclosing the arguments in quotes:

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


0

Response Number 4
Name: Mechanix2Go
Date: September 21, 2009 at 07:17:16 Pacific
Reply:

Carefully re-read #1.


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

M2


0

Response Number 5
Name: lantere
Date: September 22, 2009 at 02:35:51 Pacific
Reply:

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!"
)


0

Related Posts

See More



Response Number 6
Name: Darth Sidious
Date: September 23, 2009 at 00:12:45 Pacific
Reply:

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!


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Replace & in Folder Names

Replace space in folder names www.computing.net/answers/programming/replace-space-in-folder-names/16936.html

Comparing strings in folder names www.computing.net/answers/programming/comparing-strings-in-folder-names/15701.html

Srch Rplce Characters in FOLDER Nam www.computing.net/answers/programming/srch-rplce-characters-in-folder-nam/16573.html