1.

Solve : Output to a file?

Answer»

I am trying to output information from a batch file to a text file. I am using a batch file program which searches a single text file, line by line for certain names and if they exist, it outputs the name to the screen. I wish to also out put the name to ANOTHER text file. The program goes something like this:

For /F {conditions} DO {actions}

SET {information to NAME}

IF EXIST {conditions} ECHO %NAME% was found

IF NOT EXIST {conditions} ECHO %NAME% was not found

:: This sends the output to the screen

{remainder of loop statements}

I added a statement to send the output to a text file.

SET {information to NAME}

IF EXIST {conditions} ECHO %NAME% was found

IF EXIST {conditions} ECHO %NAME% was found > C:\NAMELIST.txt

IF NOT EXIST {conditions} ECHO %NAME% was not found

:: This sends the output to the screen

{remainder of loop statements}

This semi works. Since only one name is processed in each pass of the loop, NAMELIST.txt is rewritten with each successive pass through the loop. How can I get each name written to successive line in the file NAMELIST.txt without LOSING the information from the previous pass in the loop.
The generation of one name at a time is required by the other parts of the batch file.

Thanks for any help,

TomAll you need to do is understand the difference between the > operator and the >> operator. The FIRST one writes to a file, erasing any file that already exists with the same name. The second creates the file if it does not already exist, and APPENDS to it if it does. Just get it straight in your head when you want to create the file and when you want to append to it.

Thank you Salmon Trout for the great and fast reply.

My problem is I have not done any Basic programming since my Apple II+ / Apple IIe days. A LOT has changed and a lot I forgot.

Tom
Tomkaz, when I first started to want to output to files in a loop, I found the same problem as you. in summary, what I came up with was this:

if exist output.file del output.file
for /f %%L in (file.to.search) do (
{whatever}
if {whatever} echo {whatever} >> output.file
{whatever}
)



Discussion

No Comment Found