Quantcast
Channel: VBForums - ASP, VB Script
Viewing all articles
Browse latest Browse all 699

Detecting and avoiding duplicate data stored in a loaded CSV

$
0
0
Morning VB community..

I am involved with a project where they have provided me a CSV file with a 3-column comma delimited data array..

It goes as follows:

Machine Name, UserID, LabelID
MSI-MACHINE01,adm-testaccoun!,LABELNAME1

This CSV file has over a thousand rows with Machine Names associated with the same LabelID at least once as follows:

Code:

MSI-MACHINE01,adm-testaccoun!,LABELNAME1
MSI-MACHINE01,adm-testaccount,LABELNAME2
MSI-MACHINE02,adm-testaccoun!,LABELNAME3
MSI-MACHINE03,adm-testaccount,LABELNAME4

Essentally, we use the CSV to capture what MachineIDs or UserIDs are attached to that third column "LabelID" and write this to a txt file called "ShortcutFile" to the user's own Z:\UserDesktop\.

So the scenario is as follows:

If the below script reads the CSV file with the MachineID or UserID matches the LabelID data on the third column then write a ShortcutFile 1.txt to Z:\UserDesktop\ in an area of the txt called "LABELNAME001" which replaces it with the LabelID found.

Before doing this it checks for the existence of any older named .txt of this type and renames them to .old as a quick backup process.

This all works fine when the MachineID or UserIDs have different LabelIDs as follows in the loaded CSV:

Code:

MSI-MACHINE01,adm-testaccoun!,LABELNAME1
MSI-MACHINE0!,adm-testaccount,LABELNAME2
MSI-MACHINE01,adm-testaccoun!,LABELNAME3
MSI-MACHINE0!,adm-testaccount,LABELNAME4

So the above scenario if the machine I am on is called "MSI-MACHINE01" and the user ID I am using is "adm-testaccount" it will create the following Shortcut X.txt files in Z:\UserDesktop\

Shortcut 1.txt -> saves as "LABELNAME1" within the txt file
Shortcut 2.txt -> saves as "LABELNAME2" within the txt file
Shortcut 3.txt -> saves as "LABELNAME3" within the txt file
Shortcut 4.txt -> saves as "LABELNAME4" within the txt file

Problem is: when it finds this instead in the CSV:

Code:

MSI-MACHINE01,adm-testaccoun!,LABELNAME1
MSI-MACHINE0!,adm-testaccount,LABELNAME1
MSI-MACHINE01,adm-testaccoun!,LABELNAME2
MSI-MACHINE0!,adm-testaccount,LABELNAME2

It will create these shortcut files:

Shortcut 1.txt -> saves as "LABELNAME1" within the txt file
Shortcut 2.txt -> saves as "LABELNAME1" within the txt file
Shortcut 3.txt -> saves as "LABELNAME2" within the txt file
Shortcut 4.txt -> saves as "LABELNAME2" within the txt file

The objective we want is if it detects trying to save the same kind of LabelID it will skip it to go onto the next unique label so the above scenario we want is this:

Shortcut 1.txt -> saves as "LABELNAME1" within the txt file
Shortcut 2.txt -> saves as "LABELNAME2" within the txt file

So as you see we want it to skip writing Shortcut 2.txt with "LABELNAME1" (since it wrote it already in Shortcut 1.txt) and move onto the next unique LabelID and write it as the next incremented Shortcut X.txt.

Here is the code we are using..

Code:

On Error Resume Next

Const ForReading = 1
Const ForWriting = 2
Const CSV_File = "WorkstationUserLabel.csv"
Const LABEL_Filename = "ShortcutFile"
Const LABEL_TargetPath = "Z:\UserDesktop\"
Dim CurDir, oFSO, sCSV, fCSV, aCSV_Line, sComputerName, sUserName, nFileIndex, fLABEL, sLABELContents

netDrive = "Z:\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
baseDir = "UserDesktop"
If Not objFSO.FolderExists (netDrive & baseDir) Then
    objFSO.CreateFolder(netDrive & baseDir)
Else

End If

'Search Z:\UserDesktop and rename existing ShortcutFile-only LABEL files to OLD
Set strUserDesktop = objFSO.GetFolder("Z:\UserDesktop")
Set folder = objFSO.GetFolder(strUserDesktop)

For each file In folder.Files 
    If instr(file, "ShortcutFile") > 0 OR instr(file, "shortcutfile") > 0 THEN
        file.name = replace(file.name, ".txt", ".old")
    End IF
 Next

sComputerName = CreateObject("WScript.Network").ComputerName
sUserName = CreateObject("WScript.Network").UserName

Set oFSO = CreateObject("Scripting.FileSystemObject")
CurDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
If Right(CurDir, 1) <> "\" Then CurDir = CurDir & "\"

sCSV = CurDir & CSV_File

If oFSO.FileExists(sCSV) Then
    Set fCSV = oFSO.OpenTextFile(sCSV, ForReading)
        Do Until fCSV.AtEndOfStream
            aCSV_Line = Split(fCSV.ReadLine, ",")
            If ((aCSV_Line(0) = sComputerName) Or (aCSV_Line(1) = sUserName)) Then  'Scan .CSV file for relevant Computer and Users and associate them to a LABEL NAME
                nFileIndex = 1
                Do Until Not(oFSO.FileExists(LABEL_TargetPath & LABEL_Filename & Chr(32) & cStr(nFileIndex) & ".txt"))
                    nFileIndex = nFileIndex + 1
                Loop
                oFSO.CopyFile CurDir & LABEL_Filename & ".txt", LABEL_TargetPath & LABEL_Filename & Chr(32) & cStr(nFileIndex) & ".txt" 'If associated LABEL file is detected, copy the select one(s) to Z:\UserDesktop
                Set fLABEL = oFSO.OpenTextFile(LABEL_TargetPath & LABEL_Filename & Chr(32) & cStr(nFileIndex) & ".txt", ForReading)
                    sLABELContents = fLABEL.ReadAll
                fLABEL.Close
                sLABELContents = Replace(sLABELContents, "LABELNAME001", aCSV_Line(2))
                Set fLABEL = oFSO.OpenTextFile(LABEL_TargetPath & LABEL_Filename & Chr(32) & cStr(nFileIndex) & ".txt", ForWriting) 'Associate detected LABEL NAME to declared LABEL filenames using how many Computer or Users are either found
                    fLABEL.WriteLine sLABELContents
                fLABEL.Close
            End If
        Loop
    fCSV.Close
End If

All help is most appreciated.

Viewing all articles
Browse latest Browse all 699

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>