1

I have a file "file.txt" which contains the output of "dir /s /b *.c"

I want to write this whole content of file.txt in a single variable .

Any ideas?

Virendra Kumar
  • 947
  • 2
  • 13
  • 31

4 Answers4

5

The usual way to treat questions like this one is to reply: "What do you want this for?". However, your question is pure and simple, so here is the answer. The Batch file below not just store the contents of file.txt in a single variable, but it also later process the variable value as individual lines.

EDIT: I added the method to extract individual lines from the variable value as substrings.

@echo off
setlocal EnableDelayedExpansion

rem Create variables with LF and CR values:
set LF=^
%empty line 1/2, don't remove%
%empty line 2/2, don't remove%
for /F %%a in ('copy /Z "%~F0" NUL') do set CR=%%a

rem Store the contents of file.txt in a single variable,
rem end each line with <CR><LF> bytes
set "variable="
for /F "delims=" %%a in (file.txt) do (
   set "variable=!variable!%%a!CR!!LF!"
)

rem 1- Show the contents of the variable:
echo !variable!

rem 2- Process the contents of the variable line by line
echo/
set i=0
for /F "delims=" %%a in ("!variable!") do (
   set /A i+=1
   echo Line !i!- %%a
)

rem Get the starting position and length of each line inside the variable
set /A i=0, lastStart=0
for /F "delims=:" %%a in (
      '(cmd /V:ON /C set /P "=^!variable^!" ^& echo/^) ^<NUL ^| findstr /O "^^"'
   ) do (
   set /A len[!i!]=%%a-lastStart-2, i+=1
   set /A start[!i!]=%%a, lastStart=%%a
)
set "len[0]="
set "start[%i%]="
set /A lastLine=i-1

rem 3- Extract individual lines from the variable contents as substrings
:getNumber
   echo/
   set "num="
   set /P "num=Enter line number (nothing to end): "
   if not defined num goto end
   if %num% gtr %lastLine% echo Invalid number & goto getNumber
   for /F "tokens=1,2" %%i in ("!start[%num%]! !len[%num%]!") do (
      echo Line %num%- !variable:~%%i,%%j!
   )
goto getNumber
:end

You must note that Batch variables can only store a maximum of 8K characters.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks, It worked for me, though i needed just the half of it. – Virendra Kumar Apr 11 '15 at 12:40
  • Ok. The logical question now: What do you want this for? It is _very_ rare to store more than one line in a variable in Batch files! `;)` – Aacini Apr 11 '15 at 17:22
  • I made a executable from a c program which takes input as file names from command line and does some operation. The problem that I faced was the "Executable file.exe" that I made should accept all the file names from all the sub directories. The simple for loop solution was to run the executable every time I read a line from files.txt,But in that way it would run the executable again for every line.My need was to run the exe only one time with all the lines as command line arguments at a time. So i needed to store every line in one variable. – Virendra Kumar Apr 12 '15 at 06:20
  • Takes your .exe program a multi-line value from _one_ argument? That is very uncommon! Did this method solved your problem? – Aacini Apr 12 '15 at 12:59
1

No. But you can go through the lines one by one.

for /f "delims=" %A in (file.txt) do echo %A

Maybe say what you are trying to achieve. Knowledge of C won't help you in batch because it's contary for historical reasons.

Serenity
  • 311
  • 2
  • 3
1

You can use set /p:

set /p foo=<file.txt

See also this question.

Community
  • 1
  • 1
Helios
  • 86
  • 5
  • Oops, yeah, looks like a for loop [like this](http://stackoverflow.com/a/16116676/3418332) is what you need. – Helios Apr 11 '15 at 03:37
1

Batch-Script is a very limited tool with a primitive support for multi-line variables (with specific hacks that will not work as expected for this sceneario but if you are interested see this).

The only reasonable solution is to move to a capable language, which is every else less Batch-Script.

Community
  • 1
  • 1
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417