I have created a simple program to print numbers 1 to 10 and store them in a text file:
#echo off
SET /A X=1
:START
IF %X% LEQ 10 (
ECHO %X%>>C:\TXT.TXT
SET /A X+=1
GOTO START
)
PAUSE
Output that I am getting is:
ECHO OFF
10
Where have I gone wrong?
You could use a for loop for this (not an if statement):
FOR /L %i IN (1,1,10) do echo %i
(this loops from 1 to 10 in command line)
See also: http://ss64.com/nt/for_l.html
EDIT (as I tried to put the code in my comment -> if you change your code to output in the console, you'll see that your code does work, but in your case the txt only has the last time your echo'd):
#echo off
SET /A X=1
:START
IF %X% LEQ 10 (
ECHO %X%
SET /A X+=1
GOTO START
)
pause
Related
I would like my variable res to be updated inside the loop, but it does not update until after the second iteration.
Here is my code:
#echo off
set /a res=10
:loop
if %res% gtr 100 (
goto endLoop
) else (
set /a res=res*10
rem Here the 'res' shouldn't it be 100?
echo the result is : %res%
rem Here the first iteration display 10 not 100.
goto loop
)
:endLoop
pause > nul
Is there an explanation for this?
As an example of usage of delayed expansion here's your modified code:
#Echo Off
SetLocal EnableDelayedExpansion
Set "res=10"
:loop
If %res% Lss 100 (
Set/A res*=10
Echo the result is : !res!
GoTo loop
)
Pause>Nul
There is an alternative without using delayed expansion in this case, but I'd suggest you stick with the former until you are confident enough to understand the order in which things are read and parsed:
#Echo Off
Set "res=10"
:loop
If %res% Lss 100 (
Set/A res*=10
Call Echo the result is : %%res%%
GoTo loop
)
Pause>Nul
I have this code:
setlocal enableDelayedExpansion
set count=0
set letter=a,b,c
for %%a in (%letter%) do (
set /a "count+=1"
echo %count%
)
pause
The output is:
0
0
0
I want that the output will be:
1
2
3
I also tried to do it without EnableDelayedExpansion, but I had no luck. What did I do wrong?
you need to
echo !count!
with delayedexpansion
or
call echo %%count%%
%count% will always return the value of count as it stood when the block (parenthesised series of statements) was encountered.
I have been having troubles with batch-codes that I would expect to work, but don't...
Below is what I have written...
#echo off
cls
:loop
set /p "input=Input a number: "
set /a "number=%input%" 2>nul
REM check if input valid
if "%input%" NEQ "%number%" (
cls
Echo Please Enter a valid number! &Echo.&Echo.
goto :loop
)
Set /a Even=number%%2
if %Even% EQU 0 (
Echo Substituting Even Number in: x / 2
Echo set /p"=(%number%) / 2 = "
set /a answer=number/2
) Else (
Echo Substituting Odd Number in: 3x - 1
<nul set /p"=3(%number%)-1 = "
set /a answer=number*3
set /a answer=answer-1
)
Echo %answer%
Echo.
Echo.
goto :loop
Echo Unexpected Error . . .
pause
Exit
Whenever I input a number into the console, it does the math, like I want it to, but prints the number -1, and every time i input another number, the number goes to -2, -3, -4, so on.
Put a setlocal enableextensions at the beginning after the #echo off, e.g.
#echo off
setlocal enableextensions
cls
Also, I think you would also need to use delayed variable expansion (usually denoted by !var!), which would change your script above to something like this:
#echo off
setlocal enableextensions enabledelayedexpansion
cls
:loop
set /p "input=Input a number: "
set /a number=!input! 2>nul
REM check if input valid
if "!input!" NEQ "!number!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
REM Make sure that it is an integer put in (just in case)
set /a int=!number! %% 1
if "!input!" NEQ "!int!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
Set /a Even=!number! %% 2
if !Even! EQU 0 (
Echo Substituting Even Number in: x / 2
set /a answer=!number! / 2
) Else (
Echo Substituting Odd Number in: 3x - 1
set /a answer=!number! * 3 - 1
)
Echo !answer!
Echo.
Echo.
goto :loop
I also would like to point out that I also fixed a few other bugs (set /p isn't of any use in this script at all, especially in where it is used, and also you need the modulus to find even/odd).
It's pretty self-explanatory, what I'm trying to do here.
When I do something like this:
set n=0
:choose
set /a n+=1
choice /c yn
if errorlevel 2 ( echo NO! >>log.txt
) else echo YES! >>log.txt
if %n% lss 10 goto choose
The loop executes produces a new line for each time the command is executed (10 times=10 lines)
I want it to write all 10 outputs to one line in the log.
set n=0
:choose
set /a n+=1
choice /c yn
if errorlevel 2 ( echo|set /p=NO! >>log.txt
) else echo|set /p=YES! >>log.txt
if %n% lss 10 goto choose
I have problem with "%_Link%%num%" its not working show up link after load from txt format. I think "%_Link%%num%" is wrong syntax function. but I know %_Link1%,%_Link2%,etc can work... but i want loop that will add/change number # in variable name like "_Link#" changeable number as #. here code below...
TEXT FORMAT (NOTEPAD):
http://www.google.com
http://www.nba.com
test3
test5
test6
test7
SCRIPT code:
#echo off
:: LOAD FILE
SetLocal EnableDelayedExpansion
Set n=
Set _InputFile=loadlink.txt
For /F "tokens=*" %%I IN (%_InputFile%) DO (
Set /a n+=1
set /a i = 1
Set _Link!n!=%%I
Set /a num = 2
)
:loop1
CLS
echo.
echo %_Link%%num%
echo %i% seconds
start "" /b "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" %_Link%%num%
set /a i = i - 1
set /a num = num + 1
pause
GOTO loop1
:: pause
Try this:
ECHO !_Link%n%!