Share via

Finding specific header in multiple csv files

Anonymous
2023-07-12T09:25:26+00:00

Hi,

I have 94 csv files placed in a folder. Each has it's own header, all files don't have common headers. My work is to find does all the file has one specific header or not. Is there any option available to achieve this. Since power query uses column headers appending all files doesn't give me expected output.
Just whether "Ex: ABC" header is there in each file or not. Need to check this in all 94 files.

Thanks in advance !!

Microsoft 365 and Office | Excel | For education | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2023-07-12T11:20:05+00:00

How do you want to output the results?

The objective is to know whether that specific header is present or not for all individual files. So any method is okay with the help of excel.

Power Query \ Get Data \ From File \ From Folder

Choose your folder \ Transform Data

Add a custom column "Header" using this MCode:
=Text.Split(Text.BeforeDelimiter(Text.FromBinary([Content]),"#(cr)"),",")

Expand the Header column to new rows

This are all headings from each file, filter the column and you know the files that contains the header you are looking for.

Andreas.

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2023-07-12T10:37:25+00:00

Try running this macro:

Sub Check4Header()
    Dim wsh As Worksheet
    Dim r As Long
    Dim f As Long
    Dim strFolder As String
    Dim strHeader As String
    Dim strFile As String
    Dim strLine As String

    With Application.FileDialog(4) ' msoFileDialogFolderPicker
        If .Show Then
            strFolder = .SelectedItems(1)
        Else
            Beep
            Exit Sub
        End If
    End With
    strHeader = InputBox("Enter the header to search for")
    If strHeader = "" Then
        Beep
        Exit Sub
    End If
    Set wsh = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    wsh.Range("A1:B1").Value = Array("File Name", "Header Found")
    r = 1
    If Right(strFolder, 1) <> "\" Then
        strFolder = strFolder & "\"
    End If
    strFile = Dir(strFolder & "*.csv")
    Do While strFile <> ""
        r = r + 1
        wsh.Range("A" & r).Value = strFile
        f = FreeFile
        Open strFolder & strFile For Input As #f
        Line Input #f, strLine
        Close #f
        wsh.Range("B" & r).Value = (InStr(1, strLine, strHeader, vbTextCompare) > 0)
        strFile = Dir
    Loop
End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2023-07-12T11:27:03+00:00

    Yeah !! It works perfect. Thanks a lot for your help :)

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-07-12T09:58:05+00:00

    How do you want to output the results?

    The objective is to know whether that specific header is present or not for all individual files. So any method is okay with the help of excel.

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2023-07-12T09:49:20+00:00

    How do you want to output the results?

    Was this answer helpful?

    0 comments No comments