.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> .NET Framework >> Post New QuestionBookmark and Share Subscribe to Forum

segregation of files

Posted By: Aruna     Posted Date: December 09, 2010    Points:2   Category :.NET Framework
Hi,

In an excel sheet, i have all the file names extracted from the source folder in one column. In another column, i have destination folder names to which the files to be moved.

This is the sample data in my excel sheet. how to do this in C#.net code.

Already have this functionality in java, need to convert the same in to .net now.

Source foldername Filenames Dest folder name
DECSix Aaa_466.docx TSL
DECSix bbb_737.docx INS


class SegregateCVs {
public static void main(String[] args) {
System.out.println("Will be done!"); // Display the string.
String baseFolder = "C:" + File.separator + "Documents and Settings"+ File.separator + "288122" + File.separator + "Desktop" + File.separator + "CV Cleanup"
+ File.separator + "current";
String destFolder = "C:" + File.separator + "Documents and Settings"+ File.separator + "288122" + File.separator + "Desktop" + File.separator + "CV Cleanup" + File.separator + "Destination";
System.out.println(" baseFolder : " + baseFolder);
System.out.println(" destFolder : " + destFolder );
try
{
String inputFilename = "FileNames.xls";
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));
Workbook workbook = Workbook.getWorkbook(
new File(inputFilename),ws);
Sheet readableSheet = workbook.getSheet(0);
moveFiles(readableSheet, baseFolder, destFolder);
workbook.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (BiffException e)
{
e.printStackTrace();
}

/* File srcFile = new File(srcFilename);
File destFile = new File(destFilename);
try{
copyFile(srcFile, destFile);
srcFile.delete();
} catch (IOException ex){

System.out.println("Failed to copy");
}
System.out.println("Source File Name " + srcFilename);
System.out.println("Dest File Name " + destFilename); */
}
private static void moveFiles(Sheet s, String baseFolder, String destFolder)
{
String fileName = null;
String directoryName = null;
String buName = null;
String psNo = null;
int rowNo = 1;
int colNo = 1;
int lastIndex = 0;
Number n = null;
int nPSNo = 0;
String srcFileName = null;
String destFileName = null;
File srcFile = null;
File destFile = null;
try{
Label psNoLabel = null;
LabelCell labelCell1 = (LabelCell) s .getCell(colNo , rowNo);
fileName = labelCell1.getString();
while (!fileName.equals("")){
labelCell1 = (LabelCell) s .getCell(colNo-1 , rowNo);
directoryName = labelCell1.getString();
labelCell1 = (LabelCell) s .getCell(colNo+2 , rowNo);
buName = labelCell1.getString();
//System.out.println(" Row No: " + rowNo + "directoryName : " + directoryName + " fileName " + fileName + " buName : " + buName );
srcFileName = baseFolder + File.separator + directoryName + File.separator + fileName;
destFileName = destFolder + File.separator + buName + File.separator + fileName;
System.out.println(" Row No : " + rowNo);
System.out.println(" srcFileName " + srcFileName );
System.out.println(" destFilename " + destFileName );
srcFile = new File(srcFileName);
destFile = new File(destFileName);
try{
copyFile(srcFile, destFile);
srcFile.delete();
} catch (IOException ex){
System.out.println("Failed to copy");
}
++rowNo;
labelCell1 = (LabelCell) s.getCell(colNo , rowNo);
fileName = labelCell1.getString();
}
}catch(Exception e){
e.printStackTrace();
System.out.println("end of file?");

}

}
private static void copyFile(File sourceFile, File destFile)
throws IOException {
if (!sourceFile.exists()) {
return;
}
if (!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
if (destination != null && source != null) {
destination.transferFrom(source, 0, source.size());
}
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}

}







Responses
Author: Narayanan             
Posted Date: September 12, 2011     Points: 20   

Hi,
try this link
http://tangiblesoftwaresolutions.com/

Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend