1

I am trying to write a batch program that writes more code based on what the user inputs. I am stuck on the string manipulation, as "!", ">", "&", and "%" all need to be escapeded before they are to be outputted to other files. This is what I have so far:

@echo off
set /p code=
set newcode=%code:^%=^%%,^>=^^>,!=^^!,^&=^^&%
echo %newcode%>>file.bat

All this escapinging escaped stuff is making my brain hurt, so can you please help me?

Thanks!

1
  • PLease explain what you intend to use this, or why you want to escape all these characters. DO you simply redirect this to a file? Commented Feb 5, 2014 at 6:45

2 Answers 2

3

Since you haven't explained clearly what you are trying to do with the input, I am assuming you are trying to get the user to type something and then for that to be entered into a file. If that is the case te utilise copy in combination with con

@echo off
Echo Enter ^^Z (Ctrl + Z) and return it to end input
copy con file.txt

And that will allow the user to type WHATEVER they want, and it to be put in a file to be examined. Otherwise you're gonna have a lot of fun (And trouble) dealing with all the escaping you're gonna have to do (Rather ironic wasn't that?).

Mona.

Sign up to request clarification or add additional context in comments.

Comments

2

Simply use delayed expansion in your case, as delayed expansion needs no further escaping for the content.

@echo off
setlocal EnableDelayedExpansion
set /p code=
echo(!code!

The echo( is for safe text output, even if the text is empty or is something like /?.

To the comment of @foxidrive:
When delayed expansion is enabled, then exclamation marks ! are altered, when they are visible before the delayed expansion is done, but the expanded content of delayed expanded variables will never be altered.

@echo off
set "var=Hello!"
setlocal EnableDelayedExpansion    
echo "!var!" "Hello!"

This will result to: "Hello!" "Hello"

4 Comments

It needs to be changed for ! jeb.
@foxidrive What needs to be changed? I don't understand your statement
I thought delayed expansion would alter ! characters but it doesn't. Mea Culpa.
@foxidrive Yes it's a bit confusing, I added a sample

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.