I am having a problem with reading the files and subfolders. My code reads fine for the given fixed source path,
E:\\Folder\\test\\test2
.There are many folders in the test, like test2, test3, test4, etc. I want to extract the data files in the main folder,
test
.For example, I want to extract the files in test, so I want to read all the files contained in the test instead of writing my code for test3, test4 and many. And I want to extract and write all the files as same source structure on another drive.
like, if the source structure is like
E:\\Folder\\test\\test2
then the destination structure should be likeC:\\Folder\\test\\test2
Is there any way to do this?
Here is my code,
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
DateTime dt = dateTimePicker1.Value;
txtSelectedDate.Text = dt.ToString("yyyyMMdd");
selectedDate = txtSelectedDate.Text;
}
private void button2_Click(object sender, EventArgs e)
{
DateTime stdate = Datetimepicker1.value;
while (stdate <= DateTime.Now)
{
txtSelectedDate.Text = stdate.ToString("yyyyMMdd");
selectedDate = txtSelectedDate.Text;
string DayBgSpot = "E:\\Folder\\test\\test2";
开发者_开发技巧 string DayBgSpotDestination = "E:\\Folder1";
int DT = int.Parse(txtSelectedDate.Text);
FileReader Reader = new FileReader();
FileReader Reader1 = new FileReader();
Reader.OpenDirectory(DayBgSpot);
Reader.ReadNaster();
string path = DayBgSpotDestination + "\\" + txtSelectedDate.Text + ".txt";
StreamWriter Strwriter = new StreamWriter(path);
try
{
while (Reader.iMaRecordsLeft > 0)
{
string SecName = Reader.sMaSecName;
string Symbol = Reader.sMaSecSymbol;
Symbol = prefix + Symbol;
int abc = 0;
Reader.OpenSecurityByName(Reader.sMaSecName);
if (Reader.iSeRecords > 0)
{
while (Reader.iSeRecordsLeft > 0)
{
Reader.ReadDay();
float O = Reader.dSeo;
float H = Reader.dSeh;
float L = Reader.dSel;
float C = Reader.dSec;
double V = Reader.dSeV;
double OI = Reader.dSrest;
string T = Reader.iSeTime.ToString();
string D = Reader.iSeDate.ToString();
if (int.Parse(D) == DT)
{
string a = string.Concat(SecName, ",", Symbol, ",", D, ",", T, ",", O, ",", H, ",", L, ",", C, ",", V, ",", OI);
if (SecName != "" && V != 0)
{
Strwriter.WriteLine(a);
}
}
}
}
abc++;
Reader.ReadNaster();
}
Reader.CloseDirectory();
Strwriter.Close();
Strwriter.Dispose();
}
catch
{
}
stdate = stdate.AddDays (1); // It will get next date till present
}
}
Something like
System.IO.DirectoryInfo baseFolder = new DirectoryInfo(@"c:\Folder\test\");
string destinationPath = @"e:\Folder\test\";
System.IO.DirectoryInfo[] subDirs = baseFolder.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
string subFolder = dirInfo.Name;
System.IO.FileInfo[] fileInfos = dirInfo.GetFiles("*.txt");
foreach (System.IO.FileInfo fileInfo in fileInfos)
{
// Do something with the files
string writePath = destinationPath + subFolder + @"\" + fileInfo.Name;
// Write
}
}
If you are using .NET 4.0 this is one line:
var filepaths = Directory.GetFiles(path: @"C:\", searchPattern: "*pattern*", searchOption: SearchOption.AllDirectories);
Clearly, the root path and search pattern are not in line with the proposed sample, but my intention should clear.
I hope this helps
精彩评论