Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 14:55

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Empty Directory Enumeration

Discussion on Empty Directory Enumeration within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
XxharCs's Avatar
 
elite*gold: 34
Join Date: Apr 2011
Posts: 1,475
Received Thanks: 1,228
Empty Directory Enumeration

Hallo!

Ich enumiere alle Ordner eines Pfads bzw. eines Datenträgers und suche nach leeren Ordnern.

Soweit so gut, hab ichs geschafft, wollte aber fragen ob ich die rekursion besser gestalten kann, weil ich so ein Gefühl habe, dass man es sicher besser machen kann

Danke in vorraus!

Code:
int searchEmptyDirs(vector<string> &storedFiles, const string &startDir)
{
	string strFilePath;
	string strPattern;
	HANDLE hFile;
	WIN32_FIND_DATA fileInformation;
	
	strPattern = startDir + "\\*.*";
	hFile = FindFirstFileA(strPattern.c_str(), &fileInformation);

	if(hFile != INVALID_HANDLE_VALUE)
	{
		do
		{
			if(fileInformation.cFileName[0] != '.')
			{
				strFilePath.erase();
				strFilePath = startDir + "\\" + fileInformation.cFileName;

				if(fileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				{
					
					if(PathIsDirectoryEmpty(strFilePath.c_str()))
					{
						storedFiles.push_back(strFilePath);
						
					}

					int res = searchEmptyDirs(storedFiles, strFilePath);
					if(res)
						return res;
				}
			}
		}while(FindNextFileA(hFile, &fileInformation) == TRUE);

		FindClose(hFile);

		DWORD dwError = GetLastError();
		if(dwError != ERROR_NO_MORE_FILES)
			return dwError;
	}

	return 0;
}
Edit:
Und wie gehe ich Ordner die mit einem '.' beginnen durch? Also zB: .Bilder
XxharCs is offline  
Old 01/29/2014, 15:21   #2
 
nerdsupreme's Avatar
 
elite*gold: 0
Join Date: Jan 2014
Posts: 103
Received Thanks: 55
lass dich nicht so von deinen gefühlen leiten.
nerdsupreme is offline  
Old 02/04/2014, 00:21   #3
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
Du kannst die Rekusion verbessern, indem du ganz auf sie verzichtest!

Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <filesystem>

namespace sys = std::tr2::sys;

std::vector<sys::path> getEmptyDirectories(const sys::path& in)
{
	std::vector<sys::path> directories;

	if (!sys::exists(in))
		return directories;

	sys::directory_iterator begin(in);
	sys::directory_iterator end;

	std::for_each(begin, end, [&](sys::directory_entry directoryEntry)
	{
		auto path = directoryEntry.path();

		if (sys::is_directory(path) && sys::is_empty(path))
			directories.push_back(path);
	});

	return directories;
}

int main()
{
	auto directories = getEmptyDirectories(sys::current_path<sys::path>());

	std::cout << "Directories:\n";
	std::copy(directories.begin(), directories.end(), std::ostream_iterator<std::string>(std::cout, "\n"));

	std::cin.get();
	return 0;
}
Benötigt derzeit noch Visual Studio 2013, aber das wird noch dieses Jahr Standard, wodurch das dann später auch mit dem GCC laufen wird.

Das hier hat folgende Vorteile:
-Keine Rekursion
-Plattformübergreifend
-Lesbarer & Kürzer (dadurch wartbarer)
-Robuster durch strong exception safety
-Mehr Buzzwörter die mir gerade nicht einfallen
Nightblizard is offline  
Thanks
7 Users
Reply


Similar Threads Similar Threads
[ARCHON] Wtb ranger empty empty lvl 70
05/15/2011 - Archlord Trading - 2 Replies
wtb ranger lvl 70 empty need nothing on it greetz
directory mv: rename cores/core.5 to cores/core.4: No such file or directory mv: rena
09/16/2010 - Metin2 Private Server - 7 Replies
Moin Leute hab mein root rebootet und nun das http://img3.imagebanana.com/img/huvw54l6/Unbenann t.JPG hoffe ihr könnt mir schnell hilfen um das problem zulösen. thx
(Rappelz) Server: Bahamut WTB Empty-Angels, Empty-Kentas
09/25/2009 - Rappelz Trading - 1 Replies
WTB empty angels and empty kentas on bahamut. Private messege or post the cost of each empty angel/kenta and the number of cards your selling. Paying real cash via paypal.
WTB 2 empty blode or 2 emptyshuta and empty karkean [BRUMHART]
03/03/2009 - Archlord Trading - 0 Replies
pm me here or ingame Shien, Soulasylum n Vash73 if u have the items so we can make a deal, preferably empty. paying thru paypal only. tell me ur prices.



All times are GMT +1. The time now is 14:56.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.