File 递归搜索文件以查找excel单元格和返回路径中包含的信息

File 递归搜索文件以查找excel单元格和返回路径中包含的信息,file,search,excel,recursion,vba,File,Search,Excel,Recursion,Vba,奥克多克。。。我有一个Excel电子表格,文件名在a列。a列中列出的文件名出现在一个或多个源目录中的一个或多个文本文件中 我需要Excel递归搜索文本文件,并将包含A列中指定文件名的文件路径返回到B列。如果有多个文件转到C列等 Excel表格将是 __________________________________ __|______A___________|______B_____| 1 | filename.avi | | 2 | another_file.fl

奥克多克。。。我有一个Excel电子表格,文件名在a列。a列中列出的文件名出现在一个或多个源目录中的一个或多个文本文件中

我需要Excel递归搜索文本文件,并将包含A列中指定文件名的文件路径返回到B列。如果有多个文件转到C列等

Excel表格将是

__________________________________
__|______A___________|______B_____|
1 | filename.avi     |            |
2 | another_file.flv |            |
要搜索的文本文件将位于C:\WebDocs\下的多个目录中,并且是DokuWiki页面,有些页面非常短,例如需要返回的此页面

===== Problem Description =====
Reopen a closed bank reconciliation.

===== Solution =====
Demonstration of the tool box routine that allows reposting of the bank rec.

{{videos:bank_rec_reopen1006031511.flv|}}

===== Additional Information -cm =====
You may have noticed that in the video there is a number to the right of the bank account number. In this case it was a 0. That indicates department 0 which is all departments. You get the department 0 if you have all departments combined using the option in the bank set up called "One Bank for All Departments". If this setting is not checked then when you create your starting bank rec for each department you will get a 1 to the right of the bank rec for department 1 and so on. You should normally only have a 0, or have numbers 1 or greater. If you have both, then the method was changed after the initial bank rec was made. You just have to be aware of this as you move forward. As always backup before you make any changes.
还有一些页面虽然很长,但不包含视频,但会在被搜索的目录中。格式相同,纯文本,==是标题的占位符,可能包含指向其他页面/站点的链接

我确实找到了一个现有的VBA脚本,它可以满足我的需要。它不会递归并返回太多信息,例如日期/时间戳,我只需要路径

Private Sub CommandButton1_Click()

Dim sh As Worksheet, rng As Range, lr As Long, fPath As String
Set sh = Sheets(1) 'Change to actual
lstRw = sh.Cells.Find(What:="*", After:=sh.Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
Set rng = sh.Range("A2:A" & lstRw)

With Application.FileDialog(msoFileDialogFolderPicker)
.Show
fPath = .SelectedItems(1)
End With


If Right(fPath, 1) <> "\" Then
fPath = fPath & "\"
End If

fwb = Dir(fPath & "*.*")
x = 2
Do While fwb <> ""
For Each c In rng
If InStr(LCase(fwb), LCase(c.Value)) > 0 Then
Worksheets("Sheet2").Range("C" & x) = fwb
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(fwb)
Worksheets("Sheet2").Range("D" & x) = f.DateLastModified
Worksheets("Sheet2").Range("B" & x) = f.Path
Worksheets("sheet2").Range("A" & x) = c.Value
Columns("A:D").AutoFit
Set fs = Nothing
Set f = Nothing
x = x + 1
End If
Next
fwb = Dir
Loop
Set sh = Nothing
Set rng = Nothing

Sheets(2).Activate

End Sub
Private子命令按钮1\u单击()
将sh作为工作表,rng作为范围,lr作为长度,fPath作为字符串
将sh=图纸(1)更改为实际
lstRw=sh.Cells.Find(What:=“*”,After:=sh.Range(“A1”),LookAt:=xlPart,LookIn:=xlFormulas,SearchOrder:=xlByRows,SearchDirection:=xlPrevious,MatchCase:=False)。行
设置rng=sh.Range(“A2:A”和lstRw)
使用Application.FileDialog(msoFileDialogFolderPicker)
显示
fPath=.SelectedItems(1)
以
如果正确(fPath,1)“\”则
fPath=fPath&“\”
如果结束
fwb=Dir(fPath&“***”)
x=2
当fwb“”时执行此操作
对于rng中的每个c
如果仪表(LCase(fwb)、LCase(c.值))大于0,则
工作表(“表2”)。范围(“C”和x)=fwb
设置fs=CreateObject(“Scripting.FileSystemObject”)
Set f=fs.GetFile(fwb)
工作表(“表2”)。范围(“D”和x)=f.DateLastModified
工作表(“表2”).范围(“B”和x)=f.路径
工作表(“表2”).范围(“A”&x)=c.值
列(“A:D”)。自动拟合
设置fs=Nothing
设置f=无
x=x+1
如果结束
下一个
fwb=Dir
环
设置sh=无
设置rng=无
第(2)页。激活
端接头
到目前为止,我尝试进行的润色通常都会导致脚本损坏,因此我来到这里寻求帮助

谢谢,

西蒙

根据我有限的经验,你似乎想完成4项任务

(一)

2) (最好将文件名保存在变量中)

3) 测试文本文件中的值。建议清除“涂鸦表”,导入文件,进行检查。e、 g

    Sheets("YourScratchPatch").Select
Application.CutCopyMode = False
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & yourpath & yourfile.txt, Destination:=Range("A1"))
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 850
    .TextFileStartRow = 2
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With
4) 如果找到值,将文件名变量写入索引表

我相信应该有更好的(数组?)方法来进行比较检查,但这取决于文本文件中的内容(即只有一个文件名?)


有关文本文件结构的更多信息将非常有用。希望这有帮助。

将视频文件列表保存为纯文本文件,而不是使用电子表格

grep--file=C:\file\u包含视频文件\u name.txt-R--include=*.txt C:\Path\To\Files>grep\u output.txt

写入grep_output.txt文件的信息如下所示

C:\wiki_files\wiki\pages/my_bank_rec_page.txt:{{视频:bank_rec_reboo1006031511.flv}}

因此,在一行中有一个包含视频名称和视频名称的文件路径

已将grep_output.txt文件导入新的Excel工作簿

使用常规公式执行以下操作

  • 在“/”处拆分A列,给出A列中的路径以及B列中的页面和视频信息
  • 在“{{”字符处拆分B列中的数据,在B列中留下页面名称,在C列中留下视频信息
  • 从C列中字符串的前面和后面剥离:{和|}}

文本文件是什么样子的?一行表示一个项目或繁忙/未知/等等?此外,目标是读取列“A”,搜索所有文本文件中的值,并将文本文件中的完整“路径信息”复制粘贴到列“B”,对吗(+C、D、E,如果有更多)?因为上面的代码还没有完成。它们是DokuWiki页面,所以有结构。我已经编辑了原始问题并在其中添加了更多细节。很抱歉……我不认为文本ile内容是相关的。文本文件是来自DokuWiki网站的数据库。因此有些可能有点长。我有edited原来的问题,并把额外的细节放在那里。感谢Maus的指点。花了一段时间我才有时间去看它。但仍然无法使它工作。最终解决了大部分问题,使用grep.grep--file=C:\file\u包含视频文件\u文件名-R--include=*.txt C:\Path\to\Files>grep\u output.txt一些小的manip用excel中的公式对输出进行了计算,使其更便于读者阅读。我还没有足够的声誉来评价你的答复是否有用。