Thread: Problems with Enum and Files

  1. #1
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > FileInfo* fi[] = dui->GetFiles();
    Does this even compile?

    > while (myEnum->MoveNext()
    Perhaps a different loop condition would help, say whether a file was actually found.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.
    You seem to be using an enumerator. An Enum is something totally different. So what's wrong with the code ? Does it compile ? Does it run ? Does it produce results ? If not, what did you expect ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    I may be wrong?

    Ok, here's the best I can explain:

    For now, being a beginner and going off other source code I am under the belief that the enum part of my code will put all the filenames into an array to which I can use the
    myEnum->movenext() as the loop. I'm not even sure if I'm right.

    What my question is really, how would I take a directory and put all the filenames into an array and move them that way?

    Example: With file renaming programs out there they will rename based on arrays of information, This is what I want to do but with moving, not renaming.

    "Does this even compile": yes, it does.

    For instance:
    Code:
    void main() {
        // Create a reference to the current directory.
        DirectoryInfo* di = new DirectoryInfo(Environment::CurrentDirectory);
        // Create an array representing the files in the current directory.
        FileInfo* fi[] = di->GetFiles();
        Console::WriteLine(S"The following files exist in the current directory:");
        // Print out the names of the files in the current directory.
        Collections::IEnumerator* myEnum = fi->GetEnumerator();
        while (myEnum->MoveNext()) {
            FileInfo* fiTemp = __try_cast<FileInfo*>(myEnum->Current);
            Console::WriteLine(fiTemp->Name);
        }
    }
    If this works than so should mine...

    Anyways, I think I'm confusing you people so I'll end this thread and figure it out myself
    I think I've messed up big somewhere anyways!

    Thanks for your replies,

    Rob Sitter


    I guess I'll just keep poking at it until I figure it out.
    Thanks,

    Rob Sitter

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    			DirectoryInfo* di = Directory::CreateDirectory(path2);
    			DirectoryInfo* dui = new DirectoryInfo (path2);
    			FileInfo* fi[] = dui->GetFiles();
    If you've just created a directory, it is going to have no files. Should the highlighted portion read path1?

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Looks like you may be having problems with File::Move

    Code:
    #using <mscorlib.dll>
    
    using namespace System;
    using namespace System::IO;
    
    int main(void) {
        String* path1 = S"c:\\temp\\";
        String* path2 = S"c:\\temp2\\";
        String* path3;
        String* path4;    
    // Create a reference to the temp directory.
        DirectoryInfo* di = new DirectoryInfo(S"c:\\Temp");
        // Create an array representing the files in the current directory.
        FileInfo* fi[] = di->GetFiles();
        Console::WriteLine(S"The following files exist in the current directory:");
        // Print out the names of the files in the current directory.
        Collections::IEnumerator* myEnum = fi->GetEnumerator();
        while (myEnum->MoveNext()) {
            FileInfo* fiTemp = __try_cast<FileInfo*>(myEnum->Current);
            Console::WriteLine(fiTemp->Name);
            path3 = String::Concat(path1,fiTemp->Name );
            path4 = String::Concat(path2,fiTemp->Name );
            File::Move(path3, path4);    
            }
    return 1;
    }

  7. #7
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    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

  8. #8
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Exclamation 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. which command can be used instead of enum for long integers?
    By aarti_gehani in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 04:05 AM
  2. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  3. Enum or Const Ints?
    By Syneris in forum C++ Programming
    Replies: 5
    Last Post: 01-20-2006, 04:58 AM
  4. Problem with enum and files
    By Robert_Sitter in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2005, 01:40 AM
  5. typedef enum question
    By .shifty in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2003, 04:43 AM