Computing.Net > Forums > Programming > two for loops not working

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

two for loops not working

Reply to Message Icon

Name: lee123abc
Date: October 7, 2009 at 06:01:29 Pacific
OS: win xp 5.1.2600
CPU/Ram: 3mhz/2 gig
Product: Dell / PRECISION
Subcategory: Batch
Comment:

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
)
)



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: October 7, 2009 at 06:20:43 Pacific
Reply:

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.


0

Response Number 2
Name: lee123abc
Date: October 7, 2009 at 06:32:54 Pacific
Reply:

Sorry all.
Thank you IVO


0

Response Number 3
Name: lee123abc
Date: October 8, 2009 at 01:59:16 Pacific
Reply:

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


0

Response Number 4
Name: klint
Date: October 8, 2009 at 02:25:46 Pacific
Reply:

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 (


1

Response Number 5
Name: Judago
Date: October 8, 2009 at 02:31:18 Pacific
Reply:

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


1

Related Posts

See More



Response Number 6
Name: lee123abc
Date: October 8, 2009 at 02:42:57 Pacific
Reply:

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.


0

Response Number 7
Name: klint
Date: October 8, 2009 at 04:23:52 Pacific
Reply:

Have you used USEBACKQ as shown in my code? Without it, "filename" will be treated as a literal string instead of a file name.


0

Response Number 8
Name: lee123abc
Date: October 8, 2009 at 05:18:33 Pacific
Reply:

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


0

Response Number 9
Name: klint
Date: October 8, 2009 at 06:36:59 Pacific
Reply:

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.


0

Response Number 10
Name: lee123abc
Date: October 8, 2009 at 06:40:20 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: two for loops not working

file rename: 'for' loop not working www.computing.net/answers/programming/file-rename-for-loop-not-working/17415.html

Simple FOR Loop not working www.computing.net/answers/programming/simple-for-loop-not-working/20278.html

Two for loops at the same time? www.computing.net/answers/programming/two-for-loops-at-the-same-time/14085.html