1.

Solve : For each?

Answer»

Does anyone know how to do in a loop certain actions for each line there is in a text file.

For EXAMPLE
textfile:
one
two
three
dunnow

for each line, add a number near the word
converted text file
one1
two2
three3
dunnow4


thanks in advanceThis snippet is down and dirty. Actually it's coded specifically for the data presented and is not a generic solution.

Code: [Select]@echo off
setlocal enabledelayedexpansion
set count=0
for /f %%x in (input.txt) do (
call set /a count=%%count%%+1
echo %%x!count! >> output.txt
)

Quote from: Sidewinder on January 03, 2008, 03:36:51 AM

This snippet is down and dirty. Actually it's coded specifically for the data presented and is not a generic solution.

Code: [Select]@echo off
setlocal enabledelayedexpansion
set count=0
for /f %%x in (input.txt) do (
call set /a count=%%count%%+1
echo %%x!count! >> output.txt
)


That worked perfectly sidewinder. I'm glad to see that you're still on these forums and so helpfull towards others. I haven't been here in a long long time, how are you HOLDING up?

EDIT: another quick question: how do I make that I see in the begin from the output (so before the loop) I see a total of all the lines in the other file (plus one) (=the count of the last line + one).
for example there are five lines
lines=6
(what the LOOPS give you)
..Hey Blackberry, GOOD to see your posts again. You should have stuck around, the regulars all got 10% pay increases (you do the math).

Numbers sort before alpha, so using your data simply sorting the file is one solution:

Code: [Select]@echo off
setlocal enabledelayedexpansion
set count=0
for /f %%x in (input.txt) do (
call set /a count=%%count%%+1
echo %%x!count! >> output.txt
)
set /a count=%count%+1
echo %count% >> output.txt
sort output.txt > final.txt

A more generic solution would be to force the count to be the first record no matter what the data:

Code: [Select]@echo off
setlocal enabledelayedexpansion
set count=0
for /f %%x in (input.txt) do (
call set /a count=%%count%%+1
echo %%x!count! >> output.txt
)
set /a count=%count%+1
echo %count% >> output1.txt
copy output1.txt+output.txt final.txt

You may want to delete some of the intermediate files.



PS. I hope this is what you wanted. I had a dickens of a time figuring out exactly what you needed.Hi! Thanks a lot for your help, my final script is now:

Code: [Select]@echo off
setlocal enabledelayedexpansion
set count=0
for /f %%x in (input.txt) do (
call set /a count=%%count%%+1
echo [link!count!] >> temp.txt
echo URL=%%x >> temp.txt
)
set /a count=%count%+1
>>links.txt echo [Info]
>>links.txt echo Links=%count%
for /f %%a in (temp.txt) do @echo %%a >> links.txt
del temp.txt

I use it because I have a program rapidget that auto downloads files from rapidshare. But you can only add one link at the time in the GUI from the program. As seen I have everyday 100+ links, that was really a big waste of time. Fortunally, the added links in the GUI are saved in a config files (links.txt). Thanks to your help I can now create that config file in one click accept by clicking add link, add link one by one in the GUI.

Thanks for your help


Discussion

No Comment Found