Computer Hope

Microsoft => Microsoft DOS => Topic started by: John_L. on February 11, 2021, 04:11:29 PM

Title: set command escape character
Post by: John_L. on February 11, 2021, 04:11:29 PM
I've got the contents of a batch variable and I'd like to change "~" to "&#7E;" (without the quotes). I thought this would work:

set J=blah~blah
set J=%J:~=^&#7E;%

but alas, it does not.

Thanks in advance for any insight you can provide.
Title: Re: set command escape character
Post by: DaveLembke on February 11, 2021, 09:23:32 PM
Thinking you might need to set variable as a string as the escape character only escapes the first single character of & and the rest is unexpected syntax. More at this link on the SET command.

https://ss64.com/nt/set.html
Title: Re: set command escape character
Post by: John_L. on February 12, 2021, 09:13:14 AM
Thanks much.

The 8191 character limit for environment variables will likely bite me in the future, any experience with workarounds? I know of a DOS-based sed, and I "could" write a very small Java program...I prefer to keep it simple, and reduce dependancies (as if .bat files are "simple")

Thanks again.
Title: Re: set command escape character
Post by: DaveLembke on February 18, 2021, 09:12:06 AM
Curious if there is a need to use batch at all in what your trying to achieve? I have used system calls to DOS environment from within programs that aren't in the root of the system, so for example with C++ and PERL I have used system calls and these languages allow for far more than Batch Files themselves.  *System Calls from C++ etc, some see as poor programming practice though, but they work. They are ok for applications where you yourself are using them and others dont interact with them.

SED is good, but I have used mostly PERL for making batched like scripts do what I want and without the limits of batch itself.
Title: Re: set command escape character
Post by: Squashman on March 26, 2021, 01:03:53 PM
The problem is the tilde.  If you read the help file for the SET command you will notice that the tilde and equals symbol both have special meanings.  The tilde is used for substrings and the equals symbol is used for string replacement.  You cannot escape the tilde or equals symbol in the SET command which means you cannot do string replacement on either symbol with the SET command.