Solved

Set the 2nd line of a bath file as a batch file variable

Posted on 2011-09-07
19
415 Views
Last Modified: 2012-05-12
Hay

I need to set a second line of a txt file that looks a little like this as a variable

temp.txt
_______________

line 1
123456789
line 3
_______________



So my batch file will look like


Batch file.bat
_______________

code


echo %line2%
pause
_______________


And would show

echo 123456789

Thanks
Dan
0
Comment
Question by:netefex
  • 6
  • 5
  • 4
  • +2
19 Comments
 
LVL 14

Expert Comment

by:setasoujiro
ID: 36494687
add this:
set var=123456789


now say :
echo %var%
0
 
LVL 14

Expert Comment

by:setasoujiro
ID: 36494697
so if you want a variable called line2

set line2=123456789

now when you use %line2% the output will be 123456789
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36494782
I think you are asking to read the second line of a text file?

Try this:

@echo off
for /f skip=1 delims=" %%a in (temp.txt) do (set line2=%%a) & goto next
:next
echo Line 2 is %line2%

hth

Steve
0
 

Author Comment

by:netefex
ID: 36494866
Steve that looks close but it does not seem to work the batch file just flashs up and closes

Have I got it right?

setasoujiro I wish it was that easy, however the .txt document changes so I need the batch file to get the txt from there
@echo off

for /f skip=1 delims=" %%a in (temp.txt) do (set line2=%%a) & goto next
:next

echo Line 2 is %line2%

pause

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36494889
It sounds like you want to read the line from the TXT file, and assign it to a variable for use in the BAT script.  To do that you use the FOR /F statement as shown below.  The FOR command has a lot of power, and the /F option allows reading and parsing up a text file.

In the example below, we read lines from the temp.txt file one by one in the for loop, after skipping the first line (skip=1). The entire line will be assigned to the loop variable %%A (tokens=*) and if we haven't already found line 2 then we set the variable for use after the loop. There is no easy clean way other than GOTO to end the loop after the second line, so it will read every other line, but not set the line2 variable once it is set the first time.

Depending on what you wanted to do you could include the actual logic to work with the line from the text file right in the body of the FOR loop.  Let me know if you have questions.

@echo off
set line2=
for /F "tokens=* skip=1 usebackq" %%A in ("temp.txt") do (
  if not defined line2 set line2=%%A
)
echo %line2%

Open in new window

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36494896
I think Steve's code would want to be:

@echo off

for /f skip=1 delims=" %%a in (temp.txt) do (
  set line2=%%a
  goto next
)
:next

echo Line 2 is %line2%

pause

Open in new window

0
 

Author Comment

by:netefex
ID: 36494929
Yes that is what I am trying to do

However that did not seem to work

Here is my full code

The long story is I am using WMIC to get the Dell service TAG number and put it into a txt file
However when you do that it puts "SerialNumber" on line 1 and "21425" (the TAG number) on line 2
@echo off

wmic bios get serialnumber > temp.txt

set line2=
for /F "tokens=* skip=1 usebackq" %%A in ("temp.txt") do (
  if not defined line2 set line2=%%A
)

echo %line2%

pause

Open in new window

0
 
LVL 9

Expert Comment

by:jeff_01
ID: 36494957
Try this,

for /f "delims=" %%i in ('findstr ^ [2] c:\test.txt') do set VAR=%%i

If you are trying it direct from the command line then %%i must become %i

0
 

Author Comment

by:netefex
ID: 36495040
still no luck

Tryed it with the "If you are trying it direct from the command line then %%i must become %i " bit


@echo off

wmic bios get serialnumber > C:\Users\dowen\Desktop\test.txt


for /f "delims=" %%i in ('findstr ^ [2] C:\Users\dowen\Desktop\test.txt') do set VAR=%%i


echo %line2%

pause

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 51

Expert Comment

by:Bill Prew
ID: 36495165
I have a better way to parse WMIC output, just a sec...

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36495166
It is because you are using " " around the filename in the for command.  Sorry for mistyping the " missing on my original post.  It should be OK:

@echo off
for /f "skip=1 delims=" %%a in (temp.txt) do (set line2=%%a) & goto next
:next
echo Line 2 is %line2%

hth

Steve
0
 
LVL 9

Expert Comment

by:jeff_01
ID: 36495170
Try

 
@echo off

wmic bios get serialnumber > C:\Users\dowen\Desktop\test.txt


for /f "delims=" %%i in ('findstr ^ [2] C:\Users\dowen\Desktop\test.txt') do set line2=%%i


echo %line2%

pause

Open in new window

0
 

Author Comment

by:netefex
ID: 36495194
Steve still no luck

Billprew if you have a better way I am all ears
@echo off

wmic bios get serialnumber > temp.txt

for /f "skip=1 delims=" %%a in (temp.txt) do (set line2=%%a) & goto next
:next

echo Line 2 is %line2%

pause

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36495243
Try this approach.

@echo off
set SerialNumber=
for /f "skip=2 tokens=2 delims=," %%A in ('wmic bios get serialnumber^,version /format:csv') do set SerialNumber=%%A
echo [%SerialNumber%]
pause

Open in new window

~bp
0
 

Author Comment

by:netefex
ID: 36495360
Got this
cmd.png
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 36495653
OK try this then:

@echo off
set torun=wmic bios get serialnumber /format:value
for /f "tokens=2 delims==" %%a in ('%torun%') do set serial=%%a
echo Serial is %serial%

Steve
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36495866
==> Invalid XSL format (or) file name.

Okay, I've seen that error on some systems.  I think this will resolve that, let me know.

@echo off
set SerialNumber=
for /f "skip=2 tokens=2 delims=," %%A in ('wmic bios get serialnumber^,version /format:csv.xsl') do set SerialNumber=%%A
echo [%SerialNumber%]
pause

Open in new window

~bp
0
 

Author Closing Comment

by:netefex
ID: 36495977
GREAT THATS IS

Not only does it work it is better than the way that I had thought to do it

Top marks :D
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36496170
Glad it helped, if I'd have know that was what you are after in the first place could have said :-)

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now