swaggerbox
06-27-2015, 02:56 AM
I have a macro that searches for a particular string in multiple text files in a folder and if it finds a match, copies the file to another location. The code below simply just that. How do I modify this code so that IT COPIES THE FILES that do NOT match the search string?
Sub StringExistsInFile()
Dim theString As String
Dim path As String
Dim StrFile, NewFile As String
Dim fso As New FileSystemObject
Dim file As TextStream
Dim line As String
theString = "<DWG> 0/0"
path = "C:\MyData\"
StrFile = Dir(path & "*.txt")
Do While StrFile <> ""
Set file = fso.OpenTextFile(path & StrFile)
Do While Not file.AtEndOfLine
line = file.ReadLine
If InStr(1, line, theString, vbTextCompare) > 0 Then
FileCopy path & StrFile, "C:\MyData\1_TIF\" & StrFile
Exit Do
Else
End If
Loop
file.Close
Set file = Nothing
Set fso = Nothing
StrFile = Dir()
Loop
End Sub
Sub StringExistsInFile()
Dim theString As String
Dim path As String
Dim StrFile, NewFile As String
Dim fso As New FileSystemObject
Dim file As TextStream
Dim line As String
theString = "<DWG> 0/0"
path = "C:\MyData\"
StrFile = Dir(path & "*.txt")
Do While StrFile <> ""
Set file = fso.OpenTextFile(path & StrFile)
Do While Not file.AtEndOfLine
line = file.ReadLine
If InStr(1, line, theString, vbTextCompare) > 0 Then
FileCopy path & StrFile, "C:\MyData\1_TIF\" & StrFile
Exit Do
Else
End If
Loop
file.Close
Set file = Nothing
Set fso = Nothing
StrFile = Dir()
Loop
End Sub