batch math command not working - batch-file

I seem to have hit a road block with trying to get a batch file to subtract a set number from a user entered variable. I have a command where a user enters an IP address and then delimits it to four separate octets, then I need to subtract 19 from the last octet. No matter how I set it up all it get back is -19. I read that set /a doesn't need the % but nothing seems to help
echo. Enter IP address of the CSR on the site.
echo. Example: 11.152.34.82
set /p IP="IP Address: "
for /F "tokens=1,2,3,4 delims=." %%a in ("%IP%") do echo %%a, %%b, %%c, %%d
pause
set /a z=d - 19
echo. %z%
pause
I have also tried
set /a z=%%d - 19
echo. %z%
pause
and
set /a z=%d% - 19
echo. %z%
pause
Any help would be appreciated.

Capture the last octet within the loop then operate on that variable:
for /F "tokens=1,2,3,4 delims=." %%a in ("%IP%") do (
echo %%a, %%b, %%c, %%d
set last=%%d
)
set /a z=%last% - 19
echo z=%z%

Using the method similar to the last one I provided in you last question:
#Echo Off
Echo= Enter IP address of the CSR on the site.
Echo= Example: 11.152.34.82
Set/P "IPA= "
Call :Sub %IPA:.= %
Rem Your commands using new %IPA% here:
Echo= Your new IP address is %IPA%
Pause
Exit/B
:Sub
ClS
If %4 Gtr 19 Set/A LO=%4-19
Set "IPA=%1.%2.%3.%LO%"

Related

pulling a variable from text file in a sentence

im currently making something along the lines of a sentence generator, it uses text files which have a list of words in them, for example celebrity.txt has a list of celebrities and this script both shuffles the text file into newcelebrity.txt and takes the first one from that list so its different every time, and ive run into a problem, i want it to be one line and that you can call a variable in the sentance youre typing, not break it down as it is right now, is there a way to have it "this $celebrity is really great" as of now, it works like this: https://gyazo.com/9ae8583ed5457709bd1c1dc9cc0cc106 and outputs as this https://gyazo.com/1a5a90f1fbf80faa73d71791a8c1c761, i dont mind the quotation marks at all, its just the way you input it.
Is there any way to make it work like i want it to or is this a limitation of batch files?
set /p message=Unity:
set /p input=The variable:
set /p after=Unity:
:gen
setlocal
cd ..
cd rcs
echo doing background work, please wait a few seconds :)
for /f "delims=" %%a in (%input%.txt) do call set "$$%%random%%=%%a"
(for /f "tokens=1,* delims==" %%a in ('set $$') do echo(%%b)>new%input%.txt
endlocal
cls
set "File=C:\Users\%USERNAME%\Desktop\gg\rcs\new%input%.txt"
set /a count=0
echo background work done :)
timeout /t 1 >nul
SETLOCAL enabledelayedexpansion
for /F "tokens=* delims=" %%a in ('Type "%File%"') do (
Set /a count+=1
Set "output[!count!]=%%a"
)
For /L %%i in (1,1,%Count%) Do (
Call :Action "!output[%%i]!"
pause
)
Exit
::*******************************************************
:Action
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo %message% %1 %after%
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo if you want to go back and change your inputs type back
echo if you want to continue generating, type gen
echo.
set /p instruction=
goto %instruction%
::*******************************************************
To get rid of the quotation marks, just use %~1 instead of %1.
I'm assuming, you want to input something like 'this $celeb is great' instead of the first three set /p lines (as your output is already in one line).
I used some fixed variables to keep it short - of course you would fetch them from the user and the text file instead, as you already did.
#echo off
setlocal EnableDelayedExpansion
set "count=3"
set "output[3]=John"
set "sentence=this $celeb is great."
REM above would be your 'set /p' instead
call :action "!output[%count%]!"
goto :of
:action
echo !sentence:$celeb=%~1!
(just to be clear: $celeb is not a variable - it's just a string, that we replace later, so from user's perspective, it behaves like a variable)

batch; Save input as option for next run (livestreamer)

I created my first batch file ever a couple of weeks ago to use livestreamer more comfortably.
Basically I can either type in the name of Twitch streamer or I can start something from another plattform.
What I am trying to do is is save my input as an option for the next time.
Let's say I went to the twitch stream of "shroud". When the stream ends I would like to put shroud as an option in the beginning of the script:
:start
title twitchings
color 0a
echo Select [#] or enter Stream
echo.
echo 1. non twitch
echo 2. shroud
set /p select=?
I assume it would be easiest to store that list in a txt file and then load it upon running the batch file, but my basically non-existant knowledge in coding hinders me from creating it.
Does anyone know a quick help, I would also love to know if there is a website where I can learn this stuff without googling for hours :)
This is my updated script for those who are searching for the same issue.
#ECHO off
:start
title twitchings
color 0a
echo Select [#] or enter Stream
echo.
echo 1. non twitch
setlocal enableextensions enabledelayedexpansion
set "file=C:\Users\[...]\savedstreams.txt"
set /A i=1
for /F "usebackq delims=" %%a in ("%file%") do (
set /a i += 1
echo !i! . %%a
)
set /p select=?
set /a varCheck=%select%
if %varCheck% == %select% (goto :isnumber) else (goto :isstream)
exit /B
:isnumber
set "lineNr=%select%"
set /a lineNr-=1+1
for /f "usebackq delims=" %%a in (`more +%lineNr% savedstreams.txt`) DO (
set "stream=%%a"
goto :leave
)
:leave
set "stream=%stream:*:=%"
echo stream: %stream%
goto qual
:isstream
set "stream=%select%"
echo %stream% >>savedstreams.txt
goto qual
:TpyeInSource
echo enter URL
echo.
set /p select2=?
livestreamer %select2%
GOTO end
:qual
livestreamer http://twitch.tv/%stream% 1080p60 || livestreamer http://twitch.tv/%stream% best
GOTO end
:end
#CHOICE /C:rqn /M "[R]etry, [Q]uit or [N]ew"
IF ERRORLEVEL 3 GOTO start
IF ERRORLEVEL 2 GOTO quit
IF ERRORLEVEL 1 GOTO qual
GOTO quit
:quit
echo "bye."
#PAUSE
Thank you guys!
You can use >> to put the output of a command in text file. For example:
echo hi everyone >>textfile.txt
will add "hi everyone" to the last line of the text file.
You can also use > to add it to the first line instead.

Splitting ip and port in ip:port format using batch

For the program I am currently working on I am taking a value, which is a proxy in ip:port format, I need to be able to split the ip and port to different variables so that a different program that needs ip and port separate will be able to work. The program is basically an automated ip/proxy switcher for minecraft, just for in game reasons, I have all the code working except for the part that actually changed the proxy. I am not getting any error message, only that I don't actually know what code to write. Anyways, here is my code.
#echo off
color b
title minecraft proxy switcher
set nLine=0
echo input full path to text file containing proxies
set /P "filepath=>"
echo end >> %filepath%
:top
cls
set /A nLine=%nLine%+1
echo now at proxy number %nLine%
CALL :ReadNthLine "%filepath%" %nLine%
PAUSE >NUL & goto:top
GOTO :EOF
::***************************************************************************************
:ReadNthLine File nLine
FOR /F %%A IN ('^<"%~1" FIND /C /V ""') DO IF %2 GTR %%A (ECHO Error: No such line %2. 1>&2 & EXIT /b 1)
FOR /F "tokens=1* delims=]" %%A IN ('^<"%~1" FIND /N /V "" ^| FINDSTR /B /C:"[%2]"') DO set http_proxy=%%B
goto finish
::***************************************************************************************
:finish
if %http_proxy%==end (
cls
echo all proxies have been used
echo will return to top of list in 5 seconds
TIMEOUT /T 5 /NOBREAK
set nLine=0
goto top
)
java -DsocksProxyHost=ip -DsocksProxyPort=port -Xmx800m -jar MinecraftLauncher.exe
echo New ip is %http_proxy%
echo waiting for user input
echo press any key for a new ip
pause
goto top
Any help is greatly appreciated, also if you notice something else that's badly written or incorrect in my code please tell me.
split the string with a for, using proper tokens and delimiters:
set "line=192.168.1.1:8080"
for /f "tokens=1,2 delims=:" %%a in ("%line%") do (
set server=%%a
set port=%%b
)
echo Server %server% Port %port%
here is a basic code skeleton which processes the file line after line (your way works, but this is way easier):
#echo off
set /p "filepath=File: "
:top
set n=0
for /f "tokens=1,2 delims=:" %%a in (%filepath%) do call :process %%a %%b
timeout 5
goto :top
:process
echo trying %n%
set /a n+=1
echo host: %1
echo port: %2
pause
goto :eof

Remove the text in a ping token after a number in ping statistics to use

I'm doing a simple ping bandwidth test in batch to get speed in kb/s and I've run into a problem with the ms trailer after the average round trip time 123ms. The batch can't deal with the letters in the number token and it has no space to set another token with. I've looked at several solutions using delims= but did not success. I'm stuck and can't do anything except manually enter the number to get bandwidth. I'm running Windows XP and don't want to use anything but batch, i.e. no VBS, Java, hybred bat, etc. If it can't be done with simple command line I'll just manually enter the number and be done with it. Here is my batch:
#echo off
color 0b
MODE CON:COLS=57 LINES=15
for /F "tokens=9" %%a in ('ping -n 1 -l 1024 8.8.8.8^| find "Average"') do set "A1=%%a"
echo %A1%
set /a T=%A1%
set /a varia=1000/%T%
set /a answer=%varia%
set /a varia2=%answer%
set /a answer2=%varia2%
echo.
echo Speed %answer2% Kb/s
pause
and this from a answer here on stackoverflow
#echo off
color 0b
MODE CON:COLS=57 LINES=15
for /f "tokens=9 delims=()" %%a in (
'ping -n 1 -l 1024 8.8.8.8^| find "Average"'
) do (for /f "tokens=9" %%b in ("%%a") do (
set num=%%b & set num=!num:%%=!
if !num! == !num! goto nc
)
)
:nc
echo !num!
set /a T=!num!
set /a varia=1000/%T%
set /a answer=%varia%
set /a varia2=%answer%
set /a answer2=%varia2%
echo.
echo Speed %answer2% Kb/s
pause
Test this in XP: it works in Win 8.1
for /F "tokens=8 delims=ms=, " %%a in ('ping -n 1 -l 1024 8.8.8.8^| find "Average" ') do set "A1=%%a"

findstr gets wrong line on commad

i have a list with number of station and ip like this:
1 10.1.1.10
10 10.1.10.10
11 10.1.11.10
17 10.1.17.10
174 10.1.174.10
7602 10.16.2
7604 10.16.4
7605 10.16.5
the list is very big but this is the example.
i would like to use "findstr" to end number of station with "SET /P"
and get by that the IP.
this is what i've wrote:
#echo off
setlocal enabledelayedexpansion
set /p sn=
echo result is :
echo.
for /f "tokens=1-3" %%a in ('findstr /c:%sn% stations.list') do set station=%%a %%b %%c
echo %station%
echo.
pause
when i try to do some script it gets me for "1" it brings something else with the number "1" but no "1" exactly
Add the /b /e switches and 1 should get you the IP address on that line.
Your example doesn't use three tokens so I change it to 1,2 also:
#echo off
setlocal enabledelayedexpansion
set /p sn=
echo result is :
echo.
for /f "tokens=1,2" %%a in ('findstr /b /e /c:%sn% stations.list') do set station=%%a&set IP=%%b
echo %station%, %IP%
echo.
pause

Resources