It’s really appreciated if someone would have help me on this using vbs script.
I have text file with 4 gb data (below sample data--at end it is) and I need only these three columns data out of my attached textfile
"20150901162037","20150901162037","LDAP_Prod/xyrep, tett"
Without duplicate data since I have many of them are similar ldap ids are there.
so i need to take only with unique of ids with three columns,Please can you help me how to achive using vbs script.
Finally I need my output will be following way with out dulicates:
"20150901162037","20150901162037","LDAP_Prod/xyrep, tett"
"20150901162037","20150901162037","LDAP_Prod/ttin_tess"
.
…
my sample data is here:
"100aEB","20150901162037","20150901162037","LDAP_Prod/xyrep, tett","N","11937.91666666667","0","Test_Plan","360610","BAU","Version1","Budget","LDDRR","31029","21240","36542_31029","2016","Jan","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ xyrep, tett ","N","11937.91666666667","0","SS_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Feb","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ttin_tess","N","11937.91666666667","0","LLL_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Aug","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ xyrep, tett ","N","11937.91666666667","0","SS_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Feb","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ttin_tess","N","11937.91666666667","0","LLL_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Aug","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ttin_tess","N","11937.91666666667","0","LLL_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Aug","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ttenetess","N","11937.91666666667","0","LLL_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Aug","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ttin_tess","N","11937.91666666667","0","LLL_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Aug","6050210000",""
Describe your input file using a schema.ini file:
[33851749.txt]
Format=CSVDelimited
ColNameHeader=False
DecimalSymbol=.
Col1=AA Text
Col2=BB Text
Col3=CC Text
Col4=DD Text
(minimalistic/need to know version to get it 'to work'; you surely can do better)
Open a ADODB Connection to your input file, select your 3 columns 'distinct'ly, and write the records quoted to StdOut (or a file):
Option Explicit
Const adClipString = 2
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim oDB : Set oDB = CreateObject("ADODB.Connection")
Dim sCS : sCS = Join(Array( _
"Provider=Microsoft.Jet.OLEDB.4.0" _
, "Data Source=" & oFS.GetAbsolutePathName("..\data") _
, "Extended Properties=""text""" _
), ";")
oDB.Open sCS
Dim sSQL : sSQL = "Select Distinct BB, CC, DD from [33851749.txt]"
Dim oRS : Set oRS = oDb.Execute(sSQL)
Do Until oRS.EOF
WScript.Echo """" & oRS.GetString(adClipString, 1, """,""", """", """""")
Loop
oDB.Close
That should give you:
cscript 33851749.vbs
"20150901162037","20150901162037","LDAP_Prod/ xyrep, tett"
"20150901162037","20150901162037","LDAP_Prod/ttenetess"
"20150901162037","20150901162037","LDAP_Prod/ttin_tess"
"20150901162037","20150901162037","LDAP_Prod/xyrep, tett"
I am trying to read from a csv.txt file using Ado Recordset
I get no results back when trying..
When I copy the contents of the original file into a new text file, and read from that file, it works just fine.
Any ideas what the reason for this might be?
The second file is smaller in size, about 1/2. That's the only difference I can see. This is driving me mad :-)
'Edit
Update with code & schema.ini
Code:
Sub ImportTextFiles()
Dim objAdoDbConnection As ADODB.Connection
Dim objAdoDbRecordset As ADODB.Recordset
Dim strAdodbConnection As String
Dim pathSource As String
Dim filename As String
pathSource = "C:\Users\me\Desktop\Reports\"
filename = "test1.txt"
'filename = "test2.txt"
strAdodbConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & pathSource _
& ";Extended Properties=""text;HDR=yes;FMT=Delimited"";"
Set objAdoDbConnection = CreateObject("Adodb.Connection")
Set objAdoDbRecordset = CreateObject("ADODB.Recordset")
With objAdoDbConnection
.Open (strAdodbConnection)
With objAdoDbRecordset
.Open "Select top 10 * FROM " & filename & " WHERE [Date] > #01/01/2000# ", objAdoDbConnection, adOpenStatic, adLockOptimistic, adCmdText
If Not objAdoDbRecordset.EOF Then objAdoDbRecordset.MoveFirst
Do While Not objAdoDbRecordset.EOF
Debug.Print "Field(0): " & objAdoDbRecordset(0).Value
objAdoDbRecordset.MoveNext
Loop
.Close
End With
.Close
End With
Set objAdoDbRecordset = Nothing
Set objAdoDbConnection = Nothing
End Sub
Schema.ini:
[Test1.txt]
col1=date text
col2=interval integer
col3=application text
[Test2.txt]
col1=date text
col2=interval integer
col3=application text
notepadd++ gave me the answer, file1 is ucs-2 encoded, the newly created utf-8
I have a table in an excel file called " Employee_States_File".This table contains two columns Name and States. Both columns are filled with data. The province table contains abbreviation of the States such as " NE, WA" and so. I have another excel file called " States_File" which contains a table that has two columns: Abbreviation and FullStateName. This table is considered to be a lookup table to look for the full state names based on the abbreviation. Now, I want to write a code in VB6 so that when the user click a button, All the abbreviation names in the table of the excel file " Employee_States" are changes into the full state names based on the lookup table of the excel sheet " States_File".
does it make sense?
Please help,
If you are allowed to convert them to csv files (comma delimited text files) you can use the Jet Database engine and do normal SQL joins.
I use this to setup the connection.
Public Function OpenTextConnection(ByVal FileName As String) As Connection
Dim FSO As FileSystemObject
Dim DBFolder As String
Dim TS As TextStream
Set FSO = New FileSystemObject
DBFolder = FSO.GetParentFolderName(FileName)
If FSO.FileExists(FSO.BuildPath(DBFolder, "Schema.ini")) Then
FSO.DeleteFile (FSO.BuildPath(DBFolder, "Schema.ini"))
End If
Set TS = FSO.CreateTextFile(FSO.BuildPath(DBFolder, "Schema.ini"))
TS.WriteLine "[" & FSO.GetFileName(FileName) & "]"
TS.WriteLine "Format=CSVDelimited"
TS.WriteLine "ColNameHeader = True"
TS.WriteLine "MaxScanRows = 0"
TS.Close
Set OpenTextConnection = New Connection
If FSO.FolderExists(DBFolder) Then
OpenTextConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBFolder & ";Extended Properties=""text;HDR=Yes;FMT=Delimited;"";"
Else
MsgBox DBFolder & " Does not exists.", vbExclamation
End If
End Function
Each file is a table in the connection and you can do SQL joins. Note the example is just a simply open up a table. You can use any valid SQL Syntax.
Dim DB1 As Connection
Dim TB As Recordset
Dim FSO As FileSystemObject
Dim tImport As New DBImportList
Set FSO = New FileSystemObject
Set tImport = New DBImportList
If FSO.FileExists(FileName) Then
Set DB1 = OpenTextConnection(FileName)
Set TB = New Recordset
TB.Open "SELECT * FROM [" & FSO.GetFile(FileName).Name & "]", DB1, adOpenKeyset, adLockOptimistic, adCmdText
End If
I get a compilation error when I try to run the following vbs code from a command prompt in Windows 7.
Option Explicit
Dim objNetwork, strRemotePath1, strRemotePath2, strRemotePath3
Dim strDriveLetter1, strDriveLetter2, strDriveLetter3, strUserName
Set objNetwork = CreateObject("WScript.Network")
strUserName = objNetwork.UserName
strDriveLetter1 = "H:"
strDriveLetter2 = "P:"
strDriveLetter3 = "S:"
strRemotePath1 = "\\test\public\users\" & strUserName & "\"
strRemotePath2 = "\\test\public\groups\"
strRemotePath3 = "\\test\scans\"
'Section which maps two drives, M: and P: and S:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2
objNetwork.MapNetworkDrive strDriveLetter3, strRemotePath3
'Extra code just to add a message box
WScript.Echo "Map drives " & strDriveLetter1 & " & " & strDriveLetter2 & " & " & strDriveLetter3
Wscript.Quit
It says the issue is with Line 1 Char 1. Any ideas?
A possible problem is how your file was encoded; try to save it as ANSI and run it again.
FYI for those with the same problem in the future, to fix this:
Open the .vbs in notepad
Go to file and "save as"
Right under the file name box, you will see a drop down menu for encoding. Choose ANSI.
Check you are not using extended charater like á é í ó ú ñ in a variable's name