computing
  • 37

Solved Unable To Obtain The Name Of A File In A Directory

  • 37

Having become unstuck with a script I am working on I would appreciate some help. Basically, it reads in the string content of each file in a directory into an array (period delimited). It then searches for a keyword and if found concatenates each sentence in which it is found. The echoed output shows what was found in each file.

This works perfectly well and does what I want but in the 3rd last line of the piece of code shown below where I have “File OutPut:” I would like to have the actual name of the file instead. I have tried every which way to achieve this but without success. Although I think it is to do with “Object Required” I’m not sure how to resolve it.

I would be grateful if someone could point me in the right direction.

Thank you

[code]
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set folder = objFSO.GetFolder(strFilePath)

for each file in folder.Files
Set objfile = objFSO.OpenTextFile(file.path, ForReading)
strContents = objfile.readAll

‘Load String Into Array
outputArray = split(strContents,”.”)

‘Check For KeyWord Match
for each strCurrentLine in outputArray
If InStr(strCurrentLine, strKeyWord) Then

‘Concatenate Matches
strOutput = strOutput & “File OutPut: ” & vbCRLF & strCurrentLine & “.” & vbCRLF & vbCRLF

End if

next
[/code]

Share

1 Answer

  1. I think you just need file.name:
    strOutput = strOutput & “File OutPut: ” & file.name & vbCRLF & strCurrentLine & “.” & vbCRLF & vbCRLF

    I assume there’s another “next” to close the “for each file” loop, since your program’s working.
    I think your var. names kind of confused things, what with two different objects having related names (file, and objfile). “file” is an object, subordinate to folder, and so is “objfile”, subordinate to fso.

    • 0