Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read just first line from file

12,471 views
Skip to first unread message

Robert Crandal

unread,
May 23, 2011, 5:42:44 AM5/23/11
to
What are a few ways that I can read the first line from
a file (named "data.txt") and store that value in
a string or environment variable for my BAT file?

Thank you!


foxidrive

unread,
May 23, 2011, 6:04:20 AM5/23/11
to

Here are two:


set /p "var="<"data.txt"
echo %var%

for /f "delims=" %%a in (data.txt) do set "var=%%a"&goto :stop
:stop
echo %var%


--
Regards,
Mic

billious

unread,
May 23, 2011, 6:15:43 AM5/23/11
to

and here's a third:

(set var=)
for /f "delims=" %%a in (data.txt) do if not defined var set var=%%a


Robert Crandal

unread,
May 23, 2011, 6:18:21 AM5/23/11
to
So many geniuses here, sheesh! 8)


"billious" <billio...@hotmail.com> wrote in message
news:d5CdnfQCicNPrkfQ...@westnet.com.au...

Message has been deleted
Message has been deleted

I'm_HERE

unread,
May 23, 2011, 9:43:51 AM5/23/11
to
for /f "delims=" %%a in ('"findstr /n . data.txt|findstr /b 1:"') do
set "var=%%a"
set "var=%var:1:=%"

set var

Timo Salmi

unread,
May 23, 2011, 3:03:37 PM5/23/11
to

@echo off & setlocal enableextensions
set myfile_=My test file.txt
for /f "tokens=1,* delims=:" %%a in (
'"findstr /n . "%myfile_%"|findstr /b 1:"') do (
set FirstLine=%%b)
echo FirstLine=%FirstLine%
endlocal & goto :EOF

23} How do I get the n'th, the first and the last line of a text file?
http://www.netikka.net/tsneti/info/tscmd023.htm

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php

I'm_HERE

unread,
May 24, 2011, 4:51:36 AM5/24/11
to
another variant

for /f "tokens=1* delims=*" %%a in ('
"(echo l&echo e)|edlin /b data.txt|find "1:*""
') do echo %%b

and..

cd.>µ
for /f "tokens=1*" %%a in (
'"fc /n /a data.txt µ|find "1:""'
) do echo %%b
del µ

Timo Salmi

unread,
May 24, 2011, 5:49:20 AM5/24/11
to
On 24.05.2011 11:51 I'm_HERE wrote:
> another variant
>
> for /f "tokens=1* delims=*" %%a in ('
> "(echo l&echo e)|edlin /b data.txt|find "1:*""
> ') do echo %%b
>
> and..

All these are excellent and useful learning exercises about the
intricacies of cmd script programming. They are exactly what this
discussion newsgroup basically is about. I like this.

However, if the actual goal is effective solutions for "production
purposes", then our solutions are esoteric and too prone to several
catches like the poison characters. In the production line a sed or a
gawk or a VBS-aided solution is the effective and safe way to go.

rjo...@gmail.com

unread,
Jan 29, 2018, 4:39:30 AM1/29/18
to
How can a change my first line of a csv file
the first line of my test.csv file looks like

Country,region,Place
Netherlands,Utrecht,Soest

And i want to add a underscore into first line only
so files looks like

_Country,_region_Place
Netherlands,Utrecht,Soest

how to do that?

JJ

unread,
Jan 29, 2018, 4:11:23 PM1/29/18
to
Batch file can't actually edit the line(s) in text files. It can only
overwrite or append the file contents.

So, use FOR /F to retrieve the lines and write them into a new file. Change
the line when it's the first one, and leave the rest unchanged. But since
you need to insert an underscore onto each header column of the CSV file,
the lines would need to be parsed and be split into columns using "," as the
delimiter. Each line would then need to be rebuilt by reinserting the ","
column separators before writing them out to the output file. Once all of
the lines are processed, replace the original CSV file with the changed one.

Zaidy036

unread,
Jan 29, 2018, 5:09:20 PM1/29/18
to
did you really want to eliminate the last ","?

if the line is _first=Netherlands,Utrecht,Soest

Then use:
SET _first=%_first:,=,_%
SET _first=_%_first%

--
Zaidy036
0 new messages