How to remove string from another string in batch
Moderator: DosItHelp
How to remove string from another string in batch
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
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
Re: How to remove string from another string in batch
Code: Select all
set $x=%command: password=&rem.%
for /f "tokens=1*" %%a in ("%command:* password=%") do set "command=%$x% %%b"
Re: How to remove string from another string in batch
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
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
Re: How to remove string from another string in batch
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?
Re: How to remove string from another string in batch
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.
Thanks
VJoy
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.
Thanks
VJoy
Re: How to remove string from another string in batch
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
Re: How to remove string from another string in batch
Nice job amel27.
A little more is needed when the command has quotes in it, which it doesn't in vivek4075 case.
Output:
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
Re: How to remove string from another string in batch
DosItHelp, thanks
yes, it is probable case:DosItHelp wrote: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 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
Re: How to remove string from another string in batch
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
Re: How to remove string from another string in batch
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
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
Re: How to remove string from another string in batch
Hi,
sorry amel27 but it fails with my standard password: abc"&"&def
It could be hard to accept %1 or %* with such a content.
Do you find a solution?
jeb
sorry amel27 but it fails with my standard password: abc"&"&def
It could be hard to accept %1 or %* with such a content.
Do you find a solution?
jeb
Re: How to remove string from another string in batch
jeb wrote:sorry amel27 but it fails with my standard password: abc"&"&def
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
Re: How to remove string from another string in batch
Hi,
at the command prompt I have to type
I'm sure, it can be solved - yes we can
jeb
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
jeb
Re: How to remove string from another string in batch
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