SoFunction
Updated on 2025-03-10

BAT script receives input numbers

Bat programming, why can't the content of input (set /p) be received in the if statement?

1. Use delay variable: setlocal EnableDelayedExpansion

2. The variable name is not represented by a percentage sign but by an exclamation mark: adb shell dumpsys procstats --hours !input_time!

Original script reference:

@echo off
@echo memory infomation
setlocal EnableDelayedExpansion
title Memory information
if not exist "D:\cpu_memory_log\" md "D:\cpu_memory_log\"
set time_hh=%time:~0,2%
if /i %time_hh% LSS 10 (set time_hh=0%time:~1,1%)
set filename=%date:~,4%%date:~5,2%%date:~8,2%_%time_hh%%time:~3,2%%time:~6,2%
echo *************************************************************
echo *                                           *      
echo *                   PrintMemory information           *
echo *                   2022Year01moon19day                *
echo *                   author:Brother Sanhuo                  *
echo *                                           *
:while
echo *************************************************************
echo    0:Print all process memory
echo    1:Print导航Memory information
echo    2:PrintsurfaceflingerMemory information
echo    3:Print音乐Memory information
echo    4:PrintevsrenderengineMemory information
echo    5:Print语音Memory information
echo    6:PrintdashboardMemory information
echo    7:PrintdashboardofunityMemory information
echo    8:Print智能灯光Memory information
echo    9:PrintradioMemory information
echo    10:Print智能驾驶Memory information
echo    11:Print舒适太空仓Memory information
echo    12:Print蓝牙电话Memory information
echo    13:PrintrenderserviceMemory information
echo    14:PrintfreeMemory information
echo    15:Print高德公版Memory information
echo    16:Print各个进程of最小/average/最大Memory information
 
set/p input_number=Please enter:
if %input_number% == 0 ( 
  echo Print所有内存
  adb shell dumpsys meminfo
) else if %input_number% == 1 ( 
  echo Print导航Memory information
  adb shell dumpsys meminfo "$(pidof )" 
) else if %input_number% == 2 ( 
  echo PrintsurfaceflingerMemory information
  adb shell dumpsys meminfo "$(pidof surfaceflinger)"
) else if %input_number% == 3 ( 
  echo Print音乐Memory information
  adb shell dumpsys meminfo "$(pidof )"
) else if %input_number% == 4 ( 
  echo Print引擎Memory information
  adb shell dumpsys meminfo "$(pidof evsrenderengine)"
) else if %input_number% == 5 ( 
  echo Print语音Memory information
  adb shell dumpsys meminfo "$(pidof )"
) else if %input_number% == 6 ( 
  echo PrintdashboardMemory information
  adb shell dumpsys meminfo "$(pidof )"
) else if %input_number% == 7 ( 
  echo PrintdashboardofunityMemory information
  adb shell dumpsys meminfo "$(pidof :unity)"
)else if %input_number% == 8 ( 
  echo Print智能灯光Memory information
  adb shell dumpsys meminfo "$(pidof )"
) else if %input_number% == 9 ( 
  echo PrintradioMemory information
  adb shell dumpsys meminfo "$(pidof )"
) else if %input_number% == 10 ( 
  echo Print智能驾驶Memory information
  adb shell dumpsys meminfo "$(pidof )"
) else if %input_number% == 11 ( 
  echo Print舒适太空仓Memory information
  adb shell dumpsys meminfo "$(pidof )"
) else if %input_number% == 12 ( 
  echo Print蓝牙电话Memory information
  adb shell dumpsys meminfo "$(pidof )"
) else if %input_number% == 13 ( 
  echo Print蓝牙电话Memory information
  adb shell dumpsys meminfo "$(pidof :renderservice)"
) else if %input_number% == 14 ( 
  echo PrintfreeMemory information
  adb shell free -m
) else if %input_number% == 15 (
  echo Print高德公版Memory information
  adb shell dumpsys meminfo "$(pidof )"
) else if %input_number% == 16 (
  echo Print各个进程of最小/average/最大Memory information
  set /p input_time=Please enter获取多长时间of内存:
  adb shell dumpsys procstats --hours !input_time!
 
)else ( 
  echo Incorrect input data
)
@REM loop 
@GOTO while
:wend
 
@PAUSE 
 

This is the end of this article about receiving input numbers in BAT scripts. For more relevant content on input numbers in BAT, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!