在多个.txt文件中从一个电子表格中查找多个值

在多个.txt文件中从一个电子表格中查找多个值

我需要在 17 个 .txt 文件中查找 60 个唯一值。这通常是一个简单的“查找”和复制,当我一次收到一个或两个值时,这显然不适合这么大的列表。

电子表格中有大约 60 个帐号。我需要在 17 个 .txt 文件中的一个中找到每个帐号,这样我就可以将该帐号行复制到另一个 .txt 文件中进行编辑/完成。.txt 文件位于 ftp 存档中,因此我必须小心不要更改 .txt 文件,尽管如果需要,我可以将它们复制到 excel。

我知道有查找功能,但我还不太熟悉它们,也不确定我到底需要什么。如果能得到一些帮助,我将不胜感激!谢谢,L

答案1

我编写了一个脚本,您可能会觉得有用。

创建一个名为的文件夹账户并以 .vbs 扩展名保存该脚本。

在帐户文件夹中创建一个名为输入. 将所有要搜索的账户文件复制到输入文件夹,它们都应该是.txt 文件,否则将被忽略。

创建一个名为账户号码.txt并在单独的行中列出每个帐号。脚本应打开每个文件,读取一行并根据每个帐号进行检查。搜索结果存储在账户信息.txt。不提供保修,使用风险自负。希望对您有帮助。

' find all occurrances of an account number in a number of files
' place all account txt files into input folder
' accounts_found.txt is created if it is missing
' by 'Robert' 2017 - hereby placed into the Public Domain

' set some Constants here
Const ForRead      = 1
Const ForWrite     = 2
Const ForAppend    = 8
Const OverWrite    = True
Const NoOverWrite  = False

Dim acct_num_list(100) ' max records for the array, num_accounts variable counts them anyway

inputFolderName    = "input\"               ' the folder where the txt files are
outputFileName     = "accounts_found.txt"   ' the output file for matched records
accountsFileName   = "account_numbers.txt"  ' the file which contains account numbers to search for

' = = = Start of Code = = =

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set outFile = objFSO.OpenTextFile(outputFileName, ForWrite, OverWrite)

Set objInputFolder = objFSO.GetFolder(inputFolderName)
Set sFileCollection = objInputFolder.Files

Set accounts_file = objFSO.OpenTextFile(accountsFileName, ForRead)

num_accounts = 0
DO While Not accounts_file.AtEndOfStream
    acct_num_list(num_accounts) = accounts_file.ReadLine
    num_accounts = num_accounts + 1
Loop

accounts_file.Close

For Each objFile in sFileCollection
    If UCASE(objFSO.GetExtensionName(objFile)) = "TXT" then
        Set sourceFile = objFSO.OpenTextFile(inputFolderName & objFile.Name, ForRead)
        Do While Not sourceFile.AtEndOfStream
            curr_line = sourceFile.ReadLine
            For counter = 0 to num_accounts
                if instr(1, curr_line, acct_num_list(counter),1) >= 1 then
                    outFile.WriteLine curr_line
            End If
            Next    
       Loop 
    End If    
Next
' = = = End Of File = = =

相关内容