Error handling in batch script [duplicate] - batch-file

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.

Related

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

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%%

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

How to take input from command prompt using batch file [duplicate]

This question already has answers here:
Variables are not behaving as expected
(1 answer)
Example of delayed expansion in batch file
(5 answers)
Using "SET /P" inside an IF statement
(5 answers)
Closed 2 years ago.
I called the batch file from power shell
start-process "cmd.exe" "/c $createInDBs"
I am trying to take input like name of the database from the command prompt using a batch file
Here is my code:
echo off
cd "C:\Program Files"
Choice /N /C ab /M " <?> (A)Create All-in-one database <?> (B)Create Individual database"
If Errorlevel 2 (
set /p sIndiviName=Enter the name of webserver:
echo %sIndiviName% is an Individual Database.
) Else (
set /p sAllName=Enter the name of All-in-one database:
echo %sAllName% is an All-in-one Database.
pause
)
cmd.exe is asking for the name of the database, but in the command, the database is not taken
I got 'is an All-in-one Database'
Name of the database is not displayed

Works in Command Prompt but not in BAT-file or CMD-file [duplicate]

This question already has answers here:
Why does FOR loop command not work in a batch file which works on Windows command prompt?
(3 answers)
Closed 4 years ago.
Due to problem with WINDOWS DEFENDER I need do the following:
for /r "%appdata%\..\Local\MyProg\2.0" %D IN ("*MyProgram.exe") DO "%~fD
It works perfect in COMMAND PROMPT but not in batch-file or cmd-file - why?
How do I make in a excutable file as a Batch-file or ".cmd"?
When running for loops in a batch file, you need to use an additional % in predefined variables. So it should be:
for /r "%appdata%\..\Local\MyProg\2.0" %%D IN ("*MyProgram.exe") DO "%%~fD
I suggest you read up using the well documented help by running for /? from cmdline. You will benefit from it, guaranteed!
A recursive For loop already returns the full path, additionally there's already a system variable for the path %AppData%\..\Local.
For /R "%LocalAppData%\MyProg\2.0" %%A In ("*MyProgram.exe") Do "%%A"
Depending upon your needs, it may be worth checking out the Start command usage too, Start /?. You may find that Do Start "" "%%A" is what you needed.

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