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

前端 未结 6 1727
小鲜肉
小鲜肉 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 20:02

    Create a file called "SetFile.bat" that contains the following line with no carriage return at the end of it...

    set FileContents=
    

    Then in your batch file do something like this...

       @echo off
       copy SetFile.bat + %1 $tmp$.bat > nul
       call $tmp$.bat
       del $tmp$.bat
    

    %1 is the name of your input file and %FileContents% will contain the contents of the input file after the call. This will only work on a one line file though (i.e. a file containing no carriage returns). You could strip out/replace carriage returns from the file before calling the %tmp%.bat if needed.

提交回复
热议问题