【问题标题】:How to read all content from text flie in single variable in Batch Script?如何从批处理脚本中单个变量的文本文件中读取所有内容?
【发布时间】:2018-05-13 01:36:40
【问题描述】:

这是我在批处理脚本中的第一次体验,我正在尝试读取文本文件内容并尝试将其内容设置在单个变量中我正在使用 .bat 文件来运行脚本但脚本不起作用。

我想在单个变量中设置文件的所有内容。
尝试了大部分示例,但失败了。

下面是我正在尝试的脚本

cd "C:\documents and settings\%USERNAME%\desktop"
for /f "delims=" %%x in (Test.txt) do set Build=%%x
pause >nul
exit

这是我的文本文件


以下结果显示



我希望它在单个变量中

【问题讨论】:

    标签: batch-file cmd window


    【解决方案1】:
    cd "C:\documents and settings\%USERNAME%\desktop"
    setlocal enableDelayedExpansion
    for /f "useback delims=" %%x in ("Test.txt") do set "Build=!build! %%x"
    echo %build%
    pause >nul
    exit
    

    请注意,您可以分配给变量的字符串的最大长度是 8191 个符号。 还有一些特殊符号可能会破坏上面的脚本 (%,! ..)

    【讨论】:

    • 是的,它的工作非常感谢你,我可以在特定位置用','逗号连接
    • @M.Y.Mnu - 是的,你可以。使用set "Build=!build! , %%x"
    【解决方案2】:

    我想在单个变量中设置文件的所有内容。

    批量,唯一的办法就是自己构造一个多行变量,命名为!LF!

    @echo off
    SETLOCAL EnableDelayedExpansion
    
    ::Change this
    set "file=%~f0"
    if NOT EXIST "%file%" exit /b 1
    
    ::Initialize & empty variables
    ( set LF=^
    %= 0x0D Form Feed =%
    )
    set "lines="
    set "data="
    
    ::Count lines
    FOR /F tokens^=*^ delims^=^ eol^= %%L in (
    '2^>nul findstr /N "^" "%file%"'
    ) do set /a "lines+=1"
    
    ::Read file using SET /P
    <"%file%" (
      for /L %%a in (1 1 %lines%) do (
        set "x=" %= Workaround for empty lines =%
        set /p "x="
        set "data=!data!!LF!!x!"
      )
    )
    
    ::Print data
    echo(!data!
    
    ENDLOCAL
    pause & exit /b
    

    FOR /F tokens^=*^ delims^=^ eol^= 通过escaping every delimiter 禁用eol警告"eol=" 禁用eol,但sets the quote " as the end-of-line character


    缺点:

    • SLOW:在我的机器上,一个 2000 行的文件需要 2 秒,打印排除的变量
    • 行尾样式(\n\r\n 被忽略,始终设置为 \n
    • 变量开头的附加\n

    优点:

    • SAFE:始终适用于任何具有可打印 ASCII 字符 (0x20 ~ 0x7E) 的文件

    【讨论】:

      【解决方案3】:

      您也可以使用@dbenham 的CERTUTIL method。对&lt;CR&gt; &lt;LF&gt; ! ^是万无一失的...
      请参阅more tricks with certutil 以深入了解"poorly documented" 开关。

      @echo off
      ====SETLOCAL EnableDelayedExpansion EnableExtensions
      
      
      set "B=^!"
      set "C=^"
      set ^"L=^
      %===Line Feed===%
      "
      for /F %%C in ('wmic qfe list') do set "R=%%C" Carriage Return
      
      
      >nul 2>&1 certutil -f -encodehex Test.txt "%temp%\hex.txt" 4
      
      
      pushd "%temp%"
      >expand.txt (
        for /f "delims=" %%A in (hex.txt) do for %%B in (%%A) do (
          set "char=%%B"
          REM ! --> !B!
          set "char=!char:21=21 42 21!"
          REM ^ --> !C!
          set "char=!char:5e=21 43 21!"
          REM <CR> --> !R!
          set "char=!char:0a=21 4c 21!"
          REM <LF> --> !L!
          set "char=!char:0d=21 52 21!"
          echo(!char!
        )
      )
      
      >nul 2>&1 certutil -f -decodehex expand.txt rawContent.txt
      for /f delims^=^ eol^= %%A in (rawContent.txt) do set "fileContent=%%A"
      
      
      del hex.txt expand.txt rawContent.txt
      popd
      
      
      echo(!fileContent!
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-05
        • 1970-01-01
        • 1970-01-01
        • 2011-03-05
        • 2011-12-04
        • 1970-01-01
        • 2010-09-13
        • 2015-11-02
        相关资源
        最近更新 更多