how do i get echo to type out a % in a batch file [duplicate] - batch-file

This question already has answers here:
BATCH- echo % (print an percent sign) [duplicate]
(1 answer)
Escape percent in bat file
(1 answer)
Ignore percent sign in batch file
(6 answers)
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
(8 answers)
What is the difference between % and %% in a cmd file?
(2 answers)
Closed 6 days ago.
I want the code below to work or something that does the same thing.
#echo off
echo 100%
pause
When you open the .bat file it shuld look like this.
100%
Press any key to continue . . .
So i want the echo to read out the % and not to be a part of a code.
I want it to work the same as for example
#echo off
echo hi
pause
I have only tried echo 100% because i am a noob at coding so i dont know what else to try.

The percent sign has special meaning in (Windows) batch files, it is used to indicate a variable. For instance, echo %username% will print the contents of the username variable.
To echo a literal % sign, you need to double it, i.e. echo 100%%

Related

I Want to Create a Batch File That Creates a Batch File which creates a text file [duplicate]

This question already has answers here:
Escape angle brackets in a Windows command prompt
(6 answers)
using batch echo with special characters
(8 answers)
Closed 9 months ago.
I want to Create a Batch File1 Which Creates a Batch File2, This Batch File2 Should then Create a txt file.
Below is the 4 lines script i Want to Append to Batch File2. I need Code for Batch File1.
Please Help
echo > t.txt
:r
echo 101010101010 >> t.txt
goto r
P.S: I am new to Stack Overflow, only my account is old. until now i did not know it's True
Powers, but i'm realising.. (excuse me for being informal)
Refer to How to Escape Characters
BATCHFILE1.bat that can create the second batch file BATCHFILE2.bat
#echo off
Title I am the BatchFile1.bat who wants to create the BatchFile2.bat
(
echo #echo off
echo echo^>t.txt
echo :r
echo echo 101010101010^>^>t.txt
echo goto r
)>BATCHFILE2.bat
Batch File2.cmd should look like this:
#CD . 1>"t.txt" 2>NUL || Exit /B
:r
#(Echo 101010101010) 1>>"t.txt"
#GoTo r
The first line will create your text file, unless the current directory is not writeable. Should that be the case, your batch file will simply close without entering the loop. The code in the loop will print your required string without including any trailing spaces.In order to stop writing to the file, you will need to enter Ctrl+C
Therefore to output it from Batch File1.cmd you'd need:
#(
Echo #CD . 1^>"t.txt" 2^>NUL ^|^| Exit /B
Echo :r
Echo #(Echo 101010101010^) 1^>^>"t.txt"
Echo #GoTo r
) 1>"Batch File2.cmd"
Using this method, you simply need to escape the usual special characters, plus any nested closing parentheses. In the above your special characters are the redirection and pipe characters.

Batch file doesn't run correctly within Windows 10 [duplicate]

This question already has answers here:
Works in Command Prompt but not in BAT-file or CMD-file [duplicate]
(2 answers)
Closed 2 years ago.
I have a merge.bat file which includes the following line:
for %f in (*.txt) do type "%f" >> mergedfile.txt
This code line works perfectly if I enter it directly into a cmd window.
All the txt files are located in the same folder as this merge.bat file.
However, when I try to run it as a batch file under cmd i.e. call merge.bat it shows the following message:
f" >> mergedfile.txt was unexpected at this time
What I would like to do is setup the merge.bat file to run in the Windows 10 task scheduler at a set time, but I just can't get the .bat file to run correctly.
What am I doing wrong?
you have to add double % signs in batch file in loops.use the following code:
for %%f in (*.txt) do type "%%f" >> mergedfile.txt

Writing >Nul into a file [duplicate]

This question already has an answer here:
Writing special characters from one batch file to another
(1 answer)
Closed 3 years ago.
I am currently very new to .bat and I was messing around trying to make one batch file write another one. So far I have been fine, but I have run into the error of not being able to put >Nul into the other batch file.
I'm trying to add a simple invisible delay, for example:
ECHO PING localhost -n 4 >NUl >> Test.bat
In that text, it usually treats the >NUL as part of the code and not something that will be written into the next file.
Sorry if my wording is confusing, I am just new and do not know the best way to explain this sort of thing.
The caret ^ is the Windows escape character so you would use:
>>test.bat echo ping localhost -n 4 ^>nul
Note that I've moved the actual redirection up to the front. This isn't technically necessary but it prevents extraneous spaces from appearing at the start or end of the line in your file (all spaces before and after the >>test.bat are placed in the file if it's not before the echo).

Error handling in batch script [duplicate]

This question already has answers here:
Batch Files - Error Handling
(7 answers)
Closed 6 years ago.
I have a batch file which contains below code:
runas /user:Administrator "C:\Test.bat"
When i'm running this file it asks for Password so if in case i give incorrect password it directly terminates but i want to handle this error. It should not terminate directly.
you can do it in a loop:
:loop
runas /user:Administrator "C:\Test.bat" || goto :loop
As long as runas fails (for example because of a wrong password), it will try again.

Batch checking if a file exists or not [duplicate]

This question already has answers here:
How to verify if a file exists in a batch file?
(3 answers)
Closed 8 years ago.
I need a batch code that will tell me if test.txt is there or not it is in the same directory as the batch file. I would like to know what I should put in the space where it says the code. Can anyone help?
rem the code
echo yes
rem the code
echo no
IF EXIST yourfilename (
echo Yes
) ELSE (
echo No
)
Note : ELSE command must be on the same line as the end of the IF command.
To explore more type IF /? in cmd.

Resources