首页 > 解决方案 > 在批处理文件中将数字转换为不同的数字

问题描述

这必须是非常基本的,但我似乎无法找到完成这项工作的方法

我的批处理脚本是这样的:

@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

0我想让用户输入一个介于和之间的数字,而100不是例如0.1010% 的音量,以使其更加用户友好。0.10但是如果用户输入,我仍然需要将10.

谷歌似乎不再是我最好的朋友,我们似乎无法就此进行交流。

如果有人可以帮助我获得首发,那就太好了。

标签: batch-fileinputnumbersoutput

解决方案


您可以在循环中使用选择来制作基于键的“滑块”,然后修改变量值以包含0.前缀或1使用 if 条件:

@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

对于 NTFS 系统,一种将最后设​​置的卷存储在备用数据流中并在返回时重新分配最后一个值的变体:

@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

注意:使用这种输入方式,不能输入无效的输入。


推荐阅读