Delete a Directory?

Posted By: Dooley

Delete a Directory? - 05/27/18 10:48

Is there any way to delete a directory in lite-C?

I found "file_delete" but cannot find anything similar for directories.

I am able to create a directory in the following somewhat messy way...

Quote:

//NEW FOLDER OPTION - Creates a folder name based on user input
str_cpy(save_dir,level_string);

compatibility = 8;

galaxy_write = file_open_write("Save_Test.txt");

file_str_write(galaxy_write,"SAVE TEST");//creates a file in order to
//actually create the folder

file_str_write(galaxy_write,",");

file_close(galaxy_write);

compatibility = 10;//resumes normal compatibility


If there's a better way to do it, I would love to know that too.

However, once it's there, I have found that I cannot get rid of it without manually deleting through Windows.
Posted By: Emre

Re: Delete a Directory? - 05/27/18 21:57

Did you try RemoveDirectory function from windows.h? Directory must be empty if you gonna use this function. So you should first delete the files in the directory i guess.
Posted By: Dooley

Re: Delete a Directory? - 05/28/18 05:03

Thanks! This offers two unique challenges, as the directory will have a number of files in it, and I don't think I've ever used a command through Windows.h. However, I will look into it!
Posted By: Emre

Re: Delete a Directory? - 05/28/18 07:41

Okay. I will give you a function. This function remove all files from directory.

Code:
int iffile_exist(char* fileName) 
{
	long fileAttr;
	fileAttr = GetFileAttributes(fileName);
	if (0xFFFFFFFF == fileAttr)
	return 0;
	return 1;
}

STRING* str_temp="";
STRING* str_folder_temp="";
WIN32_FIND_DATA file_handle;
HANDLE FolderHandle;

function remove_all_files_fromfolder(STRING* folder)
{

	str_cpy(str_folder_temp,folder);
	str_cat(str_folder_temp,"*.*");//all files

	FolderHandle = FindFirstFile(_chr(str_folder_temp),&file_handle);
	if ( INVALID_HANDLE_VALUE == FolderHandle ) 
	{
		printf("No file or directory");
		return -1;
	}
	while(1)
	{
		
		str_cpy(str_temp,folder);
		str_cat(str_temp,file_handle.cFileName);
		//printf(_chr(str_temp));
		if(iffile_exist(_chr(str_temp))==1)//if file exist
		{
			file_delete(str_temp);
		}
		
		if ( FindNextFile(FolderHandle, &file_handle )== 0 )
		{
			break;	
		}
		
	}

}
function on_1_event()
{
	remove_all_files_fromfolder("G://2015//Aynalar//aaa//");	
}


You have to use two slash for folder adress. the forum does not allow me to write two slashes side by side. Here is what i'm talking about:


Then you can try to use RemoveDirectory function.
Posted By: Dooley

Re: Delete a Directory? - 05/28/18 18:32

You are awesome, thanks!
Posted By: Dooley

Re: Delete a Directory? - 05/28/18 18:57

This is really great, and in fact it solves my whole problem. It was the files in that directory that really needed to be deleted, the directory itself can actually stay with no problems.
Posted By: Emre

Re: Delete a Directory? - 05/28/18 19:10

Good to hear that! I'm always glad to help.
© 2024 lite-C Forums