How to remove string from another string in batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vivek4075
Posts: 6
Joined: 11 Nov 2010 23:03

How to remove string from another string in batch

#1 Post by vivek4075 » 11 Nov 2010 23:12

Hi All,
I am new to this forum as well as in batch knowledge.Please help me to short out the problem
there is a string command
%command% and this value is
mywork server username=abc password=xxxx doaction
so i need to reform the same string without password.
result should be like this
mywork server username=abc doaction
and also the position will not be the same every time it can vary
like may be it can be
mywork server username=abc password=xxxx doaction
or
mywork server password=xxxx username=abc doaction


so please help me

Thanks in advance
V Joy

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: How to remove string from another string in batch

#2 Post by amel27 » 12 Nov 2010 02:16

Code: Select all

set $x=%command: password=&rem.%
for /f "tokens=1*" %%a in ("%command:* password=%") do set "command=%$x% %%b"

vivek4075
Posts: 6
Joined: 11 Nov 2010 23:03

Re: How to remove string from another string in batch

#3 Post by vivek4075 » 13 Nov 2010 07:28

thanks allot Mr. Amel27
It is working fine but there is one problem.
like if command contains password then every thing is fine but some time this will not be the part of string so in this case whatever %command% contains this will print two time.

Can you help me on that ?
Problem
on Cmd.exe(command line)
we are providing the option with one .bat file that contains
set command=mywork %*

now we are apply the below code on command string
MY problem is
if it contains password then we will go for this code otherwise not required
so how to know whether command contains password or not


please help


thanks in advance

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

Re: How to remove string from another string in batch

#4 Post by DosItHelp » 13 Nov 2010 13:15

Code: Select all

@echo off
set command=mywork server username=abc password=xxxx doaction
echo %command%
echo.

rem strip password from command
set command="%command: ="     "%"
set "newcommand="
for %%A in (%command%) do (
    echo.%%~A|findstr /i /b /c:"password=">NUL||call set "newcommand=%%newcommand%%%%~A "
)
set command=%newcommand:"     "= %

echo.
echo %command%

>>set command="%command: =" "%"<< puts quotes around each argument in the command.
The FOR loop splits the command and looks at each argument one by one, only parts that don't start with "password=" are added to newcommand.
>>set command=%newcommand:" "= %<< removes quotes that were added earlier to already quoted arguments (doesn't apply in your case).

Does it help?

vivek4075
Posts: 6
Joined: 11 Nov 2010 23:03

Re: How to remove string from another string in batch

#5 Post by vivek4075 » 14 Nov 2010 04:20

thanks allot Mr. amel27
absolutely right ,perfect
now this is what i want.

Please suggest me some book or link to start batch concepts.

In the same forum i have posted one more Question and i know you will be the perfect man to answer the problem

How to pass unicode param on cmd line or read unicode from properties file using batch script.
once you have time please look it and suggest me some clue or code. :lol:
Thanks
VJoy

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: How to remove string from another string in batch

#6 Post by amel27 » 14 Nov 2010 05:47

vivek4075 wrote:if command contains password then every thing is fine but some time this will not be the part of string so in this case whatever %command% contains this will print two time

Code: Select all

@echo off

call :RmPass password=xxx server username=abc doaction
call :RmPass server username=abc doaction password=xxx
call :RmPass server username=abc doaction

EXIT

:RmPass
set command=mywork %*

set $x=%command: password=&rem.%
if not "%$x%"=="%command%" for /f "tokens=1*" %%a in ("%command:* password=%") do set "command=%$x% %%b"

set command
GoTo :EOF

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

Re: How to remove string from another string in batch

#7 Post by DosItHelp » 14 Nov 2010 13:06

Nice job amel27.

A little more is needed when the command has quotes in it, which it doesn't in vivek4075 case.

Code: Select all

@echo off
call :RmPass mywork password=xxx server username=abc doaction
call :RmPass mywork server username=abc doaction password=xxx
call :RmPass mywork server username=abc doaction
call :RmPass mywork server password=xxx username=abc doaction "la la" li "lo" "lu" bla
goto:eof

:RmPass
SETLOCAL
set command=%*
set command="%command: ="     "%"
set "$x="
for %%A in (%command%) do echo.%%~A|findstr /i /b /c:"password=">NUL||call set "$x=%%$x%%%%~A "
ENDLOCAL & (set "command=%$x:"     "= %")
set command
goto:EOF

Output:

Code: Select all

command=mywork server username=abc doaction
command=mywork server username=abc doaction
command=mywork server username=abc doaction
command=mywork server username=abc doaction "la la" li "lo" "lu" bla

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: How to remove string from another string in batch

#8 Post by amel27 » 14 Nov 2010 20:43

DosItHelp, thanks
DosItHelp wrote:A little more is needed when the command has quotes in it, which it doesn't in vivek4075 case.
yes, it is probable case:

Code: Select all

@echo off
call :RmPass password=xxx server username=abc doaction
call :RmPass server username=abc doaction password="xxx"
call :RmPass server username=abc password="1&2" doaction
call :RmPass server username=abc doaction

EXIT

:RmPass
SETLOCAL EnableDelayedExpansion
  set command=mywork %*
  set $x=%command: password=&rem.%
  if not !$x!==!command! for /f "tokens=1*" %%a in ("!command:* password=!") do set "$x=!$x! %%b"
ENDLOCAL& set command=%$x%
set command
GoTo:EOF

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: How to remove string from another string in batch

#9 Post by amel27 » 14 Nov 2010 22:46

Similar, but long way:

Code: Select all

@echo off
call :RmPass password=xxx server username=abc doaction
call :RmPass server username=abc doaction password="xxx"
call :RmPass server username=abc password="1&2" doaction
call :RmPass server username=abc doaction

EXIT

:RmPass
SETLOCAL EnableDelayedExpansion
set "$new="& set $old=mywork %*
:RmPass_LOOP
for /f "tokens=1*" %%a in ("!$old!") do (
  set "$a=%%a"
  if /i not "!$a:~,8!"=="password" set "$new=!$new!%%a "
  set "$old=%%b"
)
if defined $old GoTo:RmPass_LOOP
ENDLOCAL& set command=%$new%

set command
GoTo:EOF

vivek4075
Posts: 6
Joined: 11 Nov 2010 23:03

Re: How to remove string from another string in batch

#10 Post by vivek4075 » 15 Nov 2010 03:49

Thanks Allot
Mr. Mxpert/Amel27

last two solution is generic sol'n and this was applicable for me with double quotes which i never thought about that situation and today i got the same problem so again i thanks to all of you how game me the solution in advance.

Please reffer some book link


Thanks
VJoy

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

Re: How to remove string from another string in batch

#11 Post by jeb » 15 Nov 2010 04:04

Hi,

sorry amel27 but it fails with my standard password: abc"&"&def :wink:

It could be hard to accept %1 or %* with such a content.
Do you find a solution? :)

jeb

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: How to remove string from another string in batch

#12 Post by amel27 » 15 Nov 2010 05:01

jeb wrote:sorry amel27 but it fails with my standard password: abc"&"&def :wink:

Hi, jeb!.. You are ready enter this password via command line parameter?.. How? :)
in some case it is possible option of CMD tool, not CMD.EXE

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

Re: How to remove string from another string in batch

#13 Post by jeb » 15 Nov 2010 06:33

Hi,

at the command prompt I have to type

Code: Select all

myBatch.bat abc"&"^&def


I'm sure, it can be solved - yes we can :wink:
jeb

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: How to remove string from another string in batch

#14 Post by amel27 » 15 Nov 2010 20:30

jeb wrote:I'm sure, it can be solved - yes we can

Hmm... I'm in confusion, it is possible save this sequence of chars to VAR from file, but not from command line parameters... Unless not so?
But we can bypass this problem for this special case... ;)

Code: Select all

@echo off

:LOOP
SETLOCAL& set $a=%1
if not defined $a GoTo:NEXT
if /i %1==password SHIFT& SHIFT& GoTo:LOOP
set $x=%$x%%1
if /i %1==username set $x=%$x:~,-1%=
SHIFT& GoTo:LOOP
:NEXT
ENDLOCAL& set command=%$x%

set command

command line:

Code: Select all

myBatch.bat server username=abc password=abc"&"^&def doaction

Post Reply