Results 1 to 2 of 2
  1. #1
    InunoTaishou's Avatar
    Join Date
    Jan 2009
    Gender
    male
    Location
    The Internet
    Posts
    446
    Reputation
    20
    Thanks
    950
    My Mood
    Relaxed

    Finding all directories in current directory, getting weird characters

    Trying to get a list of all directories in the current directory (and eventually directories within all directories, within directories, within directories, etc) and then get a list of all files in directories. Problem is, I'm using TCHAR[] for my buffer, working just fine, when I go to my listDirectories function, which stores all found directories in a std::vector<TCHAR*> it stores weird characters.

    I can do a std::wcout << buffer << std::endl; and it will print the proper directory fine. I suspect it's my vector doing some bad conversion but TCHAR[] to TCHAR* shouldn't have any weird conversions. But, what do I know. I haven't done really any windows coding.

    Code:
    #include <iostream>
    #include <vector>
    #include <Windows.h>
    #include <tchar.h>
    
    // Declare function prototypes
    DWORD listDirectories(std::vector<TCHAR*>&);
    
    // Global variable that holds the current working path of the program
    TCHAR buffer[MAX_PATH];
    
    void main()
    {
    	// Declare variables, dwCount for return value from listDirectories, directories stores all sub directories, cDirectory stores current directory
    	DWORD dwCount;
    	std::vector<TCHAR*> directories;
    	TCHAR cDirectory[MAX_PATH];
    
    	// Get current directory
    	GetCurrentDirectory(MAX_PATH, buffer);
    
    	// Set cDirectory (current directory) to buffer (ATM is current directory) + add \\ to the end and make it the working directory
    	_tcscpy_s(cDirectory, buffer);
    	_tcscpy_s(cDirectory, "\\");
    
    	// dwCount is count of how many directories found, to be used later
    	dwCount = listDirectories(directories);
    
    	// Range for loop to print each value in the std::vector<TCHAR*> directories
    	for (auto tStr : directories) {
    		// std::cout and std::wcout print the same weird characters
    		std::wcout << tStr << std::endl;
    	}
    	
    	std::cin.get();
    }	// end void main()
    
    DWORD listDirectories(std::vector<TCHAR*> &directories)
    {
    	// Declare variables, count used for number of directories, hFind for FindFirstFile, data used to store file data
    	DWORD count = 0;
    	HANDLE hFind = INVALID_HANDLE_VALUE;
    	WIN32_FIND_DATA data;
    	
    	// Append "\\*" to buffer to make it a working directory
    	_tcscat_s(buffer, "\\*");
    
    	// Find first file in the current working directory, storying data in data as a reference
    	hFind = FindFirstFile(buffer, &data);
    
    	// If hFind is not an invalid handle
    	if (hFind != INVALID_HANDLE_VALUE) {
    		// Go through each file in the directory
    		do
    		{
    			// If the file attributes is a directory
    			if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    			{
    				// Create a sub directory
    				TCHAR subDir[MAX_PATH];
    
    				// Fill subDir with current directory + "\\" + dir name
    				sprintf_s(subDir, "%s%s%s", currentDir, "\\", data.cFileName);
    
    				// Add directory to my directories (std::vector<TCHAR*>)
    				directories.push_back(data.cFileName);//data.cFileName);
    				
    				// Add 1 to count, used as the return for how many directories found in the current directory
    				count++;
    			}
    		} while (FindNextFile(hFind, &data) != 0);
    	}
    
    	// Return count of directories found
    	return count;
    }	// end DWORD listDirectories(std::vector<TCHAR*>&)
    Last edited by InunoTaishou; 05-11-2015 at 07:25 AM.
    https://www.mpgh.net/forum/signaturepics/sigpic210976_1.gif

  2. #2
    InunoTaishou's Avatar
    Join Date
    Jan 2009
    Gender
    male
    Location
    The Internet
    Posts
    446
    Reputation
    20
    Thanks
    950
    My Mood
    Relaxed
    Changing the std::vector<TCHAR*> to std::vector<std::basic_string<TCHAR>> fixes the issue. I switched over to std::wstring instead because it's easier and TCHAR kind of sucks.

    Got it working but I'll post full code later.

    - - - Updated - - -

    Can't edit my first post but here is the solution. Recursively finds all sub directories within the current directory (and I do mean all of them. Threw it on my C: drive and it took about 15 seconds but it found them all, and then 3 minutes to std::wcout all of the directories, used almost 400,000kb of RAM to do it lol)

    Code:
    #include <iostream>
    #include <vector>
    #include <Windows.h>
    #include <stdio.h>
    #include <string>
    
    // Declare function prototypes
    DWORD listDirectories(std::vector<std::wstring>&, std::wstring);
    
    void main()
    {
    	// Declare variables: dwCount (count of files/directories found), directories (all sub directories), startDir (GetCurrentDirectory)
    	// buffer (holds initial value for the GetCurrentDirectory)
    	DWORD dwCount;
    	std::vector<std::wstring> directories;
    	std::vector<std::wstring> files;
    	std::wstring startDir;
    	WCHAR buffer[MAX_PATH];
    
    	// Get current directory
    	GetCurrentDirectory(MAX_PATH, buffer);
    
    	// Set my starting directory and current working directory to the buffer
    	startDir = buffer;
    
    	// Get the current directories within the start directory
    	listDirectories(directories, startDir);
    
    	// For loop to go through directories and add to it. Note: Could not use iterators since the directories.end() iterator constantly changes.
    	// Using subscript works properly and the size constantly changes until there are no more sub directories being added to the std::vector<std::wstring>
    	for (int i = 0; i != directories.size(); i++) {
    		listDirectories(directories, directories[i]);
    	}
    	
    	// List all directories found
    	for (auto dir : directories) {
    		std::wcout << dir << std::endl;
    	}
    
    	std::cin.get();
    }   // end void main()
    
    DWORD listDirectories(std::vector<std::wstring> &directories, std::wstring dir)
    {
    	// Declare variables, count used for number of directories, hFind for FindFirstFile, data used to store file data
    	DWORD count = 0;
    	HANDLE hFind = INVALID_HANDLE_VALUE;
    	WIN32_FIND_DATA data;
    
    	// Find first file in the current working directory, storying data in data as a reference
    	//hFind = FindFirstFile(dir.c_str(), &data);
    	hFind = FindFirstFile((dir + L"\\*").c_str(), &data);
    
    	// If hFind is not an invalid handle
    	if (hFind != INVALID_HANDLE_VALUE) {
    		// Go through each file in the directory
    		do
    		{
    			// If the file attributes is a directory
    			if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    			{
    				// Add directory to my directories (std::vector<std::wstring). if (count > 1) because I want to ignore the "." and ".." directories
    				if (count > 1) {
    					directories.push_back(dir + L"\\" + data.cFileName);
    				}	// end if (count > 1)
    
    				// Add 1 to count, used as the return for how many directories found in the current directory
    				count++;
    			}
    		} while (FindNextFile(hFind, &data) != 0);	// end while (FindNextFile(hFind, &data) != 0);
    	}
    	
    	// Return count of directories found
    	return (count - 2);
    }   // end DWORD listDirectories(std::vector<std::wstring>&, std::wstring)
    Last edited by InunoTaishou; 05-12-2015 at 06:31 PM.
    https://www.mpgh.net/forum/signaturepics/sigpic210976_1.gif

Similar Threads

  1. [Discussion] Where are people getting hacks if all of the current ones are patched or detected?
    By PurpleCHEEZ in forum CrossFire Discussions
    Replies: 4
    Last Post: 02-01-2015, 07:51 AM
  2. Scan all files and directories in lua directory
    By Blue Kirby in forum Garry's Mod Discussions & Help
    Replies: 12
    Last Post: 05-31-2013, 02:58 AM
  3. Current Directory
    By guza44_44 in forum Visual Basic Programming
    Replies: 7
    Last Post: 01-12-2010, 12:51 AM
  4. To All members who didn't get their VIPs
    By Diisasta in forum News & Announcements
    Replies: 54
    Last Post: 06-02-2009, 12:59 AM
  5. [CS 1.6] All the missing .wad files (Get them here)
    By stingray001 in forum General Game Hacking
    Replies: 1
    Last Post: 05-17-2006, 08:36 PM