PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Initializing an array with a Directories file count?-VBForums
Results 1 to 6 of 6
  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,772

    Initializing an array with a Directories file count?

    How do I fix the below statement?

    Code:
    string[] fileName = {Directory.GetFiles(Server.MapPath(strPath)).Length};    //This is not working
    
    for (int x = 0; x <= Directory.GetFiles(Server.MapPath(strPath)).Length; x++)
    .
    .
    .
    Blake

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,681

    Re: Initializing an array with a Directories file count?

    What are you expecting?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,772

    Re: Initializing an array with a Directories file count?

    As far as the array initialization...the number of files in the directory. Disregard the For statement.
    Blake

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,181

    Re: Initializing an array with a Directories file count?

    Do you want the paths of the files in the folder or the number of files in the folder? If you want the former then that's what GetFiles returns, so just assign that to your variable:
    csharp Code:
    1. var filePaths = Directory.GetFiles(Server.MapPath(strPath));
    How you name things is up to you but, personally, I pretty much always use a plural for an array or collection and I also use "path" for actual paths and "name" only if it's just the unqualified name, e.g. "C:\Folder\Subfolder\File.ext" would be a path while "File.ext" would be a name. Note also that I used 'var', so the variable type is inferred from the initialising expression. GetFiles returns a string array so that's the type of the variable.

    If you actually want the number of files in the folder then you should be using a different variable name:
    csharp Code:
    1. var fileCount = Directory.GetFiles(Server.MapPath(strPath)).Length;
    In this case, Length is type 'int' so the variable is type 'int'.

    The correct code depends on what you're actually trying to achieve but you haven't actually told us what that is. You've been here too long to be making that mistake. I'm guessing that you should be doing something like this:
    csharp Code:
    1. var filePaths = Directory.GetFiles(Server.MapPath(strPath));
    2.  
    3. foreach (var filePath in filePaths)
    4. {
    5.     // Use filePath here.
    6. }
    You should always use a `foreach` loop in preference to a `for` loop if the aim is to do something with each item in a list. Only use a `for` loop if you need to use the loop counter for something other than getting an item from a single list. If you do need a `for` loop:
    csharp Code:
    1. var filePaths = Directory.GetFiles(Server.MapPath(strPath));
    2.  
    3. for (var i = 0; i < filePaths.Length; i++)
    4. {
    5.     var filePath = filePaths[i];
    6.  
    7.     // Use filePath here.
    8. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,181

    Re: Initializing an array with a Directories file count?

    Quote Originally Posted by blakemckenna View Post
    As far as the array initialization...the number of files in the directory. Disregard the For statement.
    You posted while I was typing. It appears that all you need is the second code snippet I posted. If you prefer not to use type inference:
    sharp Code:
    1. int fileCount = Directory.GetFiles(Server.MapPath(strPath)).Length;
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,772

    Re: Initializing an array with a Directories file count?

    I actually was using the foreach construct and forget why I even tried using the for construct. Thanks for the examples jmc.
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width