Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

Escape characters in input?

This could come useful in some of my scripts, on phone so ignore typos etc
So take this code as example
Code
@echo off
echo How are you?
echo.
set /p onput=
echo %onput%

Say if you typed, &goto LA&ipconfig | findstr Connection

So on, how do you automatically add like a ^| etc?
Hi 🤝
Try something like that :

Code
@echo off
setlocal EnableDelayedExpansion
:main
cls
echo How are you?
echo.
set /p onput=
set "temp=!%~1!"
set "temp=!temp:^=^^!"
set "temp=!temp:&=^&!"
set "temp=!temp:|=^|!"
set "temp=!temp:<=^<!"
set "temp=!temp:>=^>!"
set "temp=!temp:"=^^"!"
set "!onput!=!temp!"
echo !onput!
pause
goto:main

Or more simple like this :You can take a look at
setlocal EnableDelayedExpansion

Code
@echo off
setlocal EnableDelayedExpansion
:main
cls
echo How are you ?
echo.
set /p onput=
echo !onput!
pause
goto:main

Hope that what do you expecting for and of course this may be improved by other members here !
Good Luck for you ✌
Hackoo is correct :>

To further explain, we use string substitution to escape any reserved characters.

Consider this simple example.
Code
set "var=Cats are best"
echo %var%
echo %var:best=fluffy%

Code
Cats are best
Cats are fluffy

On the first line, we set a variable to contain the text Cats are best.
On the second line, we echo that variable.
On the third line, we echo the variable, but we replace any instances of "best" with "fluffy". So what actually comes out is "Cats are fluffy".

So it's %var:A=B%
Where A is the thing to be replaced, and B is the thing to replace it with. :>

In this case, we use this technique to replace any instances of | with ^|
:>


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by notebookJan 25, 2021 07:52 AM 0 182 Jan 25, 2021 07:52 AM
Last Post: notebook
[.NET] by CendrixMar 21, 2020 09:24 AM 8 335 Mar 21, 2020 09:24 AM
Last Post: Cendrix
[C] by hansbaldMay 9, 2018 11:02 AM 1 181 May 9, 2018 11:02 AM
Last Post: HFDone