Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I have the next problem that i can't resolve:
I need to read a file in a batch file (.bat)execution. For each line of that file I need to execute a command. (Can anybody help me??
Regards Sergio.fisico

One can invoke another batch file with the CALL command. Or to just go to that other batch, just use it's name as a line.
Best

Hi!!
Sorry by i can not understand you.I am going to explain better my problem:
1) I have a batch file call 'init.bat' and i have other text file call 'del.txt'.
2) In 'del.txt' i store the file's name that i want to delete:
For example 'del.txt' could store:file1.txt
file2.txt
file3.txt3) I execute 'init.bat' and i want to read 'del.txt', after read each row i want to execute the command : delete <file>
4) then my execution should be:
- execute 'init.bat'
- for each row in del.txt execute
delete <each row>
end forMy questions are:
who can i read each line of my del.txt??
who can i store each line of my del.txt and then execute other command with that value??Kind Regards
Sergiofisico

About your first question
How (I suppose who is a typo) can I read each line of my del.txt (and execute a del command)?
the answer is
For /f "tokens=* delims=" %%a in (del.txt) Do Del %%a
About your other question that is not clear to me, please explain better what you mean.

Hello again!!
Your solution was fantastic for me. Thank you very much. (Forget my second question!!!)
But know i have other different problem:
I want to execute more than one command for each file's row.
In your answer (thank you again)
For /f "tokens=* delims=" %%a in (del.txt) Do Del %%a
I can only execute one command (del)
Is it posible to call a method with each row as a parameter and then execute differents commands ???
Example:
For /f "tokens=* delims=" %%a in (del.txt) CALL a method XXX
Where XXX is the method i want to use the value of each row)
command 1
command 2
...
command nIf i can not call a method, is it posible to execute more than one command???
Kind Regards.
fisico

Yes, it is possible and you have two choices
First - rewrite the For to execute a "block sequence", i.e.
For /f "tokens=* delims=" %%a in (del.txt) Do (
Command1 %%a
.......
CommandN %%a)where %%a is each row of your text file. A block sequence is a list of commands embedded between (...) each command holding one line..
Second - use an internal subroutine (much like a "method"), i.e.
For /f "tokens=* delims=" %%a in (del.txt) Do Call :Method %%a
.......
......-:Method
Command1 %*
.......
.......
CommandN %*
GoTo :EOFwhere :Method (with the leading :!) is the internal subroutine (Call on the same line holding For), %* is the row it refers (%%a in the For tail) and :EOF is the standard return point to not be declared as it is built in NT batch interpreter.
I hope I have explained in a clean way, if you need more help post again or mail me.

i have a question regarding this...i'm using a similar concept to make another script. But all the method calling etc is that done in the txt file or in the .bat file?

my example is like
cd\Documents and Settings\%user\Application Data\Microsoft\Outlook\
xcopy *.* x:\%user\outlook /ewhere %user comes from a list of users residing in user.txt file. so i need to read one user after the other..sort of in a while loop.
thanks.
Vishal

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

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