Problems with Enum and Files
I am trying to use Enum to work with files from a directory chosen from a directory chooser but it doesn't seem to work.
Could someone help me out with this code?
Code:
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
//To String Button
//---------
//Goal:
//Create Dir: Dialog Selected\TextBox1 Text
//Then,
//Move: Files_In_Enumerator To Dialog Selected\TextBox1 Text
//---------
//Path: Selected Path
//Path2: Selected Path\TextBox1 Text
//Di: Create Directory: "Path2"
//Movepath: Dialog Selected\TextBox1 Text\
//---------
System::String* path1 = folderBrowserDialog1->SelectedPath;
System::String* path2 = String::Concat(path1, S"\\", textBox1->Text);
DirectoryInfo* di = Directory::CreateDirectory(path2);
DirectoryInfo* dui = new DirectoryInfo (path2);
FileInfo* fi[] = dui->GetFiles();
Collections::IEnumerator* myEnum = fi->GetEnumerator();
int i=0;
while (myEnum->MoveNext())
{
FileInfo* fiTemp = __try_cast<FileInfo*>(myEnum->Current);
File::Move(myEnum->Current->ToString(), path2);
i++;
}
}
Thanks for any help!
Rob Sitter
Finally Got It, here it is :)
Hey people, thanks for your help, I've found a weird way around it but it works for now, here's the snippet:
Code:
System::String* path1 = folderBrowserDialog1->SelectedPath->ToString();
System::String* path2 = String::Concat(path1, S"\\", textBox1->Text->ToString());
System::String* pathX = String::Concat(path2, S"\\", textBox1->Text->ToString());
DirectoryInfo* di = Directory::CreateDirectory(path2);
DirectoryInfo* dui = new DirectoryInfo(path1);
FileInfo* fi[] = dui->GetFiles();
Environment::set_CurrentDirectory(path1); Collections::IEnumerator* myEnum = fi->GetEnumerator();
for(int i=0; i < fi->Count; i++)
{
listBox1->Items->Add(fi[i]->ToString());
i++;
}
while (myEnum->MoveNext())
{
FileInfo* fiTemp = __try_cast<FileInfo*>(myEnum->Current);
System::String* XName = myEnum->Current->ToString();
System::String* XProd = String::Concat(pathX, XName);
File::Move(myEnum->Current->ToString(), XProd);
//File::Move(myEnum->Current->ToString(), pathX);
Thanks again for all your help, I don't think I quite explained myself well enough,
Goodnight finally!!
Rob Sitter
Last post to finish this thread
I was able to cut down on the code, here it is for anyone who was interested in this thread:
Code:
System::String* path1 = folderBrowserDialog1->SelectedPath->ToString();
System::String* path2 = String::Concat(path1, S"\\", textBox1->Text->ToString(), S"\\");
DirectoryInfo* di = Directory::CreateDirectory(path2);
DirectoryInfo* dui = new DirectoryInfo(path1);
FileInfo* fi[] = dui->GetFiles();
Environment::set_CurrentDirectory(path1);
Collections::IEnumerator* myEnum = fi->GetEnumerator();
for(int i=0; i < fi->Count; i++)
{
listBox1->Items->Add(fi[i]->ToString());
i++;
}
while (myEnum->MoveNext())
{
FileInfo* fiTemp = __try_cast<FileInfo*>(myEnum->Current);
System::String* XFullInfo = String::Concat(path2, myEnum->Current);
File::Copy(myEnum->Current->ToString(), XFullInfo);
}
And it does exactly what I want.
You people in sysop land can close this if you wish :)
---Rob Sitter