Solved

Create a variable for local computername

Posted on 2009-04-02
1
530 Views
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
 
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
Comment
Question by:daveTechSearch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 47

Accepted Solution

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

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

634 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