Listing Files from Directory and searching pattern in the files


This code will list all the files according to the search pattern and will search the string into the files and will list the files.

The following code simply lists the files from the directory according to the specified pattern,if you want to search only text files then just put *.txt,for word file put *.doc like this.
and afetr listing all such files it will search the content of the file according to the pattern.if you want to search a word into the file called 'DHARAM' simply add it to the Regex part and all files containing word DHARAM will be listed.

aspx code


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FilesAndDirectory.aspx.cs" Inherits="LoginAsDifferentUser.FilesAndDirectory" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblNo" runat="server"></asp:Label><br />
<asp:Label ID="lbl" runat="server"></asp:Label><br />
<asp:Label ID="lblN" runat="server"></asp:Label><br />
</div>
</form>
</body>
</html>


aspx.cs file

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace LoginAsDifferentUser
{
public partial class FilesAndDirectory : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int i = 0;
int j = 0;
DirectoryInfo dir = new DirectoryInfo("C:\\DK"); //directory to search files
FileInfo[] files = dir.GetFiles("*.txt"); // you can specify any pattern here like file begining with 'dk' will be as dk*
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
sb.Append("<div id='dv' style='overflow:scroll;height:150px'><b>Total Files</b><br />");
sb1.Append("<div id='dv1' style='overflow:scroll;height:150px'><b>Files containing Search Pattern</b><br />");
sb2.Append("<div id='dv2' style='overflow:scroll;height:150px'><b>Files that have not Search Pattern</b><br />");
Regex reg = new Regex(@"DHARAM");//Search Pattern for the file, All the files which have DHARAM words will be listed
string test = null;
foreach (FileInfo file in files)
{
sb.Append(file.Name+" "+ file.CreationTime.ToString()+"<br />");
StreamReader str = File.OpenText(file.FullName);
test = str.ReadToEnd();

if (reg.IsMatch(test))
{
sb1.Append(file.Name + "<br/>");
i++;
}
else
{
sb2.Append(file.Name + "<br/>");
}

}
sb.Append("</div>");
sb1.Append("</div>");
sb2.Append("</div>");
sb.Append(sb1.ToString());
sb.Append(sb2.ToString());
Response.Write(sb.ToString());
lblNo.Text = "Total Number of Files containing Search Pattern are " + i.ToString();
lbl.Text = "Total Number of Files that have not Search Pattern are " + (files.Length-i).ToString();
lblN.Text = "Total Number of files are " + files.Length.ToString();
}
}
}

Designer file

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.3603
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace LoginAsDifferentUser {


public partial class FilesAndDirectory {

/// <summary>
/// form1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlForm form1;

/// <summary>
/// lblNo control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblNo;

/// <summary>
/// lbl control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lbl;

/// <summary>
/// lblN control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblN;
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: