Solved: Unable to obtain name of file in directory

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

JuniperGreen

Thread Starter
Joined
Jan 3, 2013
Messages
25
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
 
Joined
Sep 4, 2003
Messages
4,912
Just reference the object name used in your FOR loop. It would also be wise to add a prefix to the object name so that you don't run the risk of using a reserved keyword (e.g - instead of using word "file" use the word "oFile" or "vFile" )

for each file in folder.Files
Example that will show the full file path including document name:

MsgBox (File)
Example that will only show the document name
MsgBox (File.Name)

Rollin
 

JuniperGreen

Thread Starter
Joined
Jan 3, 2013
Messages
25
Hi Rollin_Again, thank you for your kind words of advice especially as it was accompanied by an explanation. I am now sorted! I also intend to heed what you say about reserved keywords. I must confess to being rather guilty of running the risk of conflicts doing that. Var is another one that I use so I need to desist before I try working with JScript.

Thank you once again!
 

JuniperGreen

Thread Starter
Joined
Jan 3, 2013
Messages
25
Hi Rollin_Again, applying what you said in your response to other scripts I've found that I've been unable to identify the actual file name (using objFile.Name or File.name) when using the code below. What I don't understand is that although each file is scrolled through using objFile. Name I can't identify it using objFile.Name. Why should this be?

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject") 
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
Set colFiles = objWMIService.ExecQuery _ 
    ("ASSOCIATORS OF {Win32_Directory.Name='F:\Lit_WorkPad\Library'} Where " _ 
        & "ResultClass = CIM_DataFile") 
For Each objFile In colFiles 
    Set objFile = objFSO.OpenTextFile(objFile.Name, ForReading) 
    strContents = objFile.ReadAll 
wscript.echo objFile.name & strContents
 
  objFile.Close
Advice, as always, gratefully received

Thank you
 

JuniperGreen

Thread Starter
Joined
Jan 3, 2013
Messages
25
Hi, got it at last. It only seems to work if the code is located just after "for each etc". Doesn't work where I put it.

Sorry for bothering you
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Top