Link to home
Start Free TrialLog in
Avatar of ftarvin
ftarvin

asked on

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
Avatar of exx1976
exx1976
Flag of United States of America image

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.
Avatar of ftarvin
ftarvin

ASKER

Yes. For this particular solution, prefer not doing it via login. Thanks.
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

Avatar of ftarvin

ASKER

Thanks. Testing.
Avatar of ftarvin

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of Shift-3
Shift-3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ftarvin

ASKER

Yes. you did it with your eyes closed. Boring. but you solved an important issue.

Thanks Again.