Replace space in variable with %20

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
honyk
Posts: 3
Joined: 17 Apr 2008 01:19

Replace space in variable with %20

#1 Post by honyk » 17 Apr 2008 01:31

I have difficulties with following:
set myPath="program files"
echo.%myPath: =%20%

%%20 or ^%20 doesn't work too.

It seems impossible to replace something by percent character :-(

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 20 Apr 2008 15:57

Hi,

I suppose you are right, it seems impossible to replace by percent this way.

But you can choose another way, an easy if you want to replace only spaces is ...

Code: Select all

@echo off
setlocal enabledelayedexpansion

set myPath="program files are here"
call :replace %myPath%
echo %result%
goto :eof

:::::::::::::::::::::::::::::::
:replace
::: Only to remove the "
call :replace_int %~1
goto :eof

:::::::::::::::::::::::::::::::
:replace_int
setlocal
set tmp=

:replace.loop
set tmp=!tmp!%1
if "%2"=="" goto :finish
set tmp=!tmp!%%20
shift
goto :replace.loop
:finish

(
endlocal
set result=%tmp%
goto :eof
)


jeb

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 29 Apr 2008 00:42

I played around and found the following working on my XP box:

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION
set myPath=program files
echo."!myPath: =%%20!"

"program%20files"

----------------

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION
set myPath="program files"
set myPath=!myPath: =%%20!
echo.%myPath%

"program%20files"

----------------

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION
set myPath=program files
set myPath=!myPath: =%%20!
echo.%myPath%

program%20files

:D

Thebetr1
Posts: 12
Joined: 30 Jun 2008 02:50
Location: My computer
Contact:

! !!!

#4 Post by Thebetr1 » 30 Jun 2008 03:36

Never heard of ! before?

and whats with the...
SETLOCAL ENABLEDELAYEDEXPANSION

Post Reply