Computing.Net > Forums > Windows 2000 > BATCH FILE: loop inside a for loop

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.

BATCH FILE: loop inside a for loop

Reply to Message Icon

Name: montython
Date: August 3, 2007 at 09:37:23 Pacific
OS: Windows 2000
CPU/Ram: Intel i686, 512 MB
Product: Dell
Comment:

Hello,

I am trying to create a batch file to do the following:

-Get each line from an input file input.txt
-Type each line to output.txt exactly as it is
-Process each line, type the part after the last / below the original text
-Repeat this for all the lines in the input file.

This is what I did:

-----
@echo off
setLocal EnableDelayedExpansion

for /f %%i in (input.txt) do (
set var=%%i
echo !var! >> output.txt

:loop
for /f "delims=/ tokens=1,*" %%i in ("!var!") do set var=%%j
if not !var!==!var:*/=! goto loop
echo !var! >> output.txt
)
-----

The problem is, it works for the first line only and then exits. Could anyone help?

Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: August 4, 2007 at 13:11:55 Pacific
Reply:

I just took a look at your script and noticed you use the same index (%%i) in either the inner and outer loop; so change the inner index with e.g. %%m and see if it works.

@echo off
setLocal EnableDelayedExpansion

for /f %%i in (input.txt) do (
set var=%%i
echo !var! >> output.txt

:loop
for /f "delims=/ tokens=1,*" %%m in ("!var!") do set var=%%n
if not !var!==!var:*/=! goto loop
echo !var! >> output.txt
)


0

Response Number 2
Name: montython
Date: August 5, 2007 at 13:36:10 Pacific
Reply:

Thanks for your interest IVO, but it doesn't work.

I tried everything I could think of, still I couldn't get this working. I am sure there is a way to do it, but simply I cannot figure out how...

It seems that I should try vbscript instead, though I would really prefer it as a batch file.


0

Response Number 3
Name: IVO
Date: August 5, 2007 at 15:15:35 Pacific
Reply:

Sorry I wasted your time, but as I said I just had a quick look at your code.

If you can wait another day, I'll examine carefully the script to find out the bug as it has to work, no doubt.


0

Response Number 4
Name: montython
Date: August 6, 2007 at 12:27:42 Pacific
Reply:

Actually, I tried this and many other things before you suggested. Until now, I was able to handle simple things in batch files in some way or another. But this time it is so annoying that I cannot solve it.

Of course, it is always possible to do these with sed scripts or VBScript. The thing is when you spend your time on something and not get it working (and you know that it can be done), you cannot stop thinking about it. :)

I appreciate your help. If you cannot fix it easily, don't spend your time on it. :) Thanks again.


0

Response Number 5
Name: IVO
Date: August 6, 2007 at 13:32:44 Pacific
Reply:

Today I got an unexpected hot call to service, here is why your solution is still delayed. Trust in me, I never leave the problems I take care of unresolved and this will not be the first.


0

Related Posts

See More



Response Number 6
Name: IVO
Date: August 6, 2007 at 14:31:49 Pacific
Reply:

Here your batch script; I tested it carefully and it worked fine, almost under my system.

The problem is due to the GoTo inside the For that breaks the main loop flow.

There are other minor enhancements to fix bugs in case a row holds no / separators.

@Echo Off > output.txt
SetLocal EnableDelayedExpansion

For /F "tokens=* delims=" %%i in (input.txt) Do (
Set Var=%%i
Echo !Var!>> output.txt
Call :LOOP
)
GoTo :EOF

:LOOP
For /F "tokens=1,* delims=/" %%m in ("!Var!") Do Set Var=%%n
If not defined Var GoTo :EOF
If not !Var!==!Var:*/=! GoTo :LOOP
Echo !Var!>> output.txt
GoTo :EOF


0

Response Number 7
Name: montython
Date: August 7, 2007 at 15:49:50 Pacific
Reply:

Thank you so much IVO. It works perfectly. And this is a good example that simple dos commands can be used to perform string operations.

After I saw your edits, I noticed the bug in the original script that would occur in case there are no / separators in a line: endless loop problem. Thanks for pointing out...

In fact, I will use this for handling URL's in a plain text file, so there should be / separators in all lines, but it is good to take every possibility into consideration. I will do more advanced string manipulation than the one I posted here. Now that I have the idea, I can handle the rest...

You are great! Thanks again.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Windows 2000 Forum Home


Sponsored links

Ads by Google


Results for: BATCH FILE: loop inside a for loop

Batch file to build a script www.computing.net/answers/windows-2000/batch-file-to-build-a-script/60897.html

GOTO command inside a FOR loop www.computing.net/answers/windows-2000/goto-command-inside-a-for-loop/65812.html

Batch files on w2k startup www.computing.net/answers/windows-2000/batch-files-on-w2k-startup/25243.html