two for loops not working

Dell PRECISION
October 7, 2009 at 06:01:29
Specs: win xp 5.1.2600, 3mhz/2 gig
dir /b> directory.txt

type nul> result.txt
set counter=1
setlocal enabledelayedexpansion
for /f %%a in (directory.txt) do (
set filename=%%a
for /f %%A in (!filename!) do (
set firstline=%%A
if %counter% GEQ 2 goto :SKIPPED
echo !filename! !firstline!> result.txt
set /a counter +=1
:SKIPPED
)
)


See More: two for loops not working

Report •


#1
October 7, 2009 at 06:20:43
It is forbidden to code GOTOs statements that refer to labels defined inside a For loop. Use the If ... (statements) else (statements) instead.

Issue covered a billion times in this Forum.


Report •

#2
October 7, 2009 at 06:32:54
Sorry all.
Thank you IVO

Report •

#3
October 8, 2009 at 01:59:16
Hi all, new development!
The following code works, but doesn't read all the files out of the "directory.txt" The files it isn't reading look like "__Filename1.txt" I think the "_" are the problem. Could anyone help me adjust the code? What I am trying to do here is create a directory of files in a folder, and display the filename and first line of contents into a result file.
Cheers
::================================
@echo off

type nul> directory.txt
type nul> result.txt

dir /a-d /x /b> directory.txt

setlocal enabledelayedexpansion
for /f "tokens=* delims= " %%a in (directory.txt) do (
set filename=%%a
set counter=1
for /f "tokens=* delims=" %%b in (!filename!) do (
set firstline=%%b
if !counter! LEQ '1' (
echo !filename! --- !firstline!>> result.txt
set /a counter +=1
)
)
)
start result.txt


Report •

Related Solutions

#4
October 8, 2009 at 02:25:46
I don't see why there should be any problem with underscores, but I think there may be a problem if you have any files with spaces in them. I know you've done a dir/x which is supposed to give you 8.3 short filenames without spaces, but the /b switch seems to turn off the effect of the /x switch so you end up with long filenames again.

Try enclosing in quotes:

for /f "usebackq delims=" %%b in ("!filename!") do (


Report •

#5
October 8, 2009 at 02:31:18
Here's another way to accomplish the task.

type nul> directory.txt
type nul> result.txt

dir /a-d /x /b> directory.txt

setlocal enabledelayedexpansion
for /f "tokens=* delims= " %%a in (directory.txt) do (
    set /p firstline=<"%%a"
    >> result.txt echo %%a --- !firstline!
)
start result.txt


Batch Variable how to


Report •

#6
October 8, 2009 at 02:42:57
Judago, thank you. Your code worked perfectly.

Klint, I did try that but the result was suprisingly the ("filename" --- "filename") instead of ("filename" --- "first line of file"). Thank you for looking at this though.

Have a good day all.


Report •

#7
October 8, 2009 at 04:23:52
Have you used USEBACKQ as shown in my code? Without it, "filename" will be treated as a literal string instead of a file name.

Report •

#8
October 8, 2009 at 05:18:33
Klint I apologise for that. Weh I was looking at the replies, I don't know what I did. I misread it completely and didn't try what you suggesteed. In fact I think I looked at my own post. (no excuses there).

I tried it now and it does exactly what I wanted as well. Thank you. Again, sorry.

Cheers


Report •

#9
October 8, 2009 at 06:36:59
Actually, the confusion may have been my fault. Let me come clean and confess. When I first posted my solution, I had forgotten the USEBACKQ option. After submitting my reply I looked at it and was horrified at my mistake and immediately edited it to add USEBACKQ. You may have looked at it before I corrected it. So the apologies are mine.

Report •

#10
October 8, 2009 at 06:40:20
hahaha!! One of those days!!! Klint, thanks again for your help, you guys have helped me with quite a few problems in the past. I appreciate it.

Report •


Ask Question