45702

How to check existing file for entries, so not to rewrite data

Question:

What I've created is a script which pulls the Dell Service code, Username, and Computername, from a computer, and compiles that information into a .csv file. This script will be implemented via Active Directory login scripts, so end users wont have to do a thing.

The problem I'm having though, is everytime a person logs on, it collects their information, and adds it to the list. This means my list could just be full of two people who log onto their computer over and over.

What I would like to happen is for the script to search the .csv file for the specific data collected, and if this data exists, to not enter it.

The code I have so far is this:

'Get Dell Service Tag Info set ProSet = GetObject("winmgmts:").InstancesOf("Win32_BIOS") Set ProSet1 = GetObject("winmgmts:").InstancesOf("Win32_SystemEnclosure") For each Pro in ProSet For each Pro1 in ProSet1 ServiceTag=Pro.SerialNumber exit for Next exit for Next 'get username and computername, could also be asked in a batch Set oShell = WScript.CreateObject("WScript.Shell") Set oShellEnv = oShell.Environment("Process") sComputerName = oShellEnv("ComputerName") sUsername = oShellEnv("username") dim filesys, filetxt, getname, path Set filesys = CreateObject("Scripting.FileSystemObject") Set filetxt = filesys.OpenTextFile("\\xx.xx.xx.xx\Gathering\DataLog.csv", 8, True, -2) path = filesys.GetAbsolutePathName("\\xx.xx.xx.xx\Gathering\DataLog.csv") getname = filesys.GetFileName(path) filetxt.WriteLine sUsername & ", " & sComputerName & ", " & ServiceTag filetxt.Close

This is the basic script, without the entry checker.

As for an entry checker, this is what I have tried but it doesn't seem to work:

Set objFSO = CreateObject("Scripting.Dictionary") Set objFile = objFSO.OpenTextFile ("\\xx.xx.xx.xx\Gathering\Dictionary.txt", 8, True) ' Make comparisons case insensitive. objList.CompareMode = vbTextCompare ' ... code to read user name and assign to variable strNameOfUser. If (objList.Exists(strNameOfUser) = False) Then ' Add this user to the dictionary object. objList(strNameOfUser) = True ' Log this unique user name. objFile.WriteLine strNameOfuser End If

Any help is appreciated! Thanks!

Answer1:

(1) Your code is messed up: You store a Dictionary in objFSO and then try to invoke objFSO.OpentextFile()

(2) VBScript can't read <strong>and</strong> append to a file; so ForAppending (8) won't work; you'll have to read-open the file first, gather the info, close it, append-open and append new user info (if necessary)

(3) Using a dictionary is unnecessarily complex: to fill the dictionary you'll have to read the file from start to end, before you can ask the dictionary whether a specific user exists. While simply reading the file line by line, you can break the reading as soon as you find the user - then close - open & append - close - done.

<strong>UPDATE</strong>

Item (3) in code:

Dim goFS : Set goFS = CreateObject( "Scripting.FileSystemObject" ) Dim tsUsers : Set tsUsers = goFS.OpenTextFile(sFSpec, ForReading, True) Dim bFound : bFound = False Do Until tsUsers.AtEndOfStream If 1 = Instr(tsUsers.ReadLine(), sUser) Then bFound = True Exit Do End If Loop tsUsers.Close If Not bFound Then Set tsUsers = goFS.OpenTextFile(sFSpec, ForAppending, False) tsUsers.WriteLine sUser tsUsers.Close End If

Because I'm a pessimistic worrier: How do you plan to cope with more than one user logging in and write-access the file at the same time?

Recommend

  • installer registry access in windows server 2012
  • How does VBA treat 'or' statements [duplicate]
  • Button not working in multi-page application
  • Safari cannot download passbook file .pkpass
  • Sending Emails from different accounts with Google App Script
  • What is the default consistency level in spring-data-cassandra?
  • Artificial generation of cpu load in Java
  • Is it okay to delete a POSIX timer in the event handler function?
  • Choose one key arbitrarily in a dictionary without iteration [duplicate]
  • Numerical Triple Integration in R
  • Get Most Recent Column Value With Nested And Repeated Fields
  • ICU support in a 32-bit build of Qt5 with the VS2012 compiler causes Qt5 build failure; Webkit is al
  • Creating a .Net solution on the command line with mono
  • R: how to resample a datetime variable at the millisecond level?
  • urllib2 data sending
  • HazelcastInstance vs HazelcastClient
  • X509 store can not find certificate by SerialNumber
  • Android get Serial Number
  • I want to use Stormpath, Express.js, and Node.js. Do I want stormpath-express or express-stormpath?
  • How do I run a shell command inside a rake task as administrator?
  • WMI call to get drivers
  • C++ Static Initializer - Is it thread safe
  • How to Grab SSL Certificate in OpenSSL
  • 3.2 Apps with placeholder text will be rejected
  • Google Apps Marketplace SDK install does not forward user to Setup URL
  • When i use auto bi = 123456789, in C++, is it always assigned as an int?
  • How to browse android SDK sources in Android studio 2.0
  • Is it possible to ask GIT, don't show a file?
  • How to generate an asynchronous reset verilog always blocks with chisel
  • How to create two column output from a single column
  • how to get username into sql trigger when multiple users signed on from asp membership
  • Magento get URL before current
  • Ubuntu and bcrypt
  • Using JRuby with Rails 3.2
  • Code in Job's Script Block after Start-Process Does not Execute
  • Obtain ObjectIdHex value from mgo query
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Getting error when using KSoap library to consume .NET web services