Windows programming

Moderators: None (Apply to moderate this forum)
Number of threads: 3711
Number of posts: 9173

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Functions calling themselves? Posted by Sephiroth on 20 Jul 2001 at 9:26 PM
I am taking a day or two off from my world editor to build a simple (actually, rebuild) temporary file remover, which will delete all the files in "Windows\TEMP" and in any of it's subfolders, as well as the actual subfolder. I have two questions about this though. When FindFirstFile or FindNextFile return, they only return the file or directory name, and I need the full drive and path to delete files and subfolders. Second, can a function call on itself? The way I plan on doing it is this (this is my actual code so far):
void remove(char place[512])
	{
	WIN32_FIND_DATA fd;

	fn = FindFirstFile(place, &fd);

	if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
		{
		/*
		First get the full path and directory name
		Now call remove() with that info
		Once it returns, you can delete the empty folder!
		*/
		}
	else
		//Just delete the file, if not a dir

	//Code in a loop with FindNextFile here!
	return;
	}

void clean(HWND cwnd)
	{
	if(strstr(paths[0], "Not used.") == NULL)
		remove(paths[0]);
	return;
	}

Can somebody please help me out with this? I appreciate it.
<br>-Sephiroth

Report
Re: Functions calling themselves? Posted by reunion on 21 Jul 2001 at 1:49 PM
: I am taking a day or two off from my world editor to build a simple (actually, rebuild) temporary file remover, which will delete all the files in "Windows\TEMP" and in any of it's subfolders, as well as the actual subfolder. I have two questions about this though. When FindFirstFile or FindNextFile return, they only return the file or directory name, and I need the full drive and path to delete files and subfolders. Second, can a function call on itself? The way I plan on doing it is this (this is my actual code so far):

Ok. There is nothing wrong with functions calling themselves.

int factorial(int n)
{
  return ((n <= 0) ? 1 : n * factorial(n - 1)); 
};


Like this one. It is often more effective when functions call themselves then using cycles.
Though, every calling of a function costs some time, of course. Using cycles is faster.

Very well, I think with our 1GHz proccessors it doesn't matter at all. ;o)

Reunion



Report
Re: Functions calling themselves? Posted by weicco on 22 Jul 2001 at 10:58 PM
So you're talking about recursive function call. This is very handy in a case like deleting subfolders and contents. Here's my code that finds all .mp3 files and changes their attributes. I think there's some bug in it but you get the picture.

void searchloop(void) {
long res;
struct _finddata_t fileinfo;

res = _findfirst("*.*", &fileinfo);

while(1) {
if((strcmp(fileinfo.name, ".") != 0)&&(strcmp(fileinfo.name, "..") != 0))
if(_chdir(fileinfo.name) == 0) {
searchloop();
} else {
if(!strcmp(fileinfo.name, ".mp3")) {
SetFileAttributes(fileinfo.name, FILE_ATTRIBUTE_NORMAL);
}
}
if(_findnext(res, &fileinfo) < 0)
break;
}
_chdir("..");
}

: I am taking a day or two off from my world editor to build a simple (actually, rebuild) temporary file remover, which will delete all the files in "Windows\TEMP" and in any of it's subfolders, as well as the actual subfolder. I have two questions about this though. When FindFirstFile or FindNextFile return, they only return the file or directory name, and I need the full drive and path to delete files and subfolders. Second, can a function call on itself? The way I plan on doing it is this (this is my actual code so far):
:
: void remove(char place[512])
: 	{
: 	WIN32_FIND_DATA fd;
: 
: 	fn = FindFirstFile(place, &fd);
: 
: 	if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
: 		{
: 		/*
: 		First get the full path and directory name
: 		Now call remove() with that info
: 		Once it returns, you can delete the empty folder!
: 		*/
: 		}
: 	else
: 		//Just delete the file, if not a dir
: 
: 	//Code in a loop with FindNextFile here!
: 	return;
: 	}
: 
: void clean(HWND cwnd)
: 	{
: 	if(strstr(paths[0], "Not used.") == NULL)
: 		remove(paths[0]);
: 	return;
: 	}
: 

: Can somebody please help me out with this? I appreciate it.
: -Sephiroth
:





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.