mr.donaldson
Registered User.
- Local time
- Today, 15:58
- Joined
- Apr 6, 2013
- Messages
- 10
I am hoping to get some help to get me through this issue. I am trying to copy files from a listbox to a directory. I need to check if a file exist and if it does, append to the existing file. The filenames are similar in that the source file is "TESTMAIN60" and the destination file is "TESTMAIN" I set an array with the filename strings and am trying to loop through the directory to check for the existing file. If the source filename matches a destination filename, I know I should open the source for reading and open the destination file for appending. I need help incorporating everything. Below is the code I have now, but even the check file portion is not running correctly. I receive compile error: Next without for. The first portion of the code replaces the file extension as .txt. If I am going about this the wrong way, please direct me. Any help is appreciated.
Code:
Private Sub Command111_Click()
Dim CheckFile, FileFound As String, AllOk As Boolean, i As Integer, ErrMsg As String
Dim sDest As String, strFileTemp As String, FileExist As String
Dim ctlList As Control, varItem As Variant, posn As Integer, FileName As String
CheckFile = Array("*TESTMAIN*", "*TESTCASE*", "*TESTPRIMARY*", "*TESTSECONDARY*", "*TESTALTERNATE*")
'The following lines of code use a network path for the source file :
sDest = CurrentProject.path & "\Test Folder\" s
' Return Control object variable pointing to Me.FileList list box.
Set ctlList = Me.FileList
' Enumerate through selected items.
For varItem = 0 To ctlList.ListCount - 1
' Print value of bound column - used for testing purposes.
'MsgBox ctlList.ItemData(varItem)
For x = 1 To Len(ctlList.ItemData(varItem))
' Parse filename only
If Mid(ctlList.ItemData(varItem), x, 1) = "\" Then posn = x
Next x
FileName = Right(ctlList.ItemData(varItem), Len(ctlList.ItemData(varItem)) - posn)
' Print value of bound column - used for testing purposes.
MsgBox FileName, vbInformation, "For Testing"
' Replace the extension with _Temp.txt for a temp file
strFileTemp = Replace(ctlList.ItemData(varItem), ".dat", "_Temp.txt")
For i = LBound(CheckFile) To UBound(CheckFile)
FileFound = Dir(MyPath & "\" & CheckFile(i))
If FileFound = "" Then
AllOk = False
ErrMsg = ErrMsg & CheckFile(i) & vbNewLine
End If
Next i
If Not AllOk Then
'MsgBox strFileTemp, vbInformation, "FileExist"
' Copy .txt files
FileCopy ctlList.ItemData(varItem), sDest & strFileTemp
' Reset file extension .dat
FileName = Right(ctlList.ItemData(varItem), Len(ctlList.ItemData(varItem)) - posn)
' Copy dat file
FileCopy ctlList.ItemData(varItem), sDest & FileName
Next varItem
' Print file storage location
MsgBox CurrentProject.path & "\Test Folder\", vbInformation, "Files Have Been Copied To The Below Location:"
'Kill strFileTemp
' Check that all files in Me.FileList copied MaximoExports directory
If Dir(CurrentProject.path & "\Test Folder\") = FileName Then
MsgBox "All files were successfully imported"
Else
MsgBox "All Files Did Not Import. Return To Import Menu"
End If
End Sub