read text file each line

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

read text file each line

Post by MigrationUser »

11 Aug 2013 12:16
LEARNER

I am trying to read a text file line by line and get output to new file (or use it as variable in same file)

Code: Select all

setlocal delayed expansion
for /f "tokens=*" %%i in ('C:\NEW\d.txt') do echo this %%i is ok>>C:\USERS\MINE\desktop\dl2.txt
It is not creating new file as I want

----------------------------

#2 11 Aug 2013 12:53
foxidrive

Give this a shot:

Code: Select all

@echo off
setlocal delayed expansion
for /f "delims=" %%i in ('type "C:\NEW\d.txt" ') do ">>C:\USERS\MINE\desktop\dl2.txt" echo this %%i is ok
----------------------------

#3 14 Aug 2013 15:06
LEARNER

Thanks
using TYPE it worked. but i am bit confused with only using "delims=" & "not tokens=*"
I don't know much about batch programming
but as far as I have read & found information default delimiters are space & tab
I thought if I use "tokens=*" it will get complete line.

I just want to know how it is working ?

other thing I have read in help, we need to specify only file name & here we using TYPE to get output.

so how come original help example doesn't work !!!???!!!

----------------------------

#4 14 Aug 2013 15:38
foxidrive

If you had used (C:\NEW\d.txt) and not ('C:\NEW\d.txt') then it would have worked.

Using type is one method, and it is a matter of preference, but spaces in path or filename will break the (C:\NEW\d.txt) way of specifying a file.
You could use "usebackq" too and specify the filename like this ("C:\NEW\d.txt") which will handle spaces etc too.

tokens=* removes leading whitespace while delims= preserves the entire line.

Putting the redirection first allows you to echo numerals as the last part of the string.
echo number 1>>file.txt
will fail but this will work
>>file.txt echo number 1

----------------------------

#5 15 Aug 2013 14:30
LEARNER
foxidrive wrote:

Putting the redirection first allows you to echo numerals as the last part of the string.
echo number 1>>file.txt
will fail but this will work
>>file.txt echo number 1
OMG forgive my little knowledge....
I thought it's by mistake you put it in the middle I used it the end as it was in my code.
so it means if I am not doing echo say I am doing "| find" , will it also effect / fail same way as you described ?
or using it as variable or doing some other thing in the same code will fail also ?
fortunately, none of lines in my text file end with number. so it worked.

what about this number 1 or 2. I have seen in many batch codes. why this appears or we need to put ?
specially before ">nul" in many code examples I have seen this.
what's this function ?

----------------------------

#6 15 Aug 2013 15:28
foxidrive

It's mainly in the echo command - I can't think of another command where that fails in the same way.
People often include a space before the redirection as in echo number 1 >>file.txt and that solves the number problem, but the space goes into the file too and that can be a problem in itself.

Re Number 1 and 2: I think you are referring to the stream numbers, where 1 is the STDOUT stream and 2 is the STDERR stream. Standard out and standard error respectively.
A utility of most kinds will direct the plain text to STDOUT and any error messages to STDERR.

On the console screen you see both, but a batch file can separate them by using various methods of redirection:

This will separate them into two different files.
file.exe 1>file.log 2>error.log

This will redirect them both into the file.log (STDERR goes to the same place that STDOUT is currently going to) and the 1 is implied.
file.exe >file.log 2>&1

This will log the output of file.exe into file.log and send any STDERR message to the nul device (a black hole) and you don't see it.
file.exe >file.log 2>nul

This will suppress all output from the exe file.
file.exe >nul 2>&1

Often you can just use >nul by itself, when you know there will be no error messages at all.

You should be aware that there are other stream numbers that can be used,
and regarding redirection of file output - some exe/com files write directly to the screen and can't be redirected. I think that is more to do with legacy programs now.

Hopefully that is clear.

Last edited by foxidrive (15 Aug 2013 15:33)

----------------------------

#7 21 Aug 2013 04:13
LEARNER


thanks for making it clear
I tried but could not figure it out how to use this functionality.

I have a text file for all dates of a year, having lines e.g. 01 JAN:MAX. temp was 21
if I enter date it echo line after : in text file with that date

Code: Select all

set /p DR=

for /f "tokens=1-2 delims=:" %%y in ('type "C:\reports\2000.txt" ') do if "%%y" == "!DR!" echo this part LOCATED @ %%z >>"C:\data\d2.txt"
but if any date entered that does not exist in file it doesn't do anything. I mean in output file there is no line.
I want to output error in new file like this

Code: Select all

for /f "tokens=1-2 delims=:" %%y in ('type "C:\reports\2000.txt" ') do if "%%y" == "!DR!" echo this part LOCATED @ %%z >>"C:\data\d2.txt" else echo !DR! not found 2>C:\data\d2-error.txt
tried many ways but I am unable to get this worked.
another advise appreciated - This FOR loop I am using, if any other better & faster way to do this ?

Last edited by LEARNER (21 Aug 2013 04:14)

----------------------------

#8 21 Aug 2013 12:27
foxidrive


Give this a crack:

Code: Select all

@echo off
set "flag="
set /p "DR=Enter date: "
for /f "tokens=1,2 delims=:" %%y in ('find "%dr%" ^< "C:\reports\2000.txt" ') do >>"C:\data\d2.txt" echo this part LOCATED @ %%z&set flag=1
if not defined flag >"C:\data\d2-error.txt" echo %DR% not found
Post Reply