1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Unable to obtain name of file in directory

Discussion in 'Software Development' started by JuniperGreen, Feb 9, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. JuniperGreen

    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 
    
     
  2. Rollin_Again

    Rollin_Again

    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" )

    Example that will show the full file path including document name:

    Example that will only show the document name

    Rollin
     
  3. JuniperGreen

    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!
     
  4. JuniperGreen

    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
     
  5. JuniperGreen

    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
     
  6. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1088857

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice