简体   繁体   English

从众多文本文件中提取单行数据并导入Excel

[英]Extract a single line of data from numerous text files and import into Excel

I have hundreds of text files in a folder and I need to extract a single line from each one and put the info into excel. 我在一个文件夹中有数百个文本文件,我需要从每个文件夹中提取一行并将信息放入excel中。 The text files contain all the metadata for individual photographs and I need to take out just the GPS coordinates. 文本文件包含单个照片的所有元数据,我只需要取出GPS坐标。

I have looked through various other similar threads eg: extract data from multiple text files in a folder into excel worksheet 我查看了各种其他类似的线程,例如: 将文件夹中多个文本文件的数据提取到excel工作表中

and: 和:

http://www.mrexcel.com/forum/excel-questions/531515-visual-basic-applications-retrieve-data-text-file.html (sorry, not stackoverflow!) http://www.mrexcel.com/forum/excel-questions/531515-visual-basic-applications-retrieve-data-text-file.html (对不起,不是stackoverflow!)

and many others, but can't quite get it to work. 和许多其他人一样,但不能完全开始工作。 I'm close but not quite there. 我很近但不太相似。

The data in each of the textfiles is set out like this: 每个文本文件中的数据如下所示:

... ...

---- Composite ----
Aperture                        : 3.8
GPS Altitude                    : 37.2 m Above Sea Level
GPS Date/Time                   : 2014:05:15 10:30:55.7Z
GPS Latitude                    : 50 deg 7' 33.40" N
GPS Longitude                   : 5 deg 30' 4.06" W
GPS Position                    : 50 deg 7' 33.40" N, 5 deg 30' 4.06" W
Image Size                      : 4608x3456

... ...

I have written the following code: 我写了以下代码:

Sub ExtractGPS()
    Dim filename As String, nextrow As Long, MyFolder As String
    Dim MyFile As String, text As String, textline As String, posGPS As String

    MyFolder = "C:\Users\Desktop\Test\"
    MyFile = Dir(MyFolder & "*.txt")

    Do While MyFile <> ""
        Open (MyFolder & MyFile) For Input As #1
        Do Until EOF(1)
            Line Input #1, textline
            text = text & textline
        Loop

        Close #1
        MyFile = Dir()
        posGPS = InStr(text, "GPS Position")
        nextrow = Sheet1.Cells(Rows.Count, "A").End(xlUp).row + 1
        Sheet1.Cells(nextrow, "A").Value = Mid(text, posGPS + 33, 37)
    Loop
End Sub

It appears to open each of the text files and look through them but only extracts the GPS coordinates from the first file and repeatedly puts this in excel so I end up with hundreds of rows filled with the same data - the GPS coordinates from the first file in the folder. 它似乎打开每个文本文件并浏览它们但只从第一个文件中提取GPS坐标并重复将其放入excel中,所以我最终得到数百行填充相同数据 - 来自第一个文件的GPS坐标在文件夹中。

If anyone can help me to finish this last bit off it would be greatly appreciated! 如果有人能帮我完成最后一点,我将不胜感激!

Thanks 谢谢

You have to reset your text otherwise the content of the second file is added and not replaced and the search always find the first GPS data and stop searching: 您必须重置text否则将添加第二个文件的内容而不是替换,搜索始终会找到第一个GPS数据并停止搜索:

Sub ExtractGPS()
    Dim filename As String, nextrow As Long, MyFolder As String
    Dim MyFile As String, text As String, textline As String, posGPS As String

    MyFolder = "C:\Temp\Test\"
    MyFile = Dir(MyFolder & "*.txt")

    Do While MyFile <> ""
        Open (MyFolder & MyFile) For Input As #1
        Do Until EOF(1)
            Line Input #1, textline
            text = text & textline 'second loop text is already stored -> see reset text
        Loop
        Close #1
        MyFile = Dir()
        Debug.Print text
        posGPS = InStr(text, "GPS Position")
        nextrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
        ActiveSheet.Cells(nextrow, "A").Value = Mid(text, posGPS + 33, 37)
        text = "" 'reset text
    Loop
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM