| 1. |
Solve : set info in text file to variable? |
|
Answer» hi all, 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 |
|