How to move files from one folder to different folders using split(string)concept

Posted by Sathiya_Narayanan under ASP.NET on 9/4/2012 | Points: 10 | Views : 2258 | Status : [Member] | Replies : 2
Hi,

I have an idea to develop one windows application, there are more number of files in a folder these files should split into two different folders
by using the windows applications.
example if file name is MALE means it should move to male folder, if female means it should move to female folder.
How to perform this, give me some ideas, any examples, sample articles.







Thanks,

SATHIYANARAYANAN


Responses

Posted by: Vasanthmvp on: 9/4/2012 [Member] Starter | Points: 25

Up
0
Down
Hi, here is one way you can do this.

List all the files of the directory that you want to move from. By setting the checkboxlist datasource as all files. Selecting the files you can move to your destination folder.

Eg:

.aspx Page:

<asp:CheckBoxList ID="listfiles" runat="server" />
<asp:Button ID="btngetfiles" runat="server" Text="GetFiles" OnClick="GetFiles" />
<asp:Button ID="btn1" runat="server" Text="Move to folder1" OnClick="MoverToFolder1" /> &nbsp; <asp:Button ID="btn2" runat="server" Text="Move to Folder2" OnClick="MoveToFolder2" />
<br />
<asp:Label ID="lblMessage" runat="server" />


.aspx.cs :


string dir = string.Empty;
string newdir = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
dir = Server.MapPath("~/");
}

protected void GetFiles(object sender, EventArgs e)
{
listfiles.DataSource = new DirectoryInfo(dir).GetFiles();
listfiles.DataBind();

}



protected void MoverToFolder1(object sender, EventArgs e)
{
newdir = Server.MapPath("~/NewFolder1/"); // This is my first folder into which i want to move the selected files
foreach (ListItem item in listfiles.Items)
{
if (item.Selected == true)
{
File.Move(dir + listfiles.SelectedItem, newdir + listfiles.SelectedItem);
lblMessage.Text = "File Moved successfully !";
}
}
}


protected void MoveToFolder2(object sender, EventArgs e)
{
newdir = Server.MapPath("~/NewFolder2/"); // You can create newfolder2 explicity or from code behind. This is my second folder

foreach(ListItem item in listfiles.Items)
{
File.Move(dir + listfiles.SelectedValue, newdir + listfiles.SelectedValue);
lblMessage.Text = "File Moved successfully !";
}
}


If this is not the appropriate answer, let me know.

Regards,

Awesome Coding !! :)

Sathiya_Narayanan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sathiya_Narayanan on: 9/4/2012 [Member] Starter | Points: 25

Up
0
Down
Ya i got it, but the function is the files should be moved automatically, not user make sure to move to desired folder.
when user click button it should moved all files automatically to respective folders.
In database there is a column conditioncheck(name) in that ' _male', '$female', function should check from the DB if file name is '' _xxx'' it should move to respective folders. like this to be done.
i think this can be done using split string method.

SATHIYANARAYANAN

Sathiya_Narayanan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response