Solved

Create a variable for local computername

Posted on 2009-04-02
1
527 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending Attachment via CDO 3 71
ASP CDO - Limiting Connections 3 59
Problem to With line 4 64
need the count of duplicate records in a column 12 43
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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

807 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