Read a line from several .txt files and write them into created file

眉间皱痕 提交于 2020-07-19 07:06:48

问题


I have a quite simple task.

There is a folder which contains several files with different extensions. I need to make a script which will find all files with .txt extension in this folder, read first line from every file and then write all first lines in newly created file.

For now, I've ended up with something like this:

Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim f, colFiles , objFile
Dim tFolder, tFile
Dim lineToCopy, fileContents
Dim input, output

Set tFolder = fso.GetFolder("C:\Temp")
Set tFile = tFolder.CreateTextFile("test.txt", true) 

Set f = fso.GetFolder("D:\Folder")
Set colFiles = f.Files
    For Each objFile in colFiles
        If LCase(fso.GetExtensionName(objFile.name)) = "txt" Then
            Set input = fso.OpenTextFile(LCase(objFile.name))
            If Not input.AtEndofStream Then lineToCopy = input.ReadLine
            input.close
            output = fso.OpenTextFile(tFolder, True)
            output.WriteLine lineToCopy
            output.close
        End If
    Next

WScript.sleep 60000000

When activated, .vbs file tells me he couldn't find the file from that line:

Set input = fso.OpenTextFile(LCase(objFile.name))

I suppose that happens because IF LCASE<...> block doesn't understand folder contents as .txt files. Where am I wrong and what is needed to be done to solve that problem?

Kindly yours, Richard


回答1:


Use the full .Path of the file for OpenTextFile or get the stream via OpenAsTextStream. Use tFile instead of repeatedly creating output. Delete all the risky/cargo cult fat:

Option Explicit

Dim fso   : Set fso = CreateObject("Scripting.FileSystemObject")
Dim tFile : Set tFile = fso.CreateTextFile(fso.BuildPath(".\", "test.txt"))
Dim oFile

For Each oFile in fso.GetFolder("..\data").Files
    If LCase(fso.GetExtensionName(oFile.Path)) = "txt" Then
'      Dim input: Set input = fso.OpenTextFile(LCase(oFile.Path))
       Dim input: Set input = oFile.OpenAsTextStream()
       If Not input.AtEndofStream Then tFile.WriteLine input.ReadLine()
       input.Close
    End If
Next

tFile.Close



回答2:


Looks like I've found my own decision:

Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim f, colFiles , objFile
Dim tFolder, tFile
Dim lineToCopy, readFile

Set tFolder = fso.GetFolder("C:\Temp")
Set tFile = tFolder.CreateTextFile("test.txt", true) 

Set f = fso.GetFolder("D:\Scripting Games 2008\Beginner")
Set colFiles = f.Files
    For Each objFile in colFiles
        If LCase(fso.GetExtensionName(objFile.name)) = "txt" Then
        REM Preceding passage finds all .txt files in selected folder
            Set readFile = objFile.OpenAsTextStream
            lineToCopy = ""
                Do Until lineToCopy <> "" Or readfile.atEndOfStream
                    lineToCopy = Trim(readFile.ReadLine)
                Loop
                REM Extracts first line of the text, if it is not empty
            tFile.WriteLine objFile.name & ": " & lineToCopy
        End If
    Next

Still, thanks for the answers. I've found some interesting solutions which well be of use some time.

Kindly yours, Richard



来源:https://stackoverflow.com/questions/36078265/read-a-line-from-several-txt-files-and-write-them-into-created-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!