1

I have files:
y:\a.txt
y:\b.txt

I have x.bat:

@echo off  
y:  
cd \  
for /f %%f in ('dir /b y:\*.txt') do (  
set content=  
set /p content=<y:\%%f  
echo y:\%%f  
echo content=%content%  
)

and the result:

y:\a.txt  
content=  
y:\b.txt  
content=  

Why does not contain %content% variable the first lie of the text files? The files are not empty.

gkovacs
  • 11
  • 3
  • 2
    You need delayed expansion to use a variable within a loop – foxidrive Jan 25 '14 at 11:02
  • possible duplicate of [Windows Batch Variables Won't Set](http://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set) – Joey Jan 25 '14 at 11:21
  • No. It does not work if I put setlocal enabledelayedexpansion at the start. Ruselts are the same... – gkovacs Jan 25 '14 at 12:03
  • 3
    You don't understand how to used delayed expansion. You also need to use this line instead, as the `!` marks are how delayed expansion is utilised: `echo content=!content!` and to send a message to someone you have to use @foxidrive for example somewhere in the comment. You get them automatically but I didn't know you had replied. – foxidrive Jan 25 '14 at 13:26

1 Answers1

0

Modify your batch script as follows:

@echo off  
setlocal enableDelayedExpansion
y:\
cd \
set content=
for /f %%f in ('dir /b *.txt') do (  
set /p content=<%%~dpff
echo file:%%~dpff , content:!content!
)

EDIT. Note the ~dpf to get the file path. As @foxidrive has mentioned it wouldn't have worked without a proper path.

The output is along the lines of:

file:y:\a.txt , content:lorem
file:y:\b.txt , content:ipsum
file:y:\c.txt , content:Ocaml, Haskell
mockinterface
  • 14,452
  • 5
  • 28
  • 49