2014-01-30 90 views

回答

0

腳本會將「測試」回顯到file.txt,直到它大於2 MB。雖然批處理對於這樣的事情來說很慢......您可以在大塊中回顯更多文本。像echo.test試驗試驗試驗試驗試驗

@echo off 
setlocal enabledelayedexpansion 
set filename="file.txt" 
rem 2097152 bytes=2 MB 
set maxbytesize=2097152 

:lop 
for /F %%a IN (%filename%) do set size=%%~za 
if !size! LSS %maxbytesize% (
    echo.test>>%filename% 
    goto :lop 
) 
0

可惜的是,批次算術是有限的,使用該方法,以便最大文件長度被限制爲2^31-1字節文件(2GB)。無論如何,這應該是足夠快的可用性(2MB文件少於一秒)。

@echo off 
    setlocal enableextensions enabledelayedexpansion 

    set /a "size=1024*1024*2" 
    set "file=grow.txt" 

    set "tempFile=%temp%\%~nx0.%random%.tmp" 
    <nul set /p "x=." > "%tempFile%" 
    break > "%file%" 
    set "cmd=type" 
    for /l %%n in (1 1 31) do if defined size (
     if %%n equ 22 (set "cmd=findstr ""^"" ") 
     set /a "first=size & 1" & if !first! equ 1 !cmd! "%tempFile%" >> "%file%" 
     set /a "size>>=1"  & if !size! gtr 0 (!cmd! "%tempFile%" >> "%tempFile%") else set "size=" 
    ) 
    del "%tempFile%" >nul 2>nul 
    endlocal 

並與評論

@echo off 
    setlocal enableextensions enabledelayedexpansion 

    :: Configure size and file to generate 
    set /a "size=1024*1024*2" 
    set "file=grow.txt" 

    :: Prepare a 1 byte temporary file 
    set "tempFile=%temp%\%~nx0.%random%.tmp" 
    <nul set /p "x=." > "%tempFile%" 

    :: Initialize the output file 
    break > "%file%" 

    :: To move data, type command is used for the 21 first 
    :: rounds, as it is more "efficient" for small sizes as 
    :: it does not have the overhead of creating a new process. 
    :: From this point, findstr replaces type command. The 
    :: overhead of creating the process is negligible vs the 
    :: speed on data transfer. 
    set "cmd=type" 

    :: Generate the final file, checking the bits in the 
    :: indicated size to determine how and when to add 
    :: data to the file. 
    echo %time% 
    for /l %%n in (1 1 31) do if defined size (
     if %%n equ 22 (set "cmd=findstr ""^"" ") 
     set /a "first=size & 1" & if !first! equ 1 !cmd! "%tempFile%" >> "%file%" 
     set /a "size>>=1"  & if !size! gtr 0 (!cmd! "%tempFile%" >> "%tempFile%") else set "size=" 
    ) 
    echo %time% 

    :: Show generated file information 
    echo. 
    dir "%file%" | findstr /r /c:"^[0-9]" 
    echo. 

    :: cleanup 
    del "%tempFile%" >nul 2>nul 
    endlocal 

就根據這一概念的解釋同樣看到Ancient Egyptian Multiplication