CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2012
    Posts
    7

    need help with file searching and listing for an assignment


    Hi everyone sorry if it is not the place to ask this question but im new to this site and i am new to programming. I have recently been set an assignment for file searching and listing. Im in the first year of a course. What im trying to do is search in a folder what the user has requested. So far if i specify the folder in the code. it works for that folder. what i want to do is. the user inputs a folder of their choice and it searches for it. code is below:

    else if (UserSelection == 4)
    {
    Console.Write("Please type a specified path:");
    string User = Console.ReadLine();
    DirectoryInfo folderChange = new DirectoryInfo(User);
    FileInfo[] filechange = folderChange.GetFiles(User);
    for (int index = 0; index < filechange.LongLength; index++)
    {
    Console.WriteLine("{0}.{1} ({2} bytes)", FileListNumbering++, filechange[index].Name, filechange[index].Length);
    }
    Console.ReadLine();
    }

    This part I am trying to do the samebut let the user search his own extension input. I have tried string[] fileFiltering = Directory.GetFiles("C:\\Windows", "*.{0}", UserInput); but I keep getting errors


    else if (UserSelection == 2)
    {
    Console.Write("What filetype would you like to search for?: ");
    string UserInput = Console.ReadLine();

    if (UserInput == "exe")
    {
    string[] fileFiltering = Directory.GetFiles("C:\\Windows", "*.exe");
    for (int index = 0; index < fileFiltering.Length; index++)
    {
    Console.WriteLine("{0}", fileFiltering[index]);
    }
    Console.ReadLine();
    }

    ANY HELP WOULD BE GREATLY APPRECIATED.

  2. #2
    Join Date
    Feb 2011
    Location
    DeLand, FL
    Posts
    41

    Re: need help with file searching and listing for an assignment

    Abub,

    I'll try to help.

    I took the 2nd section of code you outlined and put it into a console application. It behaves as you would expect it to. What kind of errors are you seeing?

    This is what I have set up and it works OK.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleTesting {
    	class Program {
    		static void Main(string[] args) {
    
    			Console.Write("What filetype would you like to search for?: ");
    			string UserInput = Console.ReadLine();
    
    			if (UserInput == "exe") {
    				string[] fileFiltering = Directory.GetFiles("C:\\Windows", "*.exe");
    				for (int index = 0; index < fileFiltering.Length; index++) {
    					Console.WriteLine("{0}", fileFiltering[index]);
    				}
    			}
    			//
    			//  Pause waiting for user to respond before closing the console window.
    			//
    			if (System.Diagnostics.Debugger.IsAttached) {
    				Console.WriteLine("Press ENTER to continue: ");
    				Console.ReadLine();
    			} else
    				Console.WriteLine();
    		}
    	}
    }
    -Max

  3. #3
    Join Date
    Dec 2012
    Posts
    7

    Re: need help with file searching and listing for an assignment

    Quote Originally Posted by Max Peck View Post
    Abub,

    I'll try to help.

    I took the 2nd section of code you outlined and put it into a console application. It behaves as you would expect it to. What kind of errors are you seeing?

    This is what I have set up and it works OK.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleTesting {
    	class Program {
    		static void Main(string[] args) {
    
    			Console.Write("What filetype would you like to search for?: ");
    			string UserInput = Console.ReadLine();
    
    			if (UserInput == "exe") {
    				string[] fileFiltering = Directory.GetFiles("C:\\Windows", "*.exe");
    				for (int index = 0; index < fileFiltering.Length; index++) {
    					Console.WriteLine("{0}", fileFiltering[index]);
    				}
    			}
    			//
    			//  Pause waiting for user to respond before closing the console window.
    			//
    			if (System.Diagnostics.Debugger.IsAttached) {
    				Console.WriteLine("Press ENTER to continue: ");
    				Console.ReadLine();
    			} else
    				Console.WriteLine();
    		}
    	}
    }
    -Max
    Thanks very much for your help but what im ting to do is search the extension the user inputs. rather than have string[] fileFiltering = Directory.GetFiles("C:\\Windows", "*.exe"); I would like string[] fileFiltering = Directory.GetFiles("C:\\Windows", "*.{0}", UserInput); so that it searches any file the user inputted. rather than constantly being exe. but doing that gets a error. Hope you understand where im coming from

  4. #4
    Join Date
    Feb 2011
    Location
    DeLand, FL
    Posts
    41

    Re: need help with file searching and listing for an assignment

    Oh ... OK. Here's another version of it that will accomplish what you're after. You were trying to use a formatted representation ({0}) to put together your string. Also, don't test on the input "exe". The following works for any file extension you want to input.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleTesting {
    	class Program {
    		static void Main(string[] args) {
    
    			Console.Write("What filetype would you like to search for? : ");
    			string UserInput = Console.ReadLine();
    
    			string[] fileFiltering = Directory.GetFiles("C:\\Windows", "*." + UserInput);
    			for (int index = 0; index < fileFiltering.Length; index++) {
    				Console.WriteLine("{0}", fileFiltering[index]);
    			}
    			//
    			//  Pause waiting for user to respond before closing the console window.
    			//
    			if (System.Diagnostics.Debugger.IsAttached) {
    				Console.WriteLine("Press ENTER to continue: ");
    				Console.ReadLine();
    			} else
    				Console.WriteLine();
    			
    					
    		}
    	}
    }
    HTH,

    -Max

  5. #5
    Join Date
    Dec 2012
    Posts
    7

    Re: need help with file searching and listing for an assignment

    Quote Originally Posted by Max Peck View Post
    Oh ... OK. Here's another version of it that will accomplish what you're after. You were trying to use a formatted representation ({0}) to put together your string. Also, don't test on the input "exe". The following works for any file extension you want to input.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleTesting {
    	class Program {
    		static void Main(string[] args) {
    
    			Console.Write("What filetype would you like to search for? : ");
    			string UserInput = Console.ReadLine();
    
    			string[] fileFiltering = Directory.GetFiles("C:\\Windows", "*." + UserInput);
    			for (int index = 0; index < fileFiltering.Length; index++) {
    				Console.WriteLine("{0}", fileFiltering[index]);
    			}
    			//
    			//  Pause waiting for user to respond before closing the console window.
    			//
    			if (System.Diagnostics.Debugger.IsAttached) {
    				Console.WriteLine("Press ENTER to continue: ");
    				Console.ReadLine();
    			} else
    				Console.WriteLine();
    			
    					
    		}
    	}
    }
    HTH,

    -Max
    Thanks a lot. Just what I needed. Is there a similar way to do the first code or is it completely different? What I'm trying to do is a similar principle. Userinputs the folder THEY want to search. The program should read that info and search in/for that folder . The first bit of code I posted is related to this. But much appreciated

  6. #6
    Join Date
    Feb 2011
    Location
    DeLand, FL
    Posts
    41

    Re: need help with file searching and listing for an assignment

    Extending the functionality is simply a matter of allowing the user to input the path first then the extension to be searched. Here's my modified version of the routine:

    Code:
    //
    //  General Purpose console application for testing code constructs.
    //
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleTesting {
    	class Program {
    		static void Main(string[] args) {
    			//
    			//  Obtain the path then the file extension to be searched.
    			//
    			Console.Write("Enter the path to be searched: ");
    			string path = Console.ReadLine();
    			Console.Write("What filetype would you like to search for?: ");
    			string fileExtension = Console.ReadLine();
    
    			string[] fileFiltering = Directory.GetFiles(path, "*." + fileExtension);
    			for (int index = 0; index < fileFiltering.Length; index++) {
    				Console.WriteLine("{0}", fileFiltering[index]);
    			}
    			//
    			//  Pause waiting for user to respond before closing the console window.
    			//
    			if (System.Diagnostics.Debugger.IsAttached) {
    				Console.WriteLine("Press ENTER to continue: ");
    				Console.ReadLine();
    			} else
    				Console.WriteLine();
    			
    					
    		}
    	}
    }
    Now, there are things you could still do to improve the program, such as:

    1) Enclose the routine in a while loop of some kind that will allow the program to ask you for another set of parameters instead of having to re-run the program each time. Of course you would need to provide a way for the user to say "OK, I'm done" so he could exit the program.
    2) Validation of the input parameters: For example you could code it so that if a user enters a path that doesn't exist that the program could issue an error message to that effect rather than just not returning any results.
    3) The program could be done in a GUI (Instead of a Console application) so the output could be made more attractive.

    You're at a fun time right now. Enjoy the adventure of tinkering with it to see what you can make it do.

    -Max :-)

  7. #7
    Join Date
    Dec 2012
    Posts
    7

    Re: need help with file searching and listing for an assignment

    Thanks a lot max. You''ve been great help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured