Create a variable for local computername

Posted on 2009-04-02
Last Modified: 2012-05-06
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



'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 




            Set objFile = objFSO.OpenTextFile(sFile, ForWriting) 

            objFile.Write strNewContents 



            Set objFile = Nothing

End Sub

Open in new window

Question by:daveTechSearch
1 Comment
LVL 47

Accepted Solution

dstewartjr earned 100 total points
ID: 24054889
Add these lines
Dim  WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now