dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Problem iterating a Dir

  1. #1
    John Guest

    Problem iterating a Dir

    I'm attempting to iterate through a directory using the method

    FName = Dir("DirectoryName")
    Do While FName<>""
    [...]
    FName = Dir
    Loop

    I'm using it to copy files held in a queue to remote drives.
    Before copying, I want to check that the share is connected and pass the
    share name to a separate Function.

    The problem I'm encountering is that when the share check fails (and passes
    back a False)
    my loop apparently "looses" its place and continues to call the first file
    in the directory at the line
    FName = Dir
    putting me in a infinite loop.

    By using debug.prints and rem'ing out lines I narrow it down to the Function
    call.

    Can anyone tell me what I'm doing wrong? (more specific code below)

    TIA,
    John
    '====================
    'Extract from code:

    Do While sFName <> ""

    If IsEFTSFile(sQueuePath, sFName) Then
    cFile.OpenDataFile (sQueuePath & sFName) ' open file to read
    sCRI = cFile.FieldValue(2, 73, 0, 1) ' extract CRI

    ' retrieve path based on key (CRI, above) from collection
    sSharePath = gcolDriveMap(sCRI)

    'ERROR OCCURS AT THE NEXT LINE
    If CheckForDir(sSharePath) Then ' remote drive is mapped/connected
    ' next two lines rem'd out during testing
    ' If Right(sSharePath, 1) <> "\" Then sSharePath = sSharePath &
    "\"
    ' cMove.MoveFile sQueuePath & sFName, sSharePath & sFName
    End If ' CheckForDir
    End If ' IsEFTSFile

    sFName = Dir ' find next file
    DoEvents
    Loop

    ====================
    ' in same BAS module
    Private Function CheckForDir(sDrive As String) As Boolean
    ' check to see if a remotely mapped drive is available (might be powered
    off)
    ' Parameter is the drive (folder) to test
    ' Returns "True" on availability

    On Error GoTo errClose

    Dir (sDrive) ' check for share (generates err 52 if not available)
    CheckForDir = True

    Exit Function

    errClose:
    CheckForDir = False

    End Function





  2. #2
    challis Guest

    Re: Problem iterating a Dir


    Hi,

    I suggest you use Scripting.FileSystemObjects.

    For each File in Folder
    File.Name
    next File

    fso is referenced by setting Project/References to Microsoft Scripting Runtime.

    challis



    "John" <jnielsRe-moveThis@att.net> wrote:
    >I'm attempting to iterate through a directory using the method
    >
    >FName = Dir("DirectoryName")
    >Do While FName<>""
    > [...]
    >FName = Dir
    >Loop
    >
    >I'm using it to copy files held in a queue to remote drives.
    >Before copying, I want to check that the share is connected and pass the
    >share name to a separate Function.
    >
    >The problem I'm encountering is that when the share check fails (and passes
    >back a False)
    >my loop apparently "looses" its place and continues to call the first file
    >in the directory at the line
    > FName = Dir
    >putting me in a infinite loop.
    >
    >By using debug.prints and rem'ing out lines I narrow it down to the Function
    >call.
    >
    >Can anyone tell me what I'm doing wrong? (more specific code below)
    >
    >TIA,
    >John
    >'====================
    >'Extract from code:
    >
    > Do While sFName <> ""
    >
    > If IsEFTSFile(sQueuePath, sFName) Then
    > cFile.OpenDataFile (sQueuePath & sFName) ' open file to read
    > sCRI = cFile.FieldValue(2, 73, 0, 1) ' extract CRI
    >
    > ' retrieve path based on key (CRI, above) from collection
    > sSharePath = gcolDriveMap(sCRI)
    >
    >'ERROR OCCURS AT THE NEXT LINE
    > If CheckForDir(sSharePath) Then ' remote drive is mapped/connected
    >' next two lines rem'd out during testing
    >' If Right(sSharePath, 1) <> "\" Then sSharePath = sSharePath

    &
    >"\"
    >' cMove.MoveFile sQueuePath & sFName, sSharePath & sFName
    > End If ' CheckForDir
    > End If ' IsEFTSFile
    >
    > sFName = Dir ' find next file
    > DoEvents
    > Loop
    >
    >====================
    >' in same BAS module
    >Private Function CheckForDir(sDrive As String) As Boolean
    >' check to see if a remotely mapped drive is available (might be powered
    >off)
    >' Parameter is the drive (folder) to test
    >' Returns "True" on availability
    >
    > On Error GoTo errClose
    >
    > Dir (sDrive) ' check for share (generates err 52 if not available)
    > CheckForDir = True
    >
    > Exit Function
    >
    >errClose:
    > CheckForDir = False
    >
    >End Function
    >
    >
    >
    >



  3. #3
    DimWit Guest

    Re: Problem iterating a Dir

    Thanks, I'll give it a try
    John

    challis <bmlx@yahoo.com> wrote in message news:3a18dc4c$1@news.devx.com...
    >
    > Hi,
    >
    > I suggest you use Scripting.FileSystemObjects.
    >
    > For each File in Folder
    > File.Name
    > next File
    >
    > fso is referenced by setting Project/References to Microsoft Scripting

    Runtime.
    >
    > challis
    >
    >
    >
    > "John" <jnielsRe-moveThis@att.net> wrote:
    > >I'm attempting to iterate through a directory using the method
    > >
    > >FName = Dir("DirectoryName")
    > >Do While FName<>""
    > > [...]
    > >FName = Dir
    > >Loop
    > >
    > >I'm using it to copy files held in a queue to remote drives.
    > >Before copying, I want to check that the share is connected and pass the
    > >share name to a separate Function.
    > >
    > >The problem I'm encountering is that when the share check fails (and

    passes
    > >back a False)
    > >my loop apparently "looses" its place and continues to call the first

    file
    > >in the directory at the line
    > > FName = Dir
    > >putting me in a infinite loop.
    > >
    > >By using debug.prints and rem'ing out lines I narrow it down to the

    Function
    > >call.
    > >
    > >Can anyone tell me what I'm doing wrong? (more specific code below)
    > >
    > >TIA,
    > >John
    > >'====================
    > >'Extract from code:
    > >
    > > Do While sFName <> ""
    > >
    > > If IsEFTSFile(sQueuePath, sFName) Then
    > > cFile.OpenDataFile (sQueuePath & sFName) ' open file to read
    > > sCRI = cFile.FieldValue(2, 73, 0, 1) ' extract CRI
    > >
    > > ' retrieve path based on key (CRI, above) from collection
    > > sSharePath = gcolDriveMap(sCRI)
    > >
    > >'ERROR OCCURS AT THE NEXT LINE
    > > If CheckForDir(sSharePath) Then ' remote drive is

    mapped/connected
    > >' next two lines rem'd out during testing
    > >' If Right(sSharePath, 1) <> "\" Then sSharePath = sSharePath

    > &
    > >"\"
    > >' cMove.MoveFile sQueuePath & sFName, sSharePath & sFName
    > > End If ' CheckForDir
    > > End If ' IsEFTSFile
    > >
    > > sFName = Dir ' find next file
    > > DoEvents
    > > Loop
    > >
    > >====================
    > >' in same BAS module
    > >Private Function CheckForDir(sDrive As String) As Boolean
    > >' check to see if a remotely mapped drive is available (might be powered
    > >off)
    > >' Parameter is the drive (folder) to test
    > >' Returns "True" on availability
    > >
    > > On Error GoTo errClose
    > >
    > > Dir (sDrive) ' check for share (generates err 52 if not available)
    > > CheckForDir = True
    > >
    > > Exit Function
    > >
    > >errClose:
    > > CheckForDir = False
    > >
    > >End Function
    > >
    > >
    > >
    > >

    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center