概述
1、if语句介绍
1.1 判断字符串是否相等
if "字符串1"=="字符串2" (语句)else 语句
NOTE:该语句需要在同一行
1.2 判断盘符、文件夹、文件是否存在
if exist "盘符/文件夹/文件名" (语句) else 语句
2、案例
2.1 判断字符串是否相等
2.1.1
代码如下:
set processSeries=%PROCESSOR_IDENTIFIER%
echo %processSeries%
echo %processSeries:~5,2%
echo off
if /i %processSeries:~5,2%==86 (goto X86) else goto X64
:X86
echo "X86"
pause
exit
:X64
echo "X64"
pause
exit
NOTE: /i:不区分大小写
运行结果:
D:ProjectNelsonUSB-LOCK>set processSeries=Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
D:ProjectNelsonUSB-LOCK>echo Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
Intel64 Family 6 Model 158 Stepping 9, GenuineIntelD:ProjectNelsonUSB-LOCK>echo 64
64D:ProjectNelsonUSB-LOCK>echo off
"X64"
Press any key to continue . . .
2.1.2 交互式输入字符串
代码:
set /p str1="please input the first string..."
set /p str2="please input the second string..."
echo off
if %str1%==%str2% (echo "str1 is equal to str2") else echo "str1 is not equal to str2"
if /i %str1%==%str2% (echo "str1 is equal to str2") else echo "str1 is not equal to str2"
pause
运行结果:
2.2 判断盘符/文件夹/文件是否存在
代码如下:
echo off
if exist .123.txt (echo "file 123.txt exists") else echo "there is no file 123.txt"
if exist .234.txt (echo "file 234.txt exists") else echo "there is no file 234.txt"
if exist "F:" (echo drive F exists) else echo "drive F does not exist"
if exist "D:" (echo drive D exists) else echo "drive D does not exist"
pause
运行结果:
最后
以上就是粗心树叶为你收集整理的DOS编程---if语句1、if语句介绍2、案例的全部内容,希望文章能够帮你解决DOS编程---if语句1、if语句介绍2、案例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复