返回列表 发帖
本帖最后由 aloha20200628 于 2024-7-23 14:37 编辑

回复 60# ppll2030

我说的过滤掉中英文空格+制表符构成的 ‘全空白行’,其中的中文空格即全角空格。delims表达式指定的这三个字符复制到代码区中有时会 ‘变形’ 不过知道此意你在自己的代码中调整就是了

TOP

本帖最后由 77七 于 2024-7-23 15:52 编辑

回复 57# ppll2030


   
去掉就行,提供两种方法...
@echo off
set "str=    1  2 3 *  ?    "
set "str=x %str%"
set "str=%str: =&echo;%"
for /f "skip=1" %%a in ('echo %%str%%') do (
echo [%%a]
)
pause
for /f "tokens=*" %%a in ("    1  2 3 *  ?    ") do (
set str=%%a
if defined str (
call set "str=%%str: =&echo;%%"
for /f %%a in ('call echo %%str%%') do (
echo [%%a]
)
)
)
pauseCOPY
bat小白,请多指教!谢谢!

TOP

本帖最后由 qixiaobin0715 于 2024-7-23 15:54 编辑

回复 59# ppll2030
1.排版美不美观个人看法不一,只要达到各列上下对齐的效果,调整列的宽度就是小kiss,增加或减少几个空格而已;
2.如果列数都懒得搞清楚,就太那个了吧。实际上要解决这个问题也不复杂,就是在原有代码外层加上goto循环,再if几下即可。个人感觉goto语句太过难看,很少使用此循环。

TOP

算字节长度其实也可以将文本转换为16进制字串进行计算和处理,而且不用担心任何特殊字符。
@echo off&setlocal enabledelayedexpansion
set "input=a.txt"&set /a m=line=1&set "act=set/p=,&set/a m=1"
for %%i in ("%input%") do fsutil file creATenew "%input%.zero" %%~zi >nul
(for /f "tokens=2" %%a in ('fc /b "%input%" "%input%.zero"^|findstr /irc:"[0-9A-F]*: [0-9A-F][0-9A-F] 00"') do (
    if /i "%%a"=="0A" (echo,&set/a m=1,line+=1,col=0) else if /i "%%a"=="0D" (echo,
        for %%l in (!line!) do for /l %%i in (1,1,!col!) do (
            if !C%%i_%%l! gtr !C%%imax! set/a C%%imax=C%%i_%%l
            set "C%%i_%%l="
            ) ) else if /i "%%a"=="20" (%act%) else if /i "%%a"=="09" (%act%) else (
                    set/p=%%a
                    set /a col+=m,m=0
                    set /a C!col!_!line!+=1
                   )
        ))<nul >"%input%.temp"
(for /f "delims=" %%a in (%input%.temp) do (
    set /a col=0
    for %%i in (%%a) do (
        set /a col+=1&set "space="
        set /a size=C!col!max,len=size*2+2
        for /l %%i in (1,1,!size!) do set "space=!space!20"
        set "outstr=%%i!space!"
        for %%i in (!len!) do echo,!outstr:~0,%%i!
        )   
    set /p =0d0a
)   )<nul >"out_%input%"
del /q "%input%.zero" "%input%.temp"
certutil -decodehex -f "out_%input%" "out_%input%" >nul
pauseCOPY
1

评分人数

TOP

嵌套变量延迟开关, 来消除文本中“!”的影响,弃用for %%i in (...) do...的方法,通吃文本中存在默认分隔符以及*?通配符等字符的影响,使用大多数人熟悉的cmd命令书写代码。感觉效率还可以:
@echo off
echo,>$1
:a
set Em=
set /a colsn+=1
if %colsn% equ 1 (
    (for /f "tokens=1" %%i in (a.txt) do (echo,%%i&set Em=1))>temp
) else (
    (for /f "tokens=1,%colsn%" %%i in (a.txt) do (echo,%%j&if not "%%j"=="" set Em=1))>temp
)
if not defined Em goto :b
echo,>>temp
findstr /n /o .* temp>$2
setlocal enabledelayedexpansion
    for /f "tokens=1,2 delims=:" %%i in ($2) do (
        set /a m=%%j-x
        set /a n=%%i-1
        set "a!n!=!m!"
        if !max! lss !m! set "max=!m!"
        set "x=%%j"
    )
set /a n+=1
    (for /f "delims=:" %%i in ('findstr /n .* temp') do (
        set str1=
        set str2=
        set /p str1=
        set /a z=max-a%%i
        for /l %%k in (1,1,!z!) do set str2=!str2!
        if %%i neq !n! echo,!str1!!str2!
    ))<temp>$2
    (for /f "tokens=1* delims=:" %%i in ('findstr /n .* $2') do (
        endlocal
        set /p Line1=
        set Line2=%%j
        setlocal enabledelayedexpansion
        if "!Line1!"=="" (echo,!Line2!) else echo,!Line1!    !Line2!
    ))<$1>b.txt
type b.txt>$1
endlocal
if defined Em goto :a
:b
del temp $1 $2
pauseCOPY

TOP

返回列表