Hi NeoPA & experts
Ref. my post, I found a solution for my post, which is looking one specific path and merge all files and provide an output with all files merged. This is ok, but I dont know how to read multiple paths with below code at the same time and merge all files and provide single output.
Also, another question related to this, after importing this merged data to an access table I want to delete the duplicate records based on few fields uniqueness not all fields. I have a code which can delete all the duplicate records if all fields are having same value. The second code in this post does a check on all fields and then delete the duplicate record. Can anyone guide me how to modify both codes to get the expected results
Code for merging txt files in a specific path as below:
- Sub MyCombineFiles()
-
Dim myFileDir As String
-
Dim myFileExt As String
-
Dim myFinalFileName As String
-
' This process will combine all text files in a directory into one single file (should only be used with text files)
-
myFileDir = "text file directory"
-
myFileExt = ".txt"
-
myFinalFileName = "output file name.txt"
-
-
Dim fname
-
Dim TextLine As String
-
Dim myCombinedFile As String
-
-
' Create output file
-
myCombinedFile = myFileDir & myFinalFileName
-
Open myCombinedFile For Output As #1
-
-
' Loop through all files with designated extension in the directory
-
fname = Dir(myFileDir & "*" & myFileExt)
-
While (fname <> "") And (fname <> myFinalFileName)
-
' Open file and write data to output file
-
Open myFileDir & fname For Input As #2
-
Do While Not EOF(2)
-
Line Input #2, TextLine
-
Print #1, TextLine
-
Loop
-
Close #2
-
' Delete initial file after it is merged (comment this line out if you do not want to delete initial files)
-
-
'Kill myFileDir & fname
-
-
' Go to the next file
-
-
fname = Dir()
-
Wend
-
Close #1
-
-
End Sub
-
Code for duplicate deletion as below
- Sub DeleteDuplicateRecords()
-
Dim StrTableName As String
-
-
StrTableName = "dupetest"
-
-
' Deletes exact duplicates from the specified table.
-
' No user confirmation is required. Use with caution.
-
Dim rst As DAO.Recordset
-
Dim rst2 As DAO.Recordset
-
Dim tdf As DAO.TableDef
-
Dim fld As DAO.Field
-
Dim strSQL As String
-
Dim varBookmark As Variant
-
-
Set tdf = DBEngine(0)(0).TableDefs(StrTableName)
-
strSQL = "SELECT * FROM " & StrTableName & " ORDER BY "
-
' Build a sort string to make sure duplicate records are
-
' adjacent. Can't sort on OLE or Memo fields,though.
-
For Each fld In tdf.Fields
-
If (fld.Type <> dbMemo) And (fld.Type <> dbLongBinary) Then
-
strSQL = strSQL & "[" & fld.Name & "]" & ", "
-
End If
-
Next fld
-
' Remove the extra comma and space from the SQL[/COLOR]
-
strSQL = Left(strSQL, Len(strSQL) - 2)
-
Set tdf = Nothing
-
-
Set rst = CurrentDb.OpenRecordset(strSQL)
-
Set rst2 = rst.Clone
-
rst.MoveNext
-
Do Until rst.EOF
-
varBookmark = rst.Bookmark
-
For Each fld In rst.Fields
-
If fld.Value <> rst2.Fields(fld.Name).Value Then
-
GoTo NextRecord
-
End If
-
Next fld
-
rst.delete
-
GoTo SkipBookmark
-
-
-
NextRecord:
-
rst2.Bookmark = varBookmark
-
SkipBookmark:
-
rst.MoveNext
-
Loop
-
End Sub
-
-