none
VBScript: Read Line from Text File / Write Line to New Text File

    Question

  • Let me try to explain what I am trying to do, to the best of my ability.

    I have a list of host names that I would like to read from, line by line. Once the first line is read, I would like to run a command line against the data, more specifically, the DSQUERY command.

    Once the DSQUERY has been run and results returned, I would like to determine whether or not the result is NULL. If the result is NULL, I would like to write the original host name to a NEW text file.

    I will try to lay it out a little better than I can explain it:

    Read line 1 of TEXTFILE.txt
    Input data in "cmd.exe dsquery computer -name %hostnamefromtext%"
    IF DSQUERY result = Null THEN
    WRITE %hostnamefromtext% to NEWTEXT.txt
    ELSE
    ENDIF
    CLEAR %hostnamefromtext%
    READ NEXT LINE of TEXTFILE.txt
    LOOP

    It's probably not the best explanation, but I am hopeful someone can help. I've written a few scripts, but I just can't pin it down.

    Any help would be greatly appreciated.

    Thanks.
    Wednesday, January 20, 2010 9:25 PM

Answers

  • Here is an example

    Dim WshShell, oExec
    Set wshShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set objInputFile = objFSO.OpenTextFile("c:\scripts\computers.txt",1)
    Set objOutputFile = objFSO.OpenTextFile("C:\scripts\output.txt",8,True)
    
    Do until objInputFile.AtEndofStream
    	strcomputer = objInputFile.ReadLine
    	strCommand = "dsquery computer -name " & strComputer
    	Set oExec = WShShell.Exec(strCommand)
    	If (oExec.Status = 0) Then
    		If (oExec.stdOut.ReadAll = "") Then
    			objOutputFile.WriteLine(strComputer)
    		End If
    	End If
    Loop
    
    objInputFile.Close
    objOutputFile.Close


    Thanks
    Ravikanth http://www.ravichaganti.com/blog
    Thursday, January 21, 2010 9:37 AM
    Moderator

All replies

  • Here is an example

    Dim WshShell, oExec
    Set wshShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set objInputFile = objFSO.OpenTextFile("c:\scripts\computers.txt",1)
    Set objOutputFile = objFSO.OpenTextFile("C:\scripts\output.txt",8,True)
    
    Do until objInputFile.AtEndofStream
    	strcomputer = objInputFile.ReadLine
    	strCommand = "dsquery computer -name " & strComputer
    	Set oExec = WShShell.Exec(strCommand)
    	If (oExec.Status = 0) Then
    		If (oExec.stdOut.ReadAll = "") Then
    			objOutputFile.WriteLine(strComputer)
    		End If
    	End If
    Loop
    
    objInputFile.Close
    objOutputFile.Close


    Thanks
    Ravikanth http://www.ravichaganti.com/blog
    Thursday, January 21, 2010 9:37 AM
    Moderator
  • Fantastic!

    I had a couple of lines with incorrect syntax, but this worked perfectly. Thank you so much for your assistance.
    Thursday, January 21, 2010 2:00 PM
  • You are welcome. Glad that I could help.
    Ravikanth http://www.ravichaganti.com/blog
    Thursday, January 21, 2010 2:57 PM
    Moderator
  • HI Ravikanth,

                          i  got thsi whiile i serch  something  for  my  requirments. This suits exacly &  its working  fine... Good one. :)

    RGDS,


    G
    Saturday, July 02, 2011 1:43 AM