Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest Windows batch file code to get first line of a file set to a variable?

Tags:

I am looking for the shortest amount of code possible that will get the first line of a text file and set it to a variable in Windows.

There are lots of examples of this kind of thing in other threads on this StackExchange site but I am not looking for how to loop through and display all lines in a file. What I want is to just simply get the first line.

like image 743
djangofan Avatar asked Feb 16 '11 23:02

djangofan


People also ask

What is %% f in batch file?

For simple batch files, a single character such as %%f will work. You can use multiple values for variable in complex batch files to distinguish different replaceable variables.

What does 0 |% 0 Do in batch?

What it is: %0|%0 is a fork bomb. It will spawn another process using a pipe | which runs a copy of the same program asynchronously. This hogs the CPU and memory, slowing down the system to a near-halt (or even crash the system).

What is %% g'in batch file?

%%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'.

What is %1 in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.


1 Answers

set /p var= <Text.txt echo %var% 

Referenced from: http://forums.techguy.org/software-development/551867-batch-file-read-line-text.html Ironically the poster is able to get the first line but wanted to loop through the whole file!

like image 110
Joe Avatar answered Sep 30 '22 02:09

Joe