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.
Re: Passing variables to FindFile Function
Can't sprintf (or swprintf, or _stprintf) help you? :confused:
Besides, there are two (potential) problems in your code snippet:
- The mixture of plain char/char* types and TCHAR/TCHAR*; so there may be problem in UNICODE build.
- 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.