2

我试图将文件拆分为 8159 字节长的连续页面。我如何读取文件的 8159 字节并保存到全部!计数!变量?如果文件为 8159 或更少,它会读取文件并将其设置为 !all! 多变的。我如何在 :split 标签中只读取这么多字节并保存到变量中。

@echo off
setlocal EnableDelayedExpansion EnableExtensions
for /f "tokens=*" %%a in ("newhtml.html") do set FileSize=%%~za
echo FileSize is %FileSize% bytes
if %FileSize% GTR 8159 goto split

SETLOCAL DisableDelayedExpansion 
set "all="
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ newhtml.html"`) do (
    set "line=%%a"
    SETLOCAL EnableDelayedExpansion
    set "line=!line:#=#S!"
    set "line=!line:*:=!"
    for /F "delims=" %%p in ("!all!#L!line!") do (
        ENDLOCAL
        set "all=%%p"
    )
)
SETLOCAL EnableDelayedExpansion
if defined all (
set "all=!all:~2!"
set ^"all=!all:#L=^
[blank line, remove this comment]
[blank line, remove this comment]   
!"
set "all=!all:#S=#!"
)
echo the all variable is: !all!
goto end

:split
set count=0
set /a all_sub=%FileSize% / 8159
set /a all_rem=%FileSize% %% 8159
if %all_rem% NEQ 0 set /a all_ttl=%all_sub% + 1
echo %all_sub% full page(s), %all_rem% bytes(s) leftover, %all_ttl% total pages

for  %%a in ("newhtml.html") do (
    set /a count=count + 1
    echo Read 8159 bytes from this file newhtml.html, save to all!count!
    if !count! EQU %all_ttl% echo All done & goto end
)

goto end

:end

需要帮助的部分是拆分标签,在读取文件的for循环中,如何一次只获取8159字节的数据并写入顺序变量。我想我应该做!所有!一个函数然后调用它?

编辑:我找到了这个文件(http://www.fourmilab.ch/splits/)来进行拆分,将其添加到 ALL 例程以重新组装的简短工作。非常感谢dbenham和jeb!

4

1 回答 1

2

为什么不简单地将文件读入数组
如果没有任何行超过 ~8190 个字符,则每一行将是一个有效的条目。
然后你不需要换行等的替换技巧。

但这取决于您的实际问题。

@echo off
SETLOCAL DisableDelayedExpansion
set "all="
set count=0
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ aux1.txt"`) do (
    set "line=%%a"
    set /a count+=1
    SETLOCAL EnableDelayedExpansion
    set "line=!line:*:=!"
    for /F "delims=" %%p in (^"set "array[!count!]=!line!"^") do (
        ENDLOCAL
        %%p
    )
)

SETLOCAL EnableDelayedExpansion
for /L %%n in (1 1 %count%) do (
  echo Line%%n:!array[%%n]!
)
于 2012-05-17T20:13:22.087 回答