VB script to take computer name from input file, check specific line in text file in specific location, change if necessary, log which computers were changed

input file: pc.txt
pc1
pc2
etc.
check for c:\windows\example.txt
check for x=22 line in file (only one specific line)
change to x=5
save
log to results.txt
ftarvinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

exx1976Commented:
It would most likely be easier to check for that file and make the change in a login script rather than trying to do it remotely, you could run into permissions issues and such.

Is that acceptable?  If so, I can whip something up.

Let me know.
0
ftarvinAuthor Commented:
Yes. For this particular solution, prefer not doing it via login. Thanks.
0
Shift-3Commented:
Paste the script below into a text file with a .vbs extension.  Customize the value of the strList variable with the location of the computer list.  Customize the value of the strFile variable with the location of the target file, as it would appear after the computer name in a UNC path.  Running the script will replace the first line in the file which matches the value of the strFindLine variable with the value of the strReplaceLine variable.  The names of computers where files were modified will be logged to the file specified in the strLog variable.

Please test this carefully before running in a production environment.


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
On Error Resume Next
 
strList = "pc.txt"
strFile = "c$\windows\example.txt"
strFindLine = "x=22"
strReplaceLine = "x=5"
strLog = "results.txt"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objList = objFSO.OpenTextFile(strList, ForReading, False, TriStateUseDefault)
arrComputers = Split(objList.ReadAll, vbCrLf)
objList.Close
 
Set objLog = objFSO.OpenTextFile(strLog, ForWriting, True)
objLog.WriteLine Date & " " & Time
 
For Each strComputer in arrComputers
    strTarget = "\\" & strComputer & "\" & strFile
    
    If objFSO.FileExists(strTarget) Then
        blnFound = False
        
        Set objTarget = objFSO.OpenTextFile(strTarget, ForReading, False, TriStateUseDefault)
        
        Do Until objTarget.AtEndOfStream
            strNextLine = objTarget.Readline
 
            If Not blnFound And strNextLine = strFindLine Then
                strNextLine = strReplaceLine
                objLog.WriteLine "File modified on " & strComputer
                blnFound = True
            End If
 
            strNewFile = strNewFile & strNextLine & vbCrLf
        Loop
        
        objTarget.Close
        
        Set objTarget = objFSO.OpenTextFile(strTarget, ForWriting)
 
        objTarget.WriteLine strNewFile
        objTarget.Close
    End If
Next
 
objLog.Close

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

ftarvinAuthor Commented:
Thanks. Testing.
0
ftarvinAuthor Commented:
Testing great so far. One minor change if possible... in results.txt
can it just have 2 comma delimited results..
I.E.
"pc1","same"
"pc2","changed"
"pc3", "changed"

etc..

Thanks!
0
Shift-3Commented:
So modified.


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
On Error Resume Next
 
strList = "pc.txt"
strFile = "c$\windows\example.txt"
strFindLine = "x=22"
strReplaceLine = "x=5"
strLog = "results.txt"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objList = objFSO.OpenTextFile(strList, ForReading, False, TriStateUseDefault)
arrComputers = Split(objList.ReadAll, vbCrLf)
objList.Close
 
Set objLog = objFSO.OpenTextFile(strLog, ForWriting, True)
objLog.WriteLine Date & " " & Time
 
For Each strComputer in arrComputers
    strTarget = "\\" & strComputer & "\" & strFile
    blnFound = False
    
    If objFSO.FileExists(strTarget) Then 
        Set objTarget = objFSO.OpenTextFile(strTarget, ForReading, False, TriStateUseDefault)
        
        Do Until objTarget.AtEndOfStream
            strNextLine = objTarget.Readline
 
            If Not blnFound And strNextLine = strFindLine Then
                strNextLine = strReplaceLine
                blnFound = True
            End If
 
            strNewFile = strNewFile & strNextLine & vbCrLf
        Loop
        
        objTarget.Close
        
        Set objTarget = objFSO.OpenTextFile(strTarget, ForWriting)
 
        objTarget.WriteLine strNewFile
        objTarget.Close
        
        If blnFound Then
            objLog.WriteLine Chr(34) & strComputer & Chr(34) & "," & Chr(34) & "changed" & Chr(34)
        Else
            objLog.WriteLine Chr(34) & strComputer & Chr(34) & "," & Chr(34) & "same" & Chr(34)
        End If
    Else
        objLog.WriteLine Chr(34) & strComputer & Chr(34) & "," & Chr(34) & "file not found" & Chr(34)
    End If
Next
 
objLog.Close

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ftarvinAuthor Commented:
Yes. you did it with your eyes closed. Boring. but you solved an important issue.

Thanks Again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.