How to escape '&' ampersand in command prompt [duplicate] - batch-file

This question already has answers here:
How do I escape ampersands in batch files?
(8 answers)
Closed 4 years ago.
If you have maybe a code like this
set "CONTENT_LENGTH=24" & echo first=1&second=2&third=3
I want the echo to echo with the &

Two ways you could do this.
Escape characters
In Windows batch files it seems to be the ^ character, so you'd just stick a ^ before your special character.
This page seems to cover the exceptions: http://www.robvanderwoude.com/escapechars.php
On OS X/Linux, use backslashes: echo three\&four
Wrap the parameter phrase in quotes.
echo "three&four"
This should work on either system.

Related

How to check if an "if" statement contains something [duplicate]

This question already has answers here:
Batch file: Find if substring is in string (not in a file)
(13 answers)
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
(1 answer)
Closed 1 year ago.
For example:
if "%input%" contains "Hello"
I've also tried:
if "%input%" neq "Hello" (
echo example
)
But whenever I press ENTER, or type anything, I didn't set here, it just returns example
Any way to fix this?

How do I get the echo command to display %date% and not print the actual date [duplicate]

This question already has answers here:
Escape percent in bat file
(1 answer)
Ignore percent sign in batch file
(6 answers)
Escape percent signs in given variables
(1 answer)
Is there a way to escape % while using call in batch file?
(2 answers)
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
(8 answers)
Closed 3 years ago.
I had just showed a friend of mine some of the cool stuff you can do with the command prompt and batch code and she gotten really interested and wants to try it out on her own when she has the time. Shortly after that, I had gotten the idea of making a nice, well polished batch file that will not only demonstrate, but also display display the code in the code on the command window with out the command window executing the commands.
Example: the code in question, %date%, will display the current date when I have the code of echo %date% on the line. I would like to have %date% to be displayed like normal text.
What code am I missing to have %date% and %time% and any other batch code be displayed as normal text so the person having fun with the file can see the code without having to cipher through the code to find the code I am wanting them to learn?
P.S.
I have a feeling this question might have been answer through a different thread and if so, fee free to link me to it. Thank you!

String compare with batch file [duplicate]

This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed 3 years ago.
A very simple batch file. I'm trying to search for file extensions that are not .txt. There will be one .txt, but the rest will be like .txt_20190607.
for %%I in (\\01mtsdv130\Myapp\Log\*.*) do (
set var1=%%~xI
echo %var1
if %var1%==".txt" (
echo Matches
) else (
echo does not match
)
)
I have files in that folder both .txt and those with the extra date info in the extension. What do I have wrong?
There are two problems in the code.
The first one is that %-based expansion of normal variables is rather "static", in that it happens the first time code is parsed/executed and is fixed since then. That means that in iterations of the loop after the first, the result of %var1% will not change. You'd have to use !var1! (along with setting EnableDelayedExpansion) to get the behaviour you want.
An easier alternative is to get rid of var1 altogether and just use %%~xI.
The other problem is that CMD treats quotes (almost) as any other character. Most notably, the strings a and "a" are not considered equal. Therefore, the if should look like this:
if "%%~xI"==".txt" (

How to echo something to a .txt file even if it's executable as a program BATCH [duplicate]

This question already has answers here:
using batch echo with special characters
(8 answers)
Closed 4 years ago.
I have a BATCH script that will create a text file that will later be executed by another program based on user input. I want to be able to echo Label>start to a text file. Unfortunately, CMD reads it as a command (because of the > character) and does not echo properly. If I use quotation marks they echo to the .txt file
"Level>start"
and so it cannot be executed. I really need some help with this.
echo Level^>start
seems to work.

Batch Escape Percent sign [duplicate]

This question already has answers here:
Ignore percent sign in batch file
(6 answers)
Closed 2 years ago.
Hi Guys having a really hard time getting an environmental variable piped to clip.exe, in a regular command prompt it works but once in batch file it fails. I have tried escaping with double %% and a few other tricks. Nothing seems to work.
I am trying to pipe the output of the citrix environmental variable %clientname% to clip.exe like the following:
#echo off
echo %clientname% | clip
Just use the ^ sign to escape the % char in command line:
C:\> echo ^%clientname^%
But you need to use the % sign to escape a % symbol in a batch file:
#echo %%clientname%%

Resources