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

    How to enter a directory with random name?

    In Vista, I want my code to create the following data inside a string:

    Code:
    // string ffpath holds:
    "C:\\Users\\$_CurrentUser_$\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\ewg8ep30.default\\"
    I need to use the WinAPI code:

    Code:
    DeleteFile( ffPath + "bookmarks.html" );
    As you can see, the last folder name seems to be very randomized (ewg8ep30.default) for each computer and I'm guessing the more firefox profiles one has, the more random folders there will be (lets assume all users using my code will have only one).

    What techniques can I use to get that folders name into the string and delete bookmarks.html? "C:\Users\$_CurrentUser_$\AppData\Roaming\Mozilla\Firefox\Profiles\" is already determined but I need to join that last part to create the full path just this one time to delete bookmarks.html.

    I was thinking maybe search \Profiles\ for other folders? But im not sure how to do that or if thats a good method.

    Maybe theres a way to "skip" the next folder once inside \Profiles\?

    Thanks.
    Last edited by ASHL33T; January 2nd, 2010 at 06:42 AM.
    New to C++ and Visual C++ 2008 Express Edition.

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: How to enter a directory with random name?

    In windows, FindFirstFile and FindNextFile can be used to iterate through a directory:

    Code:
    int main()
    {
      WIN32_FIND_DATA wfd;
      HANDLE f;
      int err;
      wchar_t ProfDir[MAX_PATH];
    
      //I'd do it this way instead of $_CurrentUser_$:
      wchar_t *curusr=_wgetenv(L"USERPROFILE");
      if(!curusr){
        return 1;
      }
    
      swprintf(ProfDir,L"%s\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\*",curusr);
      f=FindFirstFile(ProfDir,&wfd);
      if(f==INVALID_HANDLE_VALUE){
        return 1;
      }
      do{
        wprintf(L"%s\n",wfd.cFileName);
      }while(FindNextFile(f,&wfd));
      FindClose(f);
      return 0;
    }

  3. #3
    Join Date
    Dec 2009
    Posts
    44

    Re: How to enter a directory with random name?

    Could you please add some comments in or explain a bit from top to bottom? I'm not exactly sure whats going here since im new to the syntax (I apologize). Ignore $_CURRENTUSER_$, thats just to let the forums know its a variable that's already gonna be filled in (i've already used GetUserName() call to simplify that part).

    To simplify the problem:
    bookmarks.html exists inside \\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\RANDOM_FOLDER_NAME, RANDOM_FOLDER_NAME is a name not given to the program, and therefore I need a function that will reveal that name or the full path of where bookmarks.html relies.

    Is it possible to search all of Profiles\\ for bookmarks.html and return the full location of it?

    Thanks!
    Last edited by ASHL33T; January 3rd, 2010 at 12:03 AM.
    New to C++ and Visual C++ 2008 Express Edition.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to enter a directory with random name?

    Quote Originally Posted by ASHL33T View Post
    Is it possible to search all of Profiles\\ for bookmarks.html and return the full location of it?
    It can be done, since Windows File Search can do it.

    You can do it by using FindFirstFile() and FindNextFile(). You use the WIN32_FIND_DATA structure to determine what you've found (directory or file name). If it's a directory, then you recursively go into that directory and find the file you're looking for.

    Why not write a simple program that just calls FindFirstFile and then FindNextFile in a loop? Debug it, and see at each step when FindNextFile returns, inspect the WIN32_FIND_DATA structure. Once you understand how it works, then you formulate the loop to do anything you want.

    Here is the CodeGuru FAQ on file searching:

    http://www.codeguru.com/forum/showthread.php?t=312461

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Dec 2009
    Posts
    44

    Re: How to enter a directory with random name?

    I'm sorry say it again but this is all really beyond me, I'm new to the syntax and looking at FAQs or large pieces of code with things added to them really confuse me.

    Is there any tutorial I can read on basic file searching and returning the found file's path?
    New to C++ and Visual C++ 2008 Express Edition.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: How to enter a directory with random name?

    Quote Originally Posted by ASHL33T View Post
    I'm sorry say it again but this is all really beyond me, I'm new to the syntax and looking at FAQs or large pieces of code with things added to them really confuse me.

    Is there any tutorial I can read on basic file searching and returning the found file's path?
    Probably, such tutorials exist but you should not expect to find, in any of them, examples ready to be copy/pasted in every particular application.
    The previous answers posted by hoxsiew and Paul McKenzie give almost all necessary hints. All remaining for you is to sweat a little and to avoid
    "I'm new to the syntax", "This is all really beyond me", then... "Could you please add some comments in or explain a bit from top to bottom?" (what about "Please, write yourself my program for free!"). .
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Dec 2009
    Posts
    44

    Re: How to enter a directory with random name?

    I get a bunch of errors using the above codes. I never asked anyone to write my program for free, I just have no idea what half this stuff is saying, I'm trying my best to read it but im not at the point with C++ where I can just pull out what I need from large sections of code. Maybe the hints work for you, but they don't for me. Even doing some research on it yields complicated results where "sweating a little" didnt help me much. If you read my first post again, all im asking for is a concept or how to complete the problem at hand. Thats the whole point of a forums, I guess not though, thank you anyway.

    For anyone else wondering how I solved it:

    Code:
            WIN32_FIND_DATA	data;
    	string	profileDir( "C:\\Users\\CURRENT_USER_HERE\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\" );
            string       findPath( profileDir + "*.*" ), dataDir;
    
    
    	HANDLE h = FindFirstFile( findPath.c_str(), &data );
    	if( h )	{	
    		do {
    			char* currentDir = new char[ lstrlen( data.cFileName ) + 1 ];
    			for( int i = 0; i < lstrlen( data.cFileName ); i++ )
    					currentDir[ i ] = char( data.cFileName[ i ] );
    			currentDir[ lstrlen( data.cFileName ) ] = '\0';
    			dataDir = profileDir + currentDir + "\\"; // Stores the whole path of the last folder, and you can use this to access bookmarks.html or whatever file.
    		} while( FindNextFile( h, &data ) );
    		FindClose(h);
    	}
    Last edited by ASHL33T; January 3rd, 2010 at 10:35 AM.
    New to C++ and Visual C++ 2008 Express Edition.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to enter a directory with random name?

    Quote Originally Posted by ASHL33T View Post
    I never asked anyone to write my program for free, I just have no idea what half this stuff is saying, even doing some research on it yields complicated results that "sweating a little" didnt help me much. Thats the whole point of a forums, I guess not though, thank you anyway.
    You don't seem to understand -- The Windows API, and really any API, is meant for programmers who are already familiar with the language they're using -- familiar enough so that they know how to call functions, pass correct parameters, check return codes (if any), and read and understand the documentation associated with the API.

    API's are not meant for beginner programmers -- that's the bottom line. If you do not have the requisite knowledge of the language you're using, then you need to gain that knowledge before embarking in calling API routines. You were given the answer by both myself and hoxsiew as to the API routines that search directories. You have sample programs, MSDN, etc. , so there is nothing else we can do or anyone else can do beyond writing the code for you.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to enter a directory with random name?

    Quote Originally Posted by ASHL33T View Post
    I get a bunch of errors using the above codes.
    If you copied and pasted the exact examples in the FAQ, then there should not have been any errors.
    For anyone else wondering how I solved it:

    Code:
            WIN32_FIND_DATA	data;
    	string	profileDir( "C:\\Users\\CURRENT_USER_HERE\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\" );
            string       findPath( profileDir + "*.*" ), dataDir;
    
    
    	HANDLE h = FindFirstFile( findPath.c_str(), &data );
    	if( h )	{	
    		do {
    			char* currentDir = new char[ lstrlen( data.cFileName ) + 1 ];
    			for( int i = 0; i < lstrlen( data.cFileName ); i++ )
    					currentDir[ i ] = char( data.cFileName[ i ] );
    			currentDir[ lstrlen( data.cFileName ) ] = '\0';
    			dataDir = profileDir + currentDir + "\\"; // Stores the whole path of the last folder, and you can use this to access bookmarks.html or whatever file.
    		} while( FindNextFile( h, &data ) );
    		FindClose(h);
    	}
    That code above has major problems. The major problem is that you create memory leaks. There is no delete[] for that call to new[], and worse, that call to new[] is in a loop.

    You could have just used std::string and not have to do any memory allocation:
    Code:
    	do {
    		string currentDir = data.cFileName;
    		dataDir = profileDir + currentDir + "\\"; // Stores the whole path of the last folder, and you can use this to access bookmarks.html or whatever file.
    		} while( FindNextFile( h, &data ) );
    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: How to enter a directory with random name?

    Look at next quotes to discover yet another one mistake:

    From MSDN
    FindFirstFile
    If the function fails, the return value is INVALID_HANDLE_VALUE.
    From the FAQ pointed by Paul
    hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
    if(hFile != INVALID_HANDLE_VALUE)
    From the code posted by hoxsiew
    f=FindFirstFile(ProfDir,&wfd);
    if(f==INVALID_HANDLE_VALUE){

    From WINBASE.H where INVALID_HANDLE_VALUE is defined
    #define INVALID_HANDLE_VALUE (HANDLE)-1
    And finally...
    From your code
    HANDLE h = FindFirstFile( findPath.c_str(), &data );
    if( h ) {
    Have you seen it?



    // Even reading the return value in the function documentation takes a little sweat, isn't it?
    Last edited by ovidiucucu; January 3rd, 2010 at 11:41 AM. Reason: typo
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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