1.

Solve : Having trouble manipulating data read from a file?

Answer»

Hello,

Can someone explain how to manipulate data read from a file?

I'm trying to read one line from a text file, put that line in a variable, do something with that variable and repeat with next line until the end of the file.

It's for another homework assignment. The teacher seems to have a habit of not giving us enough information. It's really frustrating and time consuming, I've spent the whole afternoon trying to figure out how to do this. Sorry, I'll stop complaining now.

I think I need to use a For statement. I can get it to read the file and print the contents to the screen. What I can't figure out is how to take each line from the text file and do something with it.

This is what I have:Code: [Select]@echo off

for %%f in (machine.txt) do type %%f
Can ANYONE recommend a good reference, like a book or something, that explains how to do stuff like this?

ThanksCode: [Select]@echo off
for /f %%F in (machine.txt) do (
echo %%F
)
pause
this will echo each line in file

and try for /? in cmd to get more infoThis site has good inflammation of the FOR command.
FOR /f means to read lines for just one file.

Otherwise FOR often means the file names found in a directory

The example you gave was FOR without the /f switch, that is what changes the meaning of the for command.This is a kind of metal abuse that MS has imposed on us. Instead of a /f option, they shroud have named a new command. like maybe
ROF. Which is FOR slept backwards and means Read One File. and that would be easy to remember. But nobody can remember to use the /f with FOR command, except the experts on this forum , they can withstand the heavy mental abuse.

Excuse me, I need a Prozac. Quote from: The Hansenator on April 04, 2009, 03:08:17 PM

Can someone explain how to manipulate data read from a file?
there are better tools to do file manipulation than using batch. even VBSCRIPT has a fair amount of FUNCTIONS for that. splitting up lines, replacing text, storing data into arrays, etc that makes things easy for a programmer. If you are learning programming, at least get a proper programming LANGUAGE. (or change your teacher )Perl is specifically designed to be easy to use for manipulating text files, I believe.Quote from: BC_Programmer on April 04, 2009, 07:45:36 PM
Perl is specifically designed to be easy to use for manipulating text files, I believe.
not just Perl. Python, sed, awk , even Ruby and PHPQuote from: The Hansenator on April 04, 2009, 03:08:17 PM
The teacher seems to have a habit of not giving us enough information.

It's a well known teacher's trick, called "hopefully making you think and learn how to discover things". Like the fact that commands have help that you see by TYPING the command, a space, and /? at the prompt.

Try typing FOR /? at the command prompt.
Quote from: Dias de verano on April 05, 2009, 03:54:25 AM
It's a well known teacher's trick, called "hopefully making you think and learn how to discover things". Like the fact that commands have help that you see by typing the command, a space, and /? at the prompt.

Try typing FOR /? at the command prompt.


I'm familiar with that trick but it wasn't until yesterday, after stumbling across it on the internet, that I knew there even was a FOR command. It's hard to look up something when you don't know what to look for.

I did type FOR /?, and maybe I'm a little slow, but I wasn't able to figure it out by looking at the help file.Thank you to both of you, that was just what I needed!

Quote from: devcom on April 04, 2009, 03:15:32 PM
Code: [Select]@echo off
for /f %%F in (machine.txt) do (
echo %%F
)
pause
this will echo each line in file

Quote from: Geek-9pm on April 04, 2009, 03:59:24 PM
FOR /f means to read lines for just one file.

Otherwise FOR often means the file names found in a directory


Discussion

No Comment Found