批量变量无法保存全文文件

时间:2015-09-03 02:59:05

标签: batch-file

我正在尝试创建一个批处理文件,该文件将从.txt文件中删除一些特殊字符并将其附加到另一个文件,但该变量无法在文本文件中保存完整的100万个++字。

有没有办法让变量保存所有单词或至少将其拆分成组/集?

文字看起来像这样

{"One:1","two:2","three:4","four:3","five:5","EG:[512]","sets:{559,212,333,940}"};{"One:9","two:3","three:2","four:1","five:6","EG:[513]","sets:{551,215,331,944}"};...

到目前为止,我已尝试使用FOR循环将其附加到许多不同的文件,然后使用goto遍历每个文件,但它只是不会像它应该的那样工作。

1 个答案:

答案 0 :(得分:1)

已修改 - 底部的上一个答案。正如jeb评论这更快,不确定它是否可以使用但更快(但是,当然,比以前的代码更快是一件容易的事)

@echo off
    setlocal enableextensions disabledelayedexpansion

    <"in.txt" >"out.txt" call :process
    goto :Eof

:process
    set /p "buffer=" || goto :eof
    set "buffer=%buffer:"=%"
    set "buffer=%buffer:,= %"
    set "buffer=%buffer:{=%"
    set "buffer=%buffer:}=%"
    set "buffer=%buffer:[=%"
    set "buffer=%buffer:]=%"
    set "buffer=%buffer:;=%"
    set "buffer=%buffer::=%"
    set "buffer=%buffer:0=%"
    set "buffer=%buffer:1=%"
    set "buffer=%buffer:2=%"
    set "buffer=%buffer:3=%"
    set "buffer=%buffer:4=%"
    set "buffer=%buffer:5=%"
    set "buffer=%buffer:6=%"
    set "buffer=%buffer:7=%"
    set "buffer=%buffer:8=%"
    set "buffer=%buffer:9=%"

    <nul set /p "=%buffer%"
    goto :process

警告:这不应该写。它非常慢。

一旦说完,

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem File configuration
    set "inputFile=data.txt"
    set "outputFile=out.txt"

    rem Variable to hold a cariage return used to show that the 
    rem script is still working
    set "CR="
    for /f %%c in ('copy /Z "%~f0" nul') do if not defined "CR" set "CR=%%c"

    rem A temporary file will be used.
    for %%t in ("%temp%\%~nx0.%random%%random%.tmp") do (

        echo Splitting input file into temporary file
        > "%%~ft" (
            (   %= Split the input file into one character per line =%
                cmd /u /q /c"type ""%inputFile%""" | cmd /a /q /c"find /v "" " 
                %= Ensure we have a terminator to empty buffer (keep reading...) =%
                <nul set /p"=;"

            )|( %= Remove non needed characters =%
                findstr /i /r /c:"^[a-z,;]"
            )
        ) 
        echo Splitting done

        echo Starting to read temporary file
        echo %time% Here we go ...
        set "buffer=@"
        rem All data to stdout will be placed into the output file
        > "%outputFile%" (
            for /f usebackq^ delims^=^ eol^= %%a in ("%%~ft") do (

                rem Concatenate one character to the buffer
                setlocal enabledelayedexpansion
                for /f delims^=^ eol^= %%b in ("!buffer!") do (
                    endlocal
                    set "buffer=%%b%%a"
                )

                rem We will use the semicolon as a delimiter to do 
                rem partial processing of the input data
                if "%%a"==";" (

                    rem Execute the required processing on the buffer
                    call :processSection buffer

                    setlocal enabledelayedexpansion
                    < nul (
                        rem Write the processed buffer to stdout
                        set /p "=!buffer!"
                        rem Show we are still working
                        >con set /p "=!time! ... still working ... !CR!"
                    )
                    endlocal

                    set "buffer=@"
                )
            )
        )
        rem Processed temporary file can be removed
    ) & del "%%~ft"

    echo(
    echo %time% ... Done
    goto :eof


:processSection varName
    setlocal enableextensions disabledelayedexpansion

    rem Retrieve the data from the indicated variable
    setlocal enabledelayedexpansion
    for /f delims^=^ eol^= %%a in ("!%~1!") do (
        endlocal 
        set "line=%%a"
    )
    rem The passed buffer includes an initial filler character
    set "line=%line:~1%"

    rem Process buffer
    set "line=%line:,= %"
    set "line=%line:;= %"
    set "line=%line:  = %"

    rem Return buffer to the caller
    endlocal & set "%~1=%line%"
    goto :eof

它只是为了测试而写的,但请使用任何其他东西。