Can Excel VBA check a file for a string before opening it?

USAMax

Well-known Member
Joined
May 31, 2006
Messages
843
Office Version
  1. 365
Platform
  1. Windows
I have the file path (FilePath) and file name (FileName) for the downloaded file but I need to know if there is a way to check the file for the string, "Over 500 results" before opening it.

Can the file be tested for the string?
 
It should look like this. The space in the folder name DuPage County may possibly cause an error. The space may need to be handled as well, but I would first test it on a different folder to make sure it works first.

Code:
s = CreateObject("Wscript.Shell").Exec("cmd /c find /c /i ""Over 500 results"" C:\Users\David\Downloads\Markets\IL\DuPage County\60540.csv").StdOut.ReadAll
 
Last edited:
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
If you want a purely VBA way to do it here is a function you could use:

Code:
Function TextIsInFile(FilePath As String, TextToFind As String, Optional Compare As VbCompareMethod = vbBinaryCompare) As Boolean

Dim File As Long
Dim Line As String

    TextIsInFile = False
    File = FreeFile()
    Open FilePath For Input As #File
    
    Do Until EOF(File)
        Line Input #File, Line
        
        If InStr(1, Line, TextToFind, Compare) > 0 Then
            TextIsInFile = True
            Exit Do
        End If
    Loop
    
    Close #File

End Function

I prefer using the Scripting Runtime FileSystem objects for reading files because the code is more readable. To do so you could use this function:

Code:
Function TextIsInFile(FilePath As String, TextToFind As String, Optional Compare As VbCompareMethod = vbBinaryCompare) As Boolean

Dim File As Object

    TextIsInFile = False
    Set File = CreateObject("Scripting.FileSystemObject").OpenTextFile(FilePath)
    
    Do Until File.AtEndOfStream
        If InStr(1, File.ReadLine, TextToFind, Compare) > 0 Then
            TextIsInFile = True
            Exit Do
        End If
    Loop
    
    File.Close

End Function
 
Upvote 0
Thank you LockeGarmin, it looks like your solution is still opening the file but I could be wrong. I am planning to test variations of chicagocomputerclasses's idea but I will try yours first.

Thank you so much for your approach.
 
Upvote 0
You're welcome!

If you mean "does this solution open the file in Excel", then no, it does not. But if you mean "does this solution open the file, period" then yes, but something is going to have to open the file in some way to see if it has the content you're looking for whether that be a command prompt in chicagocomputerclasses's or a text stream in my case. The main advantage with the approaches I put out is that it will examine one line at a time in memory until it finds the content, instead of putting the whole file in memory and searching. It may be advantageous to put the whole file in memory if the text you're searching for is always at the end of the file and you've got a really big file, but I wouldn't worry about that until you visibly notice a significant slow down from either of these functions.
 
Upvote 0
Frankly the only reason I would use "Wscript.Shell" in this case would be if I was trying to do a search in many files at a time.
Find will allow you to pass multiple files at the same time and search the string in all of them returning one stream with results. You could search the entire folder with csv files and quickly figure out which ones have the string in them.

If you are checking one file at a time then it's not worth the headache.
 
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,321
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top