• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 508
  • Last Modified:

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
0
ftarvin
Asked:
ftarvin
  • 4
  • 2
1 Solution
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
ftarvinAuthor Commented:
Yes. you did it with your eyes closed. Boring. but you solved an important issue.

Thanks Again.
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now