TommyMaes
TommyMaes

Reputation: 33

Convert number to different number in batch file

This must be very basic but i can't seem to find a way to get this done

my batch script goes like this:

@echo off
echo Type In Desired Volume And Press Enter
echo.
echo 0 = 0 %%
echo 1 = 100 %%
echo 0.10 = 10 %%
echo 0.65 = 65 %%
echo.
set /p input=
echo %input% > "C:\SetVol\Source\Volume.txt

I want to make it more user friendly by letting the user input a number between 0 and 100 instead of, for example, 0.10 for 10% audio volume. But i still need to output the 0.10 to a textfile if the user enters 10.

Google appears to longer be my best friend and we cant seem to communicate on this.

If anybody could help me get starter that would be great.

Upvotes: 1

Views: 402

Answers (3)

T3RR0R
T3RR0R

Reputation: 2951

You can use choice in a loop to make a key based 'slider', and then modifiy the variable value to include a 0. prefix or be 1 using an if condition:

@Echo off

 set "volume=50"
:Volume
 cls
 Echo( Current volume: %Volume%%% [I]ncrease [D]ecrease [C]ontinue
 For /f "delims=" %%G in ('Choice /N /C:IDC')Do (
  If "%%G"=="I" If not %Volume% GEQ 100 Set /A Volume+=1
  If "%%G"=="D" If not %Volume% LEQ 0 Set /A Volume-=1
  If not "%%G"=="C" Goto :Volume
 )

 IF %volume% Equ 100 ( Set "Volume=1" )Else If %volume% LSS 10 (
  Set "Volume=0.0%Volume%"
 ) Else Set "Volume=0.%Volume%"
:#your script here

For NTFS systems, a variant that stores the last set volume in an alternate data stream and reasigns the last value on return:

@Echo off

 set "volume=50"
 For /f "Usebackq delims=" %%G in ("%~f0:Volume")Do Set "%%G"
:Volume
 cls
 Echo( Current volume: %Volume%%% [I]ncrease [D]ecrease [C]ontinue
 For /f "delims=" %%G in ('Choice /N /C:IDC')Do (
  If "%%G"=="I" If not %Volume% GEQ 100 Set /A Volume+=1
  If "%%G"=="D" If not %Volume% LEQ 0 Set /A Volume-=1
  If not "%%G"=="C" Goto :Volume
 )
 Set Volume >"%~f0:Volume"
 IF %volume% Equ 100 ( Set "Volume=1" )Else If %volume% LSS 10 (
  Set "Volume=0.0%Volume%"
 ) Else Set "Volume=0.%Volume%"
:#your script here

Note: by using this method of input, invalid input cannot be entered.

Upvotes: 2

jeb
jeb

Reputation: 82247

A simple way is to convert the input number to the requested output format.

One step is to prefix the input with 0.

set /p input=[Enter volume in %%]: 
set "output=0.%input%"
echo %input% > "C:\SetVol\Source\Volume.txt

But this would fails for one digit values like 2%, becomes 0.2 instead of 0.02.

This can be fixed with prefix each number with 00 and take the last three digits and add a dot between.

set /p input=[Enter volume in %%]: 
set "temp=00%input%"
set "output=%temp:~-3,1%.%temp:~-2%"
echo %output% > "C:\SetVol\Source\Volume.txt

Upvotes: 2

TommyMaes
TommyMaes

Reputation: 33

Figured out a way to use the choice input, if anyone has a more neat or different way to do this please let me know... 291 more lines to edit...

@echo off
echo Choose Audio Volume 0-100 %%
:choice
set /P c=
if /I "%c%" EQU "0" goto :0
if /I "%c%" EQU "1" goto :1
if /I "%c%" EQU "2" goto :2
goto :choice

:0
echo 0 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit
:1
echo 0.01 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit
:2
echo 0.02 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit

Upvotes: 1

Related Questions