1.

Solve : set info in text file to variable?

Answer»

hi all,

I am trying to get the computer description from active directory. I have it working so far but I would like the results to be USABLE in a variable form. The text file I have the description in looks like the following:

desc
[computer description]
dsget succeeded

I would like to pull just the second line of the text file. Any ideas on how to grab that line no matter how LONG the computer description is?

thanks,
Wayne
C:\>type blanks.txt
" "
444



3333


555



66666



7777


C:\>find "66666" blanks.txt

---------- BLANKS.TXT
66666

C:\>I am trying to put the second line in a variable for use. Also the second line will change every time the query is ran. I do not know what the description will be when it is ran. That is why I am trying to put it in the variable. The description is being accessed from active directory. CODE: [Select]@echo off
for /f "skip=1 tokens=*" %%a in (file.txt) do (
set var=%%a
goto NEXT
)
:NEXT
pausewhen I run this I get the last line of the file. any ideas?Quote from: wbrost on March 12, 2009, 11:11:48 AM

when I run this I get the last line of the file. any ideas?

Devcom's code gets the 2nd line OK for me. However, here's a slightly different way

Code: [Select]@echo off
setlocal enabledelayedexpansion
set /a line=0
for /f "delims==" %%L in (file.txt) do (
set /a line+=1
if !line! EQU 2 set var=%%L
)
echo 2nd line is %var%
wbrost,

I have not written any code since 1992. And the code I wrote was was usually UNIX shell script or C. So forgive my comments if we both don't benefit.

If the file name is different each time, then a command line argument will help. For example:


C:\>2line homewk.txt
file name homewk.txt
2nd line is " Write a batch file that display a welcoming message before displaying your

C:\>type 2line.bat
@echo off
echo file name %1
setlocal enabledelayedexpansion
set /a line=0
for /f "delims==" %%L in (%1) do (
set /a line+=1
if !line! EQU 2 set var=%%L
)
echo 2nd line is %var%
C:\>

p.s It helps me when an example of the output is listed.

Good Luck with you projectthank you to all that helped out. I have made the following to access the Active Directory computer description.

You will need to edit the code to point to your AD.

Code: [Select]@ECHO OFF
CLS
SETLOCAL ENABLEDELAYEDEXPANSION

:LOOP
CLS
ECHO please enter computer name
ECHO.
SET CN=
SET /P CN=Computer Name:

REM TESTS TO SEE IF HOST IS ACTIVE
SET ERRORLEVEL=
PING -n 1 %CN% > nul


REM PING ERRORLEVEL OF 0 MEANS SUCCESS
REM PING ERRORLEVEL OF 1 MEANS THE HOST IS NOT FOUND OR TURNED OFF

IF ERRORLEVEL 1 GOTO fail
IF ERRORLEVEL 0 GOTO ADQUERY

:fail
CLS
ECHO.
ECHO.
ECHO host %CN% non responsive or not found
ECHO please try again
PING -n 1 -w 5000 1.1.1.1 > nul
GOTO LOOP

:ADQUERY
REM command to gather information from AD
dsquery computer [AD OU] -name %CN% | dsget computer -desc > %temp%\computerdesc.txt


REM Will access text file and set the second line to %var%
setlocal enabledelayedexpansion
set /a line=0
for /f "delims==" %%L in (%temp%\computerdesc.txt) do (
set /a line+=1
if !line! EQU 2 set var=%%L
)
CLS
echo.
echo.
echo %var%

DEL /Q %temp%\computerdesc.txt

pause > nul

thank you,
wbrost


Discussion

No Comment Found