标题: [文本处理] 批处理for赋值问题,请高手执教 [打印本页]
作者: fbt_djh 时间: 2021-11-4 23:00 标题: 批处理for赋值问题,请高手执教
我通过for把文件内容赋值给不同的变量,但是几个menu切换以后,数据就重复了,就显示2个一样的sn,重现方法:菜单1-》菜单1返回
1.txt的内容如下
List of devices attached
c003 device- @echo off
- :getsn
- setlocal enabledelayedexpansion
- #获取手机的sn并赋值给变量,可以有多个手机
- FOR /F "skip=1 tokens=1 delims= " %%i in (1.txt) do (
- if not defined d1 (set d1=%%i) else (
- if not defined d2 (set d2=%%i))
- )
- ::取出设备信息的sn
- FOR /F "usebackq tokens=1 delims= " %%a in ('%d1%') do set device1=%%a
- FOR /F "usebackq tokens=1 delims= " %%i in ('%d2%') do set device2=%%i
-
- :startsn
- cls
- echo =============请选择sn =============
- echo 1.%device1%
- echo.
- echo 2.%device2%
- echo ==========================================
- set /p startsn1=你的选择是(输入序号):
- if %startsn1% ==1 (
- set startsn=%device1%
- goto menu
- )
-
- if %startsn1%==2 (
- set startsn=%device2%
- goto menu
- )
- :menu
- cls
- echo =============请选择 ==============
- echo 1.返回上一级 2.开始
- echo ===========================
- set /p user_input=选择(输入序号):
- if %user_input% ==1 goto getsn
- if %user_input%==2 goto :run
-
- :run
- echo start test
- pause
复制代码
作者: idwma 时间: 2021-11-4 23:48
本帖最后由 idwma 于 2021-11-5 00:00 编辑
- @echo off
- :getsn
- setlocal enabledelayedexpansion
- #获取手机的sn并赋值给变量,可以有多个手机
- FOR /F "skip=1 tokens=1 delims= " %%i in (1.txt) do (
- if not defined d1 (set d1=%%i) else (
- if not defined d2 (set d2=%%i
- FOR /F "usebackq tokens=1 delims= " %%a in ('!d1!') do set device1=%%a
- FOR /F "usebackq tokens=1 delims= " %%i in ('!d2!') do set device2=%%i
- call :startsn
- set d1=
- set d2=
- )
- )
- )
- goto :eof
-
- ::取出设备信息的sn
-
- :startsn
- cls
- echo =============请选择sn =============
- echo 1.%device1%
- echo.
- echo 2.%device2%
- echo ==========================================
- set /p startsn1=你的选择是(输入序号):
- if %startsn1% ==1 (
- set startsn=%device1%
- call :menu
- )
-
- if %startsn1%==2 (
- set startsn=%device2%
- call :menu
- )
- goto :eof
-
- :menu
- cls
- echo =============请选择 ==============
- echo 1.返回上一级 2.开始
- echo ===========================
- set /p user_input=选择(输入序号):
- if %user_input% ==1 goto :eof
- if %user_input%==2 goto :run
- goto :eof
-
- :run
- echo start test
- pause
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |