从第一行中提取总列数复制代码
- @echo off&setlocal enabledelayedexpansion
- set "tab=#"&rem 把左边的#号替换为 tab 键
- for /f "delims=" %%a in (ss.txt) do (
- set "str=%%a"
- set "str=!str:%tab%%tab%%tab%%tab%=%tab%0%tab%0%tab%0%tab%!"
- set "str=!str:%tab%%tab%%tab%=%tab%0%tab%0%tab%!"
- set "str=!str:%tab%%tab%=%tab%0%tab%!"
- if "!str:~-1!"=="%tab%" set "str=!str:~0,-1!%tab%0"
- echo !str!
- )
- pause
复制代码
- @echo off&setlocal enabledelayedexpansion
- set "tab= #"&rem 把左边的#号替换为 tab 键
- set /p var=<ss.txt
- for %%a in (!var!) do (
- set /a n+=1
- for /l %%i in (1 1 !N!) do set t=!t!%tab%&set tt=!tt!%tab%0
- set t!n!=!t!&set t!n!!n!=!tt:~0,-1!
- set t=&set "tt="
- )
- set /a n-=1
- for /f "delims=" %%a in (ss.txt) do (
- set "str=%%a"
- for /l %%i in (!n! -1 2) do (
- for /f "tokens=1,2 delims=/" %%j in ("!t%%i!/!t%%i%%i!") do (
- set "str=!str:%%j=%%k!"
- ))
- if "!str:~-1!"=="%tab%" set "str=!str:~0,-1!%tab%0"
- echo !str!
- )
- pause
复制代码
- @echo off&setlocal enabledelayedexpansion
- set Tab= &rem 这个空白换为Tab键
- for /f "delims=" %%a in (a.txt) do (
- set var=%%a
- set str=
- for %%b in ("!var:%Tab%=" "!") do (
- set #%%~b=%%~b
- set #=0
- set str=!str!%Tab%!#%%~b!
- )
- echo !str:~1!
- )
- pause
复制代码
- @echo off & SetLocal enabledelayedexpansion
- :: tab 键
- set "tab= "
- For /f "skip=1 delims=" %%a in (a.txt) do (
- set "sString=%%a" & set "sVar="
- set "sString=!sString:%tab%=%tab%#!"
- set /a n = 0 & call :recur !sString:~1!
- )
- set _ & Pause & exit /b
- :recur
- set /a n += 1
- For /f "tokens=1* delims=%tab%" %%a in ("%*") do (
- set "sVar=%%a" & set "sVar=!sVar:#=!"
- If "!sVar!" equ "" ( set "sVar=0" )
- set /a _!n! += sVar
- If "%%b" neq "" ( call :recur %%b )
- )
改成这样就不畏惧任何字符了:复制代码
- @echo off&setlocal enabledelayedexpansion
- set Tab= &rem 这个空白换为Tab键
- for /f "delims=" %%a in (a.txt) do (
- set var=%%a
- set var=!var:%Tab%=%Tab%#!%Tab%
- set var=!var:#%Tab%=0%Tab%!
- set var=!var:#=!
- echo !var:~,-1!
- )
- pause
复制代码
- @echo off
- set Tab= &rem 这个空白换为Tab键
- for /f "delims=" %%a in (a.txt) do (
- set var=%%a
- setlocal enabledelayedexpansion
- set var=!var:%Tab%=%Tab%#!%Tab%
- set var=!var:#%Tab%=0%Tab%!
- set var=!var:%Tab%#=%Tab%!
- echo !var:~,-1!
- endlocal
- )
- pause
复制代码
- @echo off&setlocal enabledelayedexpansion
- set "tab= " 引号里的空格换成 Tab
- for /f "delims=" %%a in (a.txt) do (
- set var=%%a
- set var="!var:%tab%="%tab%"!"
- set var=!var:""=0!
- echo !var:"=!
- )
- pause
复制代码
- @echo off&setlocal enabledelayedexpansion
- for /f "delims=" %%a in (a.txt) do (
- set var=%%a
- set n=0
- for %%a in ("!var:【Tab】=" "!") do set /a n+=1 , _!n!+=%%~a 2>nul
- )
- set _
- pause
复制代码
- @echo off
- set Tab= &rem 这个空白换为Tab键
- for /f "delims=" %%a in (a.txt) do (
- set var=%%a%Tab%
- setlocal enabledelayedexpansion
- set var=!var:%Tab%%tab%=%Tab%0%Tab%!
- set var=!var:%Tab%%tab%=%Tab%0%Tab%!
- echo !var:~,-1!
- endlocal
- )
- pause
复制代码
- @echo off&setlocal enabledelayedexpansion
- set "tab=#" rem请将#替换为tab键
- for /f "delims=" %%a in (a.txt) do (
- echo %%a&set /a m=0
- set "str=%%a"&set "str=!str:%tab%=%tab%0!"
- for %%b in (!str!) do (
- set /a m+=1
- set "var=%%b"&set "var=!var:~1!"
- if not defined var set /a var=0
- set /a _!m!+=!var!
- )
- )
- set "_1=合计"
- for /l %%a in (1,1,%m%) do (
- set /p=!_%%a!<nul
- if %%a neq %m% set /p=%tab%<nul
- )
- pause>nul
复制代码
- @echo off&setlocal enabledelayedexpansion
- set "Tab=#" 把 # 换成 Tab 键
- for /f "skip=1 delims=" %%a in (a.txt) do (
- set var=%%a
- set n=0
- for %%a in ("!var:%tab%=" "!") do set /a n+=1 , _!n!+=%%~a 2>nul
- )
- more a.txt
- for /l %%a in (1 1 %n%) do set end=!end!%tab%!_%%a!
- echo 合计%end%
- pause>nul
在表达式中的任何非数字字符串键作为环境变量
名称,这些环境变量名称的值已在使用前转换成数字。如果指定
了一个环境变量名称,但未在当前环境中定义,那么值将被定为
零。
复制代码
- @echo off
- set v=,1
- set /a v=v
- echo %v%
- pause 变量 v 的值变为 0
- set v=1,
- set /a v=v
- echo %v%
- pause 变量 v 的值变为 1
C:\Test>type test.bat @echo off&setlocal enabledelayedexpansion set "tab= " rem请将#替换为tab键 for /f "delims=" %%a in (a.txt) do ( echo %%a&set /a m=0 set "str=%%a"&set "str=!str:%tab%=%tab%0!" for %%b in (!str!) do ( set /a m+=1 set "var=%%b"&set "var=!var:~1!" if not defined var set /a var=0 set /a _!m!+=!var! ) ) set "_1=合计" for /l %%a in (1,1,%m%) do ( set /p=!_%%a!<nul if %%a neq %m% set /p= <nul ) pause>nul C:\Test>test.bat 姓名 第一项作业 第二项作业 第三项作业 第四项作业 甲 30 63 83 86 乙 27 93 98 22 丙 94 45 77 丁 14 94 78 戊 81 25 57 己 62 58 75 庚 70 49 10 47 辛 98 2 壬 95 10 81 12 癸 64 29 45 57 合计 35 46 44 24 |
有点意思。复制代码
- @echo off
- set m=1,2
- set /a n=%m%
- echo %n%
- set /a n=m
- echo %n%
- pause
复制代码
- set m=2+2
- set /a n=m
- set "2+2=1" 设置变量2+2
- set /a n=m
- pause
复制代码
- @echo off
- set v=bathome1
- set /a v=v
- echo %v%
- pause 变量 v 的值变为 0
- set v=1bathome
- set /a v=v
- echo %v%
- pause 变量 v 的值变为 1
- set v=1bathome
- set /a v=%v%
- echo %v%
- pause 而这样会出错
欢迎光临 批处理之家 (http://www.bathome.net/) | Powered by Discuz! 7.2 |