Solved

Create a variable for local computername

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using Pop Up Calculator in Excel 4 87
Modification on userform and column K 47 42
Need to Modify a Script I found 5 98
Can we place a tooltip on the actual vb6 form 5 41
Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

805 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