Finding file names


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Finding file names

  1. #1
    Tim Martin Guest

    Finding file names


    What is a good way for me to access all files in a single directory (*.*).
    I want to write a program that processes all the files that have been deposited
    in a specific directory, but don't know what the file names are going to
    be.


  2. #2
    Patrick Marshall Guest

    Re: Finding file names


    Hi Tim,

    Use the VB "dir" command, CAUTION, the dir
    mask is GLOBAL to the project and the last
    one called is used wherever you call it. Don't
    call dir from nested subroutines.

    e.g.

    dim FileMaskStr as string
    dim FileNameStr as string
    dim FileNameStrArr() as string
    dim SizeOfFileNameStrArrInt as integer

    FileMaskStr = "c:\yourPath\*.*" 'yourpath may be a UNC path, e.g. \\abc\def\*.*

    FileNameStr = dir(fileMaskStr) 'first call with mask sets subsequent calls
    do while FileNameStr <> ""
    FileNameStr = dir 'note, mask is omitted
    'note that FileNameStr is a filename only, the path is implied from
    'the masking string

    'IMPORTANT, if you call a subroutine that uses dir(mask), that mask will
    'replace the current mask, DO NOT NEST the dir command
    'if you need to do nested dir commands, store intermediate results for
    'each level in an array then move to the next directory

    'store names of files
    redim preserve FileNameStrArr(SizeOfFileNameStrArrInt)
    FileNameStrArr(SizeOfFileNameStrArrInt) = FileNameStr
    SizeOfFileNameStrArrInt = SizeOfFileNameStrArrInt + 1
    loop

    <Patrick>


    "Tim Martin" <tpmartin@_NOSPAM_bigfoot.com> wrote:
    >
    >What is a good way for me to access all files in a single directory (*.*).
    > I want to write a program that processes all the files that have been deposited
    >in a specific directory, but don't know what the file names are going to
    >be.
    >



  3. #3
    Patrick Marshall Guest

    Re: Finding file names


    Hi Tim,

    Punched "Post" too quickly, see correction below

    >do while FileNameStr <> ""
    > '****** NOT HERE FileNameStr = dir 'note, mask is omitted
    > 'note that FileNameStr is a filename only, the path is implied from
    > 'the masking string
    >
    > 'IMPORTANT, if you call a subroutine that uses dir(mask), that mask

    will
    > 'replace the current mask, DO NOT NEST the dir command
    > 'if you need to do nested dir commands, store intermediate results for
    > 'each level in an array then move to the next directory
    >
    > 'store names of files
    > redim preserve FileNameStrArr(SizeOfFileNameStrArrInt)
    > FileNameStrArr(SizeOfFileNameStrArrInt) = FileNameStr
    > SizeOfFileNameStrArrInt = SizeOfFileNameStrArrInt + 1

    '******* BELONGS HERE
    FileNameStr = dir 'note, mask is omitted
    >loop


    <Patrick>

  4. #4
    Shelly Rosenfeld Guest

    Re: Finding file names


    Patrick - I haven't had a need for the 'dir' function
    in years, but 2 red flags here:

    1 - Be sure to indicate the file types you want in the
    dir (i.e. directory, file, hidden etc.)

    2 - Sorry, I don't remember clearly, but I had to check
    the returned info to see if it in fact was of the type
    defined in step 1 above.

    Sheldon


    <Patrick Marshall> wrote in message news:39595220$1@news.devx.com...
    >
    > Hi Tim,
    >
    > Punched "Post" too quickly, see correction below
    >
    > >do while FileNameStr <> ""
    > > '****** NOT HERE FileNameStr = dir 'note, mask is omitted
    > > 'note that FileNameStr is a filename only, the path is implied from
    > > 'the masking string
    > >
    > > 'IMPORTANT, if you call a subroutine that uses dir(mask), that mask

    > will
    > > 'replace the current mask, DO NOT NEST the dir command
    > > 'if you need to do nested dir commands, store intermediate results

    for
    > > 'each level in an array then move to the next directory
    > >
    > > 'store names of files
    > > redim preserve FileNameStrArr(SizeOfFileNameStrArrInt)
    > > FileNameStrArr(SizeOfFileNameStrArrInt) = FileNameStr
    > > SizeOfFileNameStrArrInt = SizeOfFileNameStrArrInt + 1

    > '******* BELONGS HERE
    > FileNameStr = dir 'note, mask is omitted
    > >loop

    >
    > <Patrick>




  5. #5
    Daryl Guest

    Re: Finding file names


    A technique that I have used in the past is to use a hidden FileListBox. Just
    set the Path property to the path you want to check and set the Pattern property
    to *.* and then you can access it just like a list box using the List property
    to access each file name.


    "Tim Martin" <tpmartin@_NOSPAM_bigfoot.com> wrote:
    >
    >What is a good way for me to access all files in a single directory (*.*).
    > I want to write a program that processes all the files that have been deposited
    >in a specific directory, but don't know what the file names are going to
    >be.
    >



  6. #6
    Barry Guest

    Re: Finding file names


    "Tim Martin" <tpmartin@_NOSPAM_bigfoot.com> wrote:
    >
    >What is a good way for me to access all files in a single directory (*.*).
    > I want to write a program that processes all the files that have been deposited
    >in a specific directory, but don't know what the file names are going to
    >be.
    >


    You can also do it through the Windows API. MSDN lists a good example of
    how to implement file searching (including spanning multiple sub directories).
    Search MSDN for ID: Q185476. Hope this helps.
    -Barry

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