Hello,
I am just starting to do few web application and I have 2 questions to ask..Does anyone know how to count the number of files inside a folder? I am currently creating an application where I can upload images to my website. Whatever the image name is, it has to be renamed to a number before it is saved in the destination folder. That number would be to keep track of how many files will be there in the folder..For example if the new file being uploaded is 64th file, then irrespective of file name, the file should be renamed as 64.jpeg and then be saved in the folder.
If this is impossible, can anyone help me making this work with a SQL database perhaps..My idea is If the database can have the folder name and the corresponding number of files in it, when ever this file upload feature is used, the database would be accessed and the number of files in a particular folder can be known..After renaming the new file, the number of files in that folder would be incremented by 1 and that info will be updated in the database..I know how to insert data into SQL server..But I have no idea how to select data from database and use it for future calculations..I mean I don't know how to assign that returned(selected) value to either a textbox or a variable..Any valuable help would be appreciated...I am using C# for programming..I have done the image upload part..but this renaming part is really difficult for me..Please help
Thanks
Ani

to get the files use

string dir = @"C:\images\";
string[] files;
int numFiles;

files = Directory.GetFiles(dir);
numFiles = files.Length;

string nextFileName = (numFiles + 1).ToString() + ".jpg";

Hello dickersonka,
Is this code in C#?Do I need to include any header files? Visual Studio is not listing Directory.GetFiles(dir)

Thanks
Ani

hi

i tried this using vb.net.
in my page following files are imported already.

Imports System.IO
Imports System.Data.SqlClient
Imports System.Drawing

for my some other code.

and

Dim dir = "C:\images\"
            Dim files As Array
            Dim numFiles As Integer

            files = Directory.GetFiles(dir)

            numFiles = files.Length

            Dim nextFileName = (numFiles + 1).ToString() + ".jpg"

this code works fine. :)

Hi all,
Thanks for your replies..But it still doesn't work for me..I have no idea why..Well I am pasting my code..Please help me in finding out wats wrong with it..

count.aspx

 <p class="subsection1">File Upload</p>
    Select a file to upload <input type="file" id="File1" name="File1" runat="server">
    <br />
    <br />
    <asp:Button ID="CmdUpload" runat="server" Text="count" OnClick="CountFile"/>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    <br />
    <asp:Label ID="Label1" runat="server" Width="504px"></asp:Label>

count.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
public partial class count: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CountFile(object sender, EventArgs e)
    {
        string dir = @"C:\pictures\";
        string[] files;
        int numFiles;
        files = Directory.GetFiles(dir);
        numFiles = files.Length;
        //string nextFileName = (numFiles + 1).ToString() + ".jpg";
        //TextBox1.Text = nextFileName;
        Label1.Text = numFiles;
    }
}

All files inside the folder are .jpg files only...So I just have to find the length of array(files) and print that in some label or textbox right??Let me know if thats right..
Thanks
Ani

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Where are you calling countfile from?

Also ensure you have proper access on the c:\pictures\ directory, remember IIS is most likely using the asp.net service account to read the directory. You need to set the app pool user with permissions on this folder.

Hey thanks..It worked...I forgot to convert the integer value into string before printing it in a textbox...thanks...

Ani

can anyone help me...in finding out the counting the number of files in a folder using asp.net..... i want the code in asp.net ... i want to count the number of number of pdf files in a folder which has been placed in server......

This question has already been answered. Start a new discussion instead.