Excel Macro宏 - 指定路径下两个TXT文件Copy后,导入Excel中并进行对比

由于Excel格式粘贴没时间,只写关键的VBA代码和TXT文档Format示例‘

Attribute VB_Name = "CopyAndDiffer"

‘2015/06/24 ADD By MAOUIZAYOI

Option Explicit

‘ Excute Interface.
Sub CopyAndDiffer()

‘ define variables
    Dim ccnSht As Object       ‘ccnSht is named for current console sheet.
    Dim pathRng As Range
    Dim fileNameRng As Range
    Dim fieldsRng As Range
    Dim aRng As Range
    Dim bRng As Range

Dim itemFilter, strPath, strFileInfo, endMark, fiA, fiB, strInput, strData, pathFilter, strTmpPath, strTmpFn As String

Dim parentPath As String

Dim i, j As Long
    
    ‘ initialize values
    Set ccnSht = ActiveSheet.UsedRange
    Set pathRng = ccnSht.Range("A2")
    Set fileNameRng = ccnSht.Range("B2")
    Set fieldsRng = ccnSht.Range("C2")
    Set aRng = ccnSht.Range("D2")
    Set bRng = ccnSht.Range("F2")
    
    itemFilter = ":"
    pathFilter = "Path:"
    strPath = "Path"
    strFileInfo = "FileName"
    endMark = "END"
    
    parentPath = FindParentPath
    
    fiA = parentPath & "\src\a.txt"
    fiB = parentPath & "\src\b.txt"

i = 1
    j = 2
    
    ‘ main handle
    ‘ Loop1 to deal with result of fiB
    Open fiA For Input As #1
    Do While Not EOF(1)
    
        ‘one row data from TXT file
        Line Input #1, strInput
        
        strData = strInput

If InStr(strData, pathFilter) > 0 Then
                Dim strLine1(0 To 1) As String
                strLine1(0) = strPath
                strLine1(1) = Split(strData, pathFilter)(1)
                
                
                If strLine1(0) = strPath Then

‘ path cell name pair value.
                    Cells(j, pathRng.Column) = strLine1(1)
                    
                    strTmpPath = strLine1(1)
                End If
            Else
                Dim strLine2() As String
                strLine2 = Split(strData, itemFilter)

If strLine2(0) = strPath Then

‘ path cell name pair value.
                    Cells(j, pathRng.Column) = strLine2(1)
                    
                    strTmpPath = strLine2(1)
                ElseIf strLine2(0) = strFileInfo Then

‘ fileName cell name pair value.
                    Cells(j, fileNameRng.Column) = strLine2(1)
                    
                    strTmpFn = strLine2(1)
                ElseIf strLine2(0) = endMark Then

‘ a file info group is over, evaluating next group rowNo.
‘                    j = j + 1
                Else

‘ field cell name pair value.
                    Cells(j, pathRng.Column) = strTmpPath
                    Cells(j, fileNameRng.Column) = strTmpFn
                    Cells(j, fieldsRng.Column) = strLine2(0)
                    Cells(j, aRng.Column) = strLine2(1)
                    
                    j = j + 1
                End If
            End If

i = i + 1
    Loop
    Close #1

i = 1
    j = 2
    strTmpPath = ""
    strTmpFn = ""

‘ Loop2 to deal with result of b
    Open fiB For Input As #1
    Do While Not EOF(1)
    
        ‘one row data from TXT file
        Line Input #1, strInput
        
        strData = strInput

If InStr(strData, pathFilter) > 0 Then
                Dim strLine3(0 To 1) As String
                strLine3(0) = strPath
                strLine3(1) = Split(strData, pathFilter)(1)
                
                
                If strLine3(0) = strPath Then

‘                    ‘ path cell name pair value.
‘                    Cells(j, pathRng.Column) = strLine3(1)
                    
                    strTmpPath = strLine3(1)
                End If
            Else
                Dim strLine4() As String
                strLine4 = Split(strData, itemFilter)

If strLine4(0) = strPath Then

‘                    ‘ path cell name pair value.
‘                    Cells(j, pathRng.Column) = strLine4(1)
                    
                    strTmpPath = strLine4(1)
                ElseIf strLine4(0) = strFileInfo Then

‘                    ‘ fileName cell name pair value.
‘                    Cells(j, fileNameRng.Column) = strLine4(1)
                    
                    strTmpFn = strLine4(1)
                ElseIf strLine4(0) = endMark Then

‘ a file info group is over, evaluating next group rowNo.
‘                    j = j + 1
                Else

‘ field cell name pair value.
‘                    Cells(j, pathRng.Column) = strTmpPath
‘                    Cells(j, fileNameRng.Column) = strTmpFn
‘                    Cells(j, fieldsRng.Column) = strLine4(0)
                    Cells(j, bRng.Column) = strLine4(1)
                    
                    j = j + 1
                End If
            End If

i = i + 1
    Loop
    Close #1

End Sub

‘ To find ParentPath of Excel
Function FindParentPath()
    Dim curPath As String
    Dim temp As Integer
    Dim strPos As Integer
    
    curPath = ThisWorkbook.Path
    
    For temp = Len(curPath) To 1 Step -1
        strPos = InStr(temp, curPath, "\", vbTextCompare)
        If strPos <> 0 Then
            Exit For
        End If
    Next temp
    
    FindParentPath = Mid(curPath, 1, strPos - 1)
End Function

------------------------------------------------------------------------------

Txt file format e.g.

Path:D:\work\test.jpg
FileName:test.jpg
param1:1
....
END
next item info
...
END

时间: 2024-10-26 00:38:59

Excel Macro宏 - 指定路径下两个TXT文件Copy后,导入Excel中并进行对比的相关文章

访问指定路径下的目录以及文件

#include "stdafx.h" //vs2010下运行通过 #undef UNICODE #include <stdio.h> #include <stdlib.h> #include <Windows.h> #include <iostream> using namespace std; void browseFile(char* path) { char pattern[FILENAME_MAX + 1]; sprintf(p

SSIS【Foreach 循环容器_Foreach 文件枚举器】(导入路径下的所有txt文件的内容) (转)

原文:http://blog.csdn.net/kk185800961/article/details/12276449 SQLServer 2008 R2 SSIS_Foreach 循环容器_Foreach 文件枚举器(导入路径下的所有txt文件的内容) 1. 拖动一个 [Foreach 循环容器]到[控制流]中,再拖动一个[数据流任务]到[Foreach 循环容器]中.如图: 2.编辑[Foreach 循环容器],在选项[集合]中选择[Foreach 文件枚举器],配置要遍历的文件夹及文件类

获取指定路径下特定后缀的文件

# 获取指定路径下所有指定后缀的文件# dir 指定路径# ext 指定后缀,链表&不需要带点 或者不指定.例子:['xml', 'java']import osdef GetFileFromThisRootDir(dir,ext = None): allfiles = [] needExtFilter = (ext != None) for root,dirs,files in os.walk(dir): for filespath in files: filepath = os.path.j

删除指定路径下固定格式,以.log结尾、三天前的文件,或删除空的日志文件

师出‘百测’besttest 删除指定路径下固定格式,以.log结尾.三天前的文件,或删除空的日志文件. 日志文件格式:XXXX_2019-01-01.log. import os,datetime #判断文件是否为空,如果为空则删除 def del_file(file): with open(file,encoding='utf-8') as f: if f.read(): pass else: f.close() os.remove(file) #查找指定路径下xx结尾的文件 def sea

Java版本的删除指定目录下名叫“xxx.txt”文件名的文件

以前写过一个python版本的,但是在查找文件路径的时候出现错误,无法正确的获取到文件的路径,就造成无法删除该路径下的“xxx.txt”文件. 当时以为是windows版本系统的错误造成这个问题的,也就没有继续深究,就把这个bug给放过了. 最近一段时间在学习android,肯定要用的java了,就用java实现了下,思路的话,肯定还是以前的了,结果还是出错,后来仔细的看了看代码,其实是自己的bug. 把代码贴下:(主要功能,删除指定目录下名叫"pylist.txt"的所有文件,使用递

JAVA之File类-将指定目录下的所有java文件的绝对路径存储到文本文件中

/* * 将指定目录下的所有java文件的绝对路径存储到文本文件中 * 建立一个java列表. * 思路: * 1.对指定目录进行递归 * 2.获取递归过程所有的java文件的路径 * 3.将这些路径存储在集合中 * 4.将集合中的内容写到文本文件 * 注:3,4步骤也可以合并成一个步骤 */ package ioTest.io3; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; imp

删除指定路径下指定天数之前(以文件的最后修改日期为准)的文件

1 @echo off 2 ::删除指定路径下指定天数之前(以文件的最后修改日期为准)的文件. 3 ::本例需要Win2003/Vista/Win7系统自带的forfiles命令的支持 4 rem 指定待删除文件的存放路径 5 set SrcDir=D:\ajaxtrain\web 6 rem 指定天数 7 set DaysAgo=0 8 rem 要删除的文件通配 9 set searchmast=*.html 10 set filename=D:\ajaxtrain\web\deletefil

C#实现把指定文件夹下的所有文件复制到指定路径下以及修改指定文件的后缀名

1.实现把指定文件夹下的所有文件复制到指定路径下 1 public static void copyFiles(string path) { 2 DirectoryInfo dir = new DirectoryInfo(path); 3 if (!dir.Exists) 4 return; //获得指定文件夹内的所有的子目录信息 5 DirectoryInfo[] dirs = dir.GetDirectories(); //获得指定文件夹内所有的文件信息 6 FileInfo[] files

拷贝一个目录或者文件到指定路径下

/** * 拷贝一个目录或者文件到指定路径下 * * @param source * @param target */ public static void copy(File source, File target) { File tarpath = new File(target, source.getName()); if (source.isDirectory()) { tarpath.mkdir(); File[] dir = source.listFiles(); for (int