@echo off
for %%i in (bat home) do (
echo,%%i
pause
请按任意键继续. . .
@echo off
for %%i in (bat home) do (
set /p =%%i<nul
pause
bathome请按任意键继续. . .
批处理光标回退(非cls清屏)动画效果演示
@echo off
echo bbs.bathome.net
set /p =光标回退(非cls清屏)动画效果演示:<nul
for /l %%i in (1,1,10) do (
set /p =%%i<nul
set /p =<nul
ping -n 2 127.1 >nul
echo,
pause
三、使用 set /a 命令进行数学运算(1)
等号右侧的变量可以省略百分号
C:\>set x=1
C:\>set y=2
C:\>set /a n=%x%+%y%
C:\>set x=1
C:\>set y=2
C:\>set /a n=x+y
八进制和十六进制转换成十进制很简单
C:\>set /a 012
C:\>set /a 0xA
BAT文件里面进行“取余”操作的时候需要使用两个百分号
@echo off
set /a n=3%%2
echo %n%
pause
逻辑运算的特殊字符需要使用双引号
@echo off
set /a "m=1<<10"
set /a n=1"<<"10
echo %m% %n%
pause
@echo off&setlocal enabledelayedexpansion
set /p str=输入的两数为:
set /a num_1=0,num_2=0,num=1,v=0
for %%a in (%str%) do (
set /a n+=1
for /f "tokens=1,2 delims=." %%b in ("%%a") do (
set "_!n!=%%b"
if "%%c" equ "" (
set ".!n!=00"&set /a m+=1
if !m! equ 2 set "flag=a"
) else (
set "a=%%c00"&set "a=!a:~,2!"
set ".!n!=!a!"
for %%a in (+#h -#c) do (
for /f "tokens=1,2 delims=#" %%b in ("%%a") do (
if "%_1:~,1%" equ "-" set "k1=-"
if "%_2:~,1%" equ "-" set "k2=-"
set /a %%c1=_1%%b_2,%%c2=!k1!1%.1%%%100%%b!k2!1%.2%%%100
if !%%c1! lss 0 (
if !%%c2! leq -100 set /a %%c2+=100,%%c1-=1
) else (
if !%%c1! equ 0 (
if !%%c2! lss 0 set "%%c1=-0"&set "a=!%%c2!"&set "%%c2=!a:-=!"
) else (
if !%%c2! lss 0 set /a %%c2+=100,%%c1-=1
if !%%c2! geq 100 set /a %%c2-=100,%%c1+=1
for %%a in (h2 c2) do (
set "a=!%%a!"&set "a=!a:-=!"&set %%a=!a!
if !a! lss 10 set "%%a=0!a!"
echo 其和为:%h1%.%h2%&echo 其差为:%c1%.%c2%&set /a n=0
if defined flag set ".1="&set ".2="
for %%a in (1 2) do (
if "!_%%a!" equ "0" (
set "_%%a="&set /a .%%a=1!.%%a!%%100
) else (
if "!_%%a!" equ "-0" (
set "_%%a=-"
) else (
set "a=!_%%a!"&set "a=!a:-=!"
set /a n+=1&call :lp
if "!.%%a!" neq "" set "a=!.%%a!"&call :loop&set ".%%a=!a!"
set /a num1=3-num_1+num_2,m=4-v
if defined flag set /a m=0
if %num1% leq 0 set /a num1=2
for /l %%a in (1,1,%num1%) do set /a num*=10
set /a s=%_1%%.1%*num/%_2%%.2%,j=%_1%%.1%*%_2%%.2%
if %j% lss 0 set "j=%j:-=%"&set "jk=-"
if %s% lss 0 set "s=%s:-=%"&set "sk=-"
if %m% neq 0 if "!j:~%m%!" equ "" set "j=0000%j%"&set "j=!j:~-%m%!"
if %s% lss %num% set "s=%num:~1%%s%"&set "s=!s:~-%num1%!"
if %m% neq 0 set "j=!j:~,-%m%!.!j:~-%m%,2!"
set "s=!s:~,-%num1%!.!s:~-%num1%,2!
for %%a in (s j) do (
set "a=!%%a!"
if "!a:.=!" equ "!a!" (
set "%%a=!%%a!.00"
) else (
if "!a:~-2,1!" equ "." set "%%a=!%%a!0"
if "!a:~,1!" equ "." set "%%a=0!%%a!"
echo 其积为:%jk%%j%&echo 其商为:%sk%%s%
pause>nul&goto :eof
set /a num_%n%+=1&set "a=%a:~1%"
if defined a goto lp
goto :eof
:loop
if "%a:~-1%" equ "0" set "a=%a:~,-1%"&set /a v+=1&goto loop
set StrOld=http://bbs.bathome.net
set StrNew=%StrOld:bbs=www%
把字符串 bbs 删掉
set StrOld=http://bbs.bathome.net
set StrNew=%StrOld:bbs=%
把第一字符 t 及其左侧所有字符全部删掉(统配符 * 的使用)
set StrOld=http://bbs.bathome.net
set StrNew=%StrOld:*t=%
待替换的子字符串不区分大小写(可以利用这个特性实现字母大小写转换)
@echo off
setlocal enabledelayedexpansion
REM 全部转换成大写字母
set str=http://bbs.BATHOME.net
set up=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
for %%i in (%up%) do (
set str=!str:%%i=%%i! )
echo %str%
pause
全部转换成小写字母
@echo off
setlocal enabledelayedexpansion
REM 全部转换成小写字母
set str=http://bbs.BATHOME.net
set low=a b c d e f g h i j k l m n o p q r s t u v w x y z
for %%i in (%low%) do (
set str=!str:%%i=%%i! )
echo %str%
pause
七、set命令知识点(1)把命令结果赋值给变量
UNIX/Linux Shell 支持命令替换 Windows BAT 借助重定向或者for语句来实现