passing information into the findfile findnext function

This is a discussion on passing information into the findfile findnext function within the C++ Programming forums, part of the General Programming Boards category; Hi, me an my friend are creating a program that requires locating files, which requires knowing the file path. We ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Question passing information into the findfile findnext 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
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Yes, use the Win32 versions of findfile (some examples in the FAQ), NOT the obsolete DOS versions.

    Remove io.h from your includes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So put it there.
    Code:
    stringstream path;
    path << logical_drive_letter;
    path << ":/Users/";
    path << username;
    path << "/other/things";
    _findfirst(path.string().c_str());
    You'll need <sstream> for stringstreams, of course.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Would you believe it - another damn cross-poster!
    Another peachy waste of effort on some selfish git.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by GeekInTraining View Post
    [/code]
    int ff = _findfirst ("/Users/ /AppData/Local/Microsoft/Windows/*.*", &data);
    [/CODE]
    We need to pass the logical drive letter before /Users and we need to pass the username in between /Users and /AppData.
    You need to do a lot more than that...

    1) in Windows directory separaters are backslashes \
    2) in C and C++ the backslash is used to insert special charaters into strings. so you need \\

    Thus "\\users\\appdata\\local\\microsoft\\windows\\ *" is the correct search string

    Moreover that particular path is a hidden system path... used by windows to manage user details... Not a good place to be messing around, especially with cut and paste code...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Have you ever tried forward slashes in a Windows program? You might surprise yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21