Preface
In addition to the project, the company requested to copy the XLS data files uploaded by the customer according to the conditions. However, these uploaded files have all been renamed again, but there are still upload records, includes New naming rules for files. therefore, you only need to write a program to obtain the files whose names have been re-killed in batches and copy them out.
Question
I directly created An ASPX file and wrote it in the background code. Post the CS code: 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. IO;
Using system. Data. sqlclient;
Public partial class page_filesaseasy: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Arraylist arr = new arraylist ();
Sqlconnection conn = new sqlconnection ();
Conn. connectionstring = @ "Data Source =. \; initial catalog = test; user id = test; Password = test ";
Conn. open ();
String sqlcmd;
Sqlcmd = @ "select * from test ";
Sqlcommand cmd = new sqlcommand (sqlcmd, Conn );
Sqldatareader SDR = cmd. executereader ();
While (SDR. Read ())
{
Filevo Vo = new filevo ();
VO. client_name = SDR ["clientname"]. tostring ();
VO. client_id = SDR ["linkshop"]. tostring ();
VO. Category = SDR ["category"]. tostring ();
VO. filename = SDR ["FILENAME"]. tostring ();
Arr. Add (VO );
}
SDR. Close ();
Conn. Dispose ();
Response. Write ("Start copying files... <br/> ");
Response. Flush ();
Foreach (Object VaR in ARR)
{
Try
{
Filevo item = (filevo) var;
// This is the path of the renamed File
String from = @ "E: \ files \" + item. client_id.trim () + "_" + item. category. trim () + "_" + item. filename. trim () + ". xls ";
String Category = string. empty;
Switch (item. Category)
{
Case "1 ":
Category = "sell ";
Break;
Case "2 ":
Category = "rent ";
Break;
Default:
Category = "purchase ";
Break;
}
// Rename
String to = @ "F: \ xlsdata \" + item. client_name.trim () + "_" + category. Trim () + ". xls ";
// Copy an object
File. Copy (from, to, true );
// Set file attributes
File. setattributes (to, fileattributes. Normal );
}
Catch (exception ex)
{
Response. Write (ex. Message + "<br/> ");
Return;
}
}
Response. Write ("<br/> end of copying file ..");
}
Class filevo
{
Private string _ client_id;
/// <Summary>
/// Customer Code
/// </Summary>
Public String client_id
{
Get {return _ client_id ;}
Set {_ client_id = value ;}
}
Private string _ category;
/// <Summary>
/// Business type
/// </Summary>
Public String category
{
Get {return _ category ;}
Set {_ Category = value ;}
}
Private string _ filename;
/// <Summary>
/// File name
/// </Summary>
Public String filename
{
Get {return _ filename ;}
Set {_ filename = value ;}
}
Private string _ client_name;
/// <Summary>
/// CUSTOMER NAME
/// </Summary>
Public String client_name
{
Get {return _ client_name ;}
Set {_ client_name = value ;}
}
}
}
Note:
The key here is the following code: // copy an object
File. Copy (from, to, true );
// Set file attributes
File. setattributes (to, fileattributes. Normal );
In particular, it took me a lot of time to find the second sentence. If you do not add this sentence, the following error will be reported: the process cannot access the file 'f: \ xlsdata \ xxx_xxx.xls 'because it is being used by another process.
OK !! Batch Rename and copy successful!