Click here for important information about

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

February 9, 2013 at 12:08:32
Specs: Windows Vista, 1.6 gb 4gb
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

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


See More: Unable To Obtain The Name Of A File In A Directory

February 9, 2013 at 17:11:45
✔ Best Answer
I think you just need
strOutput = strOutput & "File OutPut: " & & 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.

Report •

February 10, 2013 at 10:56:57
Thank you, nbrane, you were so right and yes there was another next. The sad thing is that I had tried and it threw up an error or so I thought!

Unfortunately, when I had been trying to find the answer I had forgotten to remove a little piece of code I added in earlier in the script and it was this that produced the error message. I take your point re the confusion and as a newbie ought to start right as I mean to go on. Otherwise, I may run the risk of developing so called spaghetti style programming - remember the overuse of the GOTO back in the days of good old Basic!

I know this is maybe going a bit off script but when I was poring over my script to clean it up I discovered an anomaly I hadn't anticipated. The formatting was just as I wanted until one search I did messed it up. I was stumped for a good while until I sussed it was because the array, being period delimited, was reading a carriage return and line feed into the element along with the text whenever the previous line ended with a full stop - just as it should only I hadn't appreciated that! I solved the problem by using Replace(strContents, vbCrLf, "") to strip out all the carriage returns and line feeds.

I mention this just for the benefit of any other newbie out there who might have problems because they are unaware that an array element can include a CRLF

Thank you once again for your help and support!

Report •

February 10, 2013 at 16:55:56
Yes, vbscript enforces "goto-less" programming, but quirkily enough visbasic does not, and I've been guilty of using them. I've found certain situations actually look and seem "cleaner" with conservative application of go-tos. But you're right to discipline yourself against them - it's very easy to become sloppy and lazy using them.

Report •
Related Solutions

Ask Question