0

Okedoke... 我有一个 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

到目前为止,我的修改尝试通常导致脚本损坏,因此导致我在这里寻求帮助。

谢谢,
西蒙

4

2 回答 2

1

从http://gnuwin32.sourceforge.net/下载了 GNU 工具 grep 的 win32 端口

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

grep --file=C:\file_containing video_file_names.txt -R --include=*.txt C:\Path\To\Files >grep_output.txt

写入 grep_output.txt 文件的信息看起来像

C:\wiki_files\wiki\pages/my_bank_rec_page.txt:{{videos:bank_rec_reopen1006031511.flv|}}

因此,包含视频名称和视频名称的文件路径位于一行。

将 grep_output.txt 文件导入到新的 Excel 工作簿中。

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

  • 在“/”处拆分 A 列以给出 A 列中的路径以及 B 列中的页面和视频信息
  • 将 B 列中的数据拆分为“:{{”字符,将页面名称留在 B 列,将视频信息留在 C 列
  • 从 C 列中字符串的前后剥离 :{{ 和 |}}
于 2013-07-01T07:18:42.680 回答
0

根据我有限的经验,您似乎想要执行 4 个任务。

1)遍历目录

2)循环遍历每个目录的文件(将文件名保存在变量中的好主意)

3) 测试文本文件的值。建议清除“涂鸦表”,导入文件,运行检查。例如

    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) 如果找到值,将文件名变量写入索引表。

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

有关文本文件结构的更多信息会很有用。希望这可以帮助。

于 2013-06-18T09:55:21.770 回答