For Each loop question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fearlessfreep32
    New Member
    • Feb 2008
    • 2

    For Each loop question

    I'm having trouble understanding exactly where something comes from. Consider the bit of script below. Who decided to use oFile in the loop and why choose oFile?
    [code=vb]
    Set oFSO = WScript.CreateO bject("Scriptin g.FileSystemObj ect")

    sPath = InputBox("Provi de starting folder path")
    Set oFolder = oFSO.GetFolder( sPath)

    If oFSO.FolderExis ts(sPath) Then

    For Each oFile in oFolder.Files
    Msgbox "File " & oFile.Name & " last changed on on " & _
    oFile.DateLastM odified & " and of type " & _
    oFile.Type & ". It is contained in folder " & _
    oFile.ParentFol der.Path & " and uses the short " & _
    " filename " & oFile.Shortname & "."
    Next
    End If
    [/code]
    Last edited by debasisdas; Feb 11 '08, 05:17 AM. Reason: added code=vb tags
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    I dont think the code is yours

    oFile is declared some where else which is not part of this code and that must be a file object.

    Comment

    • jimmylee
      New Member
      • Feb 2008
      • 17

      #3
      Originally posted by fearlessfreep32
      I'm having trouble understanding exactly where something comes from. Consider the bit of script below. Who decided to use oFile in the loop and why choose oFile?
      [code=vb]
      Set oFSO = WScript.CreateO bject("Scriptin g.FileSystemObj ect")

      sPath = InputBox("Provi de starting folder path")
      Set oFolder = oFSO.GetFolder( sPath)

      If oFSO.FolderExis ts(sPath) Then

      For Each oFile in oFolder.Files
      Msgbox "File " & oFile.Name & " last changed on on " & _
      oFile.DateLastM odified & " and of type " & _
      oFile.Type & ". It is contained in folder " & _
      oFile.ParentFol der.Path & " and uses the short " & _
      " filename " & oFile.Shortname & "."
      Next
      End If
      [/code]

      Here


      Dim oFSO As FileSystemObjec t
      Dim sPath As String
      Private Sub Command1_Click( )
      Set oFSO = New FileSystemObjec t

      sPath = InputBox("Provi de starting folder path")
      Set oFolder = oFSO.GetFolder( sPath)

      If oFSO.FolderExis ts(sPath) Then

      For Each oFile In oFolder.Files
      MsgBox "File " & oFile.Name & " last changed on on " & _
      oFile.DateLastM odified & " and of type " & _
      oFile.Type & ". It is contained in folder " & _
      oFile.ParentFol der.Path & " and uses the short " & _
      " filename " & oFile.ShortName & "."
      Next
      End If

      End Sub

      Comment

      • fearlessfreep32
        New Member
        • Feb 2008
        • 2

        #4
        The code is NOT mine. I was reading it and trying to figure out the thing I was asking about. The code also works as is so it is not declared anywhere else.

        Thanks anyway

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by fearlessfreep32
          ... The code also works as is so it is not declared anywhere else.
          Then VB must be set to allow you to use variables without explicitly declaring them - a very bad practice.

          Comment

          Working...