Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

Create a variable for local computername

I have the below vbs script that searches through .txt files and deletes any lines from them thats not equal to what is specified. I need to change AT6666T6 to be a variable based off of the computer name. In a bat file the variable would be %computername:~0,6%T6.
Option Explicit
 
 
 
Const ForReading = 1 
 
Const ForWriting = 2 
 
 
 
Dim objFSO
 
Dim fld
 
Dim fil
 
Dim strFile
 
 
 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
 
 
 
'Set the directory you are looking at
 
Set fld = objFSO.GetFolder("C:\tempdirectory\findit")
 
 
 
'Loop through each file in the folder
 
For Each fil in fld.Files
 
            strFile = lcase(fil.path)
 
            'If it is a text file send it off for editing
 
            If Right(strFile,3) = "txt" Then
 
                        DeleteLines strFile, "AT6666T6"
 
            End If
 
Next
 
 
 
'Clean up
 
Set fld = Nothing
 
Set objFSO = Nothing
 
 
 
 
 
Private Sub DeleteLines(sFile, sSearch)
 
Dim objFile
 
Dim strLine
 
Dim strNewContents
 
            
 
            Set objFile = objFSO.OpenTextFile(sFile, ForReading) 
 
            Do Until objFile.AtEndOfStream 
 
                        strLine = objFile.ReadLine 
 
                        If InStr(strLine, sSearch) > 0 Then 
 
                                    strNewContents = strNewContents & strLine & vbCrLf 
 
                        End If 
 
            Loop 
 
            objFile.Close 
 
            
 
            Set objFile = objFSO.OpenTextFile(sFile, ForWriting) 
 
            objFile.Write strNewContents 
 
            objFile.Close 
 
            
 
            Set objFile = Nothing
 
End Sub

Open in new window

0
daveTechSearch
Asked:
daveTechSearch
1 Solution
 
Donald StewartNetwork AdministratorCommented:
Add these lines
Dim  WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName
 
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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