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.
this is just a slightly shorter version:
If delayed expansion has been enabled before (
Setlocal EnableDelayedExpansion
) then there's a shorter (one-liner) versionReferenced 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!
I couldn't get this to work under Windows 7; the variable was simply not set. But here's a slightly longer solution:
I had to change the line of file for a text file inside
Program Files(x86)
directory. After trying a lot of solutions, the following solution worked for me:Steps:
Example:
Note:
When I tried writing the change back to the original file in step 1, the result was a an empty file.
Note the string I am matching is
T4VSHost
, which occurs in the file at pathjust FYI...
I tried it and didn't work. But I realized finally that I had to reproduce the text file (originally the text file is generated by a powershell script) so when I did this and it works.
Then apply the same code (change
text.txt
totext2.txt
)If I use the original one, I would either get garbage in
%var%
, not sure if the text file has not<CR>
or what. Anyway, by reproducing the text file, it seems working fine.