Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 EnableDelayedExpansionfor /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.

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

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.

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.

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.

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.

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 EnableDelayedExpansionFor /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

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.

![]() |
![]() |
![]() |

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