Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create a variable for local computername

Posted on 2009-04-02
1
Medium Priority
?
531 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 400 total points
ID: 24054889
Add these lines
Dim  WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName
 
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

730 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