How can I load the contents of a text file into a batch file variable?

前端 未结 6 1731
小鲜肉
小鲜肉 2020-12-07 19:12

I need to be able to load the entire contents of a text file and load it into a variable for further processing.

How can I do that?


Here\'s what I di

6条回答
  •  长情又很酷
    2020-12-07 19:50

    Can you define further processing?

    You can use a for loop to almost do this, but there's no easy way to insert CR/LF into an environment variable, so you'll have everything in one line. (you may be able to work around this depending on what you need to do.)

    You're also limited to less than about 8k text files this way. (You can't create a single env var bigger than around 8k.)

    Bill's suggestion of a for loop is probably what you need. You process the file one line at a time:

    (use %i at a command line %%i in a batch file)

    for /f "tokens=1 delims=" %%i in (file.txt) do echo %%i
    

    more advanced:

    for /f "tokens=1 delims=" %%i in (file.txt) do call :part2 %%i
    goto :fin
    
    :part2
    echo %1
    ::do further processing here
    goto :eof
    
    :fin
    

提交回复
热议问题