Batch script command add/change number # in variable name - batch-file

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

Related

local variable and return value of function in windows batch

#echo off
set /a n=99
echo before call test3
echo n=%n%
set ret=
call :test3 ret
echo after call test3
echo n=%n%
echo show array:
echo %ret.Array[0]%
echo %ret.Array[1]%
echo %ret.Array[2]%
echo %ret.Array[3]%
ECHO Press any key to close the windows...
pause>NUL
goto :eof
:test3
setlocal
set /a n=0
:Loop-Start
if %n% GEQ 3 goto :Loop-End
endlocal
set %~1.Array[%n%]=V%n%
setlocal
set /a n=n+1
goto :Loop-Start
:Loop-End
endlocal
goto :eof
Hi,
I have written a function test3 with local var named n, and test3 get a reference to variable named ret from caller as it parameter.
As I show in the code, I want my test3 to make an array has three elements, variable named ret in caller would hold the array.
But in caller when I print the array, I found I have not got three elements in the array.
who can help? thanks
Resolved. Following is working code for subroutine test3:
:test3
setlocal EnableDelayedExpansion
set /a n=0
for /l %%i in (0,1,3) do (
set s1=V!n!
if defined _ret (
set _ret=!_ret! ^&
)
set _ret=!_ret!set %~1.Array[!n!]=V!n!
set /a n=n+1
)
(
endlocal
%_ret%
)
goto :eof
Thanks

How to have multiple sums in a variable in batch

I am trying to create a matrix this one works below) but is slow as it has to loop every number. So i was wondering if you can have multiple %random% %% 2 without just a "(random number) %2" being printed.
#echo off
color 0a
:a
set /a mat=%random% %% 2
echo |set /p=%mat%
goto a
I found this to be much faster:
#echo off
if not "%1" == "max" start /MAX cmd /c %0 max & exit/b
color 0a
setlocal EnableDelayedExpansion
:a
set "addition="
for /L %%i in (1,1,256) do (
set /a mat=!random! %% 2
set addition=!addition!!mat!
)
echo |set /p=%addition%
goto a
EDIT: I now see that the answer #Stephan links to in the comments is very similar, so credit goes to him too.
EDIT #2:
You might like this one too, has all kind of characters so it looks a bit more "matrixy"
#echo off
if not "%1" == "max" start /MAX cmd /c %0 max & exit/b
color a
setlocal EnableDelayedExpansion
set "CHARS=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789######$$$%%%%%%*** "
echo %CHARS%>x&for %%? in (x) do set /a strlength=%%~z? - 2&del x
:a
call :randomString addition 100 "%chars%" %strlength%
echo |set /p=%addition%
goto a
:randomString
set "length=%2"
set "CHARS=%~3"
if ["%CHARS%"]==[""] set "CHARS=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789######$$$%%%%%%***"
set "strlength=%4"
if [%strlength%]==[] echo %CHARS%>x&for %%? in (x) do set /a strlength=%%~z? - 2&del x
set "line="
for /L %%a in (1 1 %length%) do (
set /a "randnr=!random!%%!strlength!"
for /l %%n in (!randnr! 1 !randnr!) do set "line=!line!!CHARS:~%%n,1!"
)
set %1=%line%
exit /b
For extra effect add 6 spaces to the CHARS variable. Note that I also added a line to start it in full-screen mode.
The method below is the fastest possible way to solve this problem:
#echo off
setlocal EnableDelayedExpansion
rem Prepare the output for one line
set "bin=0101010101"
set "line="
for /L %%i in (1,1,79) do (
set "line=!line!%%bin:~^!random:~-1^!,1%%"
)
for /L %%i in () do call echo %line%

Number isn't added to variable in if statement

Why doesn't this batch script add the value 5 to the variable %counter%?
The script always echo the value 2 with which the variable was initialized.
Outside a if statement the counter works just fine.
:start
set /a counter=2
set /p message=Message:
set "spam=%message%"
echo %spam%
if "%message%"=="%spam%" (
set /a counter=%counter% + 5
echo %counter%
)
pause
goto
start
You need to use delayedexpansion. %counter% is being evaluated outside of the If statement but not inside of it.
#echo off
setlocal enabledelayedexpansion
:start
set /a counter=2
set /p message=Message:
set "spam=%message%" echo %spam%
if "%message%"=="%spam%" ( set /a counter=%counter% + 5
echo !counter! )
pause
goto start

How to do math in batch-file

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).

Basic cmd number loop

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

Resources