李毛毛的技术专栏 An interesting software testing engineer

条件、循环

2020-05-07

bat

if语句

详细信息可以if/?获取

set var=python
if %var%==python ehco PYTHON
if "%var%"=="python" echo PYTHON

批处理中如果左边加了引号,右边也要加上

:::::::::::::::::::改变颜色::::::::::::::::::::::::::::::
@echo off
echo what color you want?

:RETRY
set /p choice=Please input color you want,R or G:

if "%choice%"="R" goto R
if "%choice%"="r" goto R
if "%choice%"="G" goto G
if "%choice%"="g" goto G
goto RETRY

:R
color c
echo you choice is red
pause
exit

:G
color a
echo you choice is green
pause
exit
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::else::::::::::::::::::::::::::::
@echo off

if "%TIME:~0,2%" less "12" (
echo now is am
) else (
echo now is pm
)
pause
:::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::if exist:::::::::::::::::::::
if exist "D:\test\a.txt" (
del "D:\test\a.txt"
) else (
echo a.txt is not exist!
)
::::::::::::::::::::::::::::::::::::::::::::::::::::

if defined 与if exist 类似,但是if defined判断的是变量是否被定义

for循环

1.命令行中使用格式:for %i in (*.*) do @echo %i,批处理中使用格式for %%i in (*.*) do @echo %%i

2.批量修改文件名

:::::::::::::::::::::批量修改文件名1.bat:::::::::::::::::::
@echo off
:使用了 setlocal EnableDelayedExpansion 后,可以让 for 或 if 后面的执行语句中变量的值随其变化而不断更新(所以后面使用了 !num! 而不是 %num%)
setlocal EnableDelayedExpansion
set /a num=1
for %%i in (D:\test\*.txt) do (
ren "%%i" !num!.txt
set /a num+=1
)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::批量修改文件名2.bat::::::::::::::::::::::::
@echo off
setlocal EnableDelayedExpansion

set /p zpath=please input path:
set /p prefix=please input prefix(not incloud \/:*:"<>|):

下一篇 忘记mysql密码

Comments

Content