1

This is my first experience in batch script, i am trying to read text file content and trying to set its content in single variable i am using .bat file to run script but script not working.

i want to set all content of file in single variable.
tried most of example but failure.

below is my script which i am trying

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

This is my Text File

enter image description here

And
below result is showing

enter image description here

i want it in single variable

M.Y.Mnu
  • 718
  • 8
  • 22

3 Answers3

3
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

Mind that the max length of a string you can assign to a variable is 8191 symbols. Also some special symbols could break the script above (%,! ..)

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

i want to set all content of file in single variable.

In batch, the only way is to construct a multi-line variable yourself, named !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

The FOR /F tokens^=*^ delims^=^ eol^= disables eol by escaping every delimiter. Warning: "eol=" DOES NOT disable eol, but sets the quote " as the end-of-line character!


Disadvantages:

  • SLOW: On my machine, it takes 2 seconds for a 2000 lines file, printing the variable excluded
  • The line ending style (\n or \r\n is ignored, and always set to \n)
  • Additional \n at the beginning of variable

Advantages:

  • SAFE: Always works for any file with printable ASCII characters (0x20 ~ 0x7E)
ScriptKidd
  • 803
  • 1
  • 5
  • 19
0

You can also use @dbenham's CERTUTIL method. It is foolproof against <CR> <LF> ! ^...
See more tricks with certutil for the "poorly documented" switches explained in depth.

@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!
ScriptKidd
  • 803
  • 1
  • 5
  • 19