Passing variables to FindFile Function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Passing variables to FindFile Function

  1. #1
    Join Date
    Jan 2011
    Posts
    2

    Exclamation Passing variables to FindFile Function

    Hi, me an my friend are creating a program that requires locating files, which requires knowing the file path. We came across functions to find the user name of the current person logged into windows, and to find all the logical drives on the target computer. We need help passing the information gained from these functions into the findfile function . Here's our code:
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <io.h>
    #include <time.h>
    #include <string>
    #include <windows.h>
    #include <direct.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <Lmcons.h>
    
    using namespace std;
    
    
    void DumpEntry(_finddata_t &data)
    {
    
    	
    
        if (data.attrib & _A_SUBDIR == _A_SUBDIR)
        {
            cout << "[" << data.name << "]" << endl;
        }
        else
        {
    			cout << data.name << endl;
        }
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    TCHAR name [ UNLEN + 1 ];
      DWORD size = UNLEN + 1;
    	
      if (GetUserName( (TCHAR*)name, &size ))
        wcout << L"Hello, " << name << L"!\n";
      else
        cout << "Hello, unnamed person!\n";
    
    	TCHAR szBuffer[1024];
    ::GetLogicalDriveStrings(1024, szBuffer);
    TCHAR *pch = szBuffer;
    while (*pch) {
    _tprintf(TEXT("%s\n"), pch);
    pch = &pch[_tcslen(pch) + 1];
    }
    
    	_finddata_t data;
    
        
        int ff = _findfirst ("/Users/ /AppData/Local/Microsoft/Windows/*.*", &data);
    
        
    	if (ff != -1)
        {
            int res = 0;
    		
            while (res != -1)
            {
                DumpEntry(data);
                res = _findnext(ff, &data);
            }
    		
            _findclose(ff);
    	}
    
    	return 0;
    }
    We need to pass the logical drive letter before /Users and we need to pass the username in between /Users and /AppData.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Passing variables to FindFile Function

    Can't sprintf (or swprintf, or _stprintf) help you?

    Besides, there are two (potential) problems in your code snippet:
    1. The mixture of plain char/char* types and TCHAR/TCHAR*; so there may be problem in UNICODE build.
    2. The folder names for user AppData and other special folders are local specific and OS-specific. Your example won't work on any non-english OS.
      The correct way would be using SHGetFolderPath to obtain the correct folder path names.
    Last edited by VictorN; January 16th, 2011 at 06:13 AM.
    Victor Nijegorodov

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center