Waitrud Weber’s blog

things and reminders for memories

Windows API : Modified: FileControler::get_files

I modyfied get_files so that we could use in the case of file name which doesn't have meaning of number even asterisk but a file.

    
 27 ://
 28 :// it refers all to
 29 :// https://stackoverflow.com/questions/11362771/list-files-in-directory-c
 30 :// https://bituse.info/winapi/31
 31 ://
 32 :// Parameters
 33 ://
 34 :// lpcstr_path
 35 ://
 36 :// Directory name which is going to be found files.
 37 ://
 38 :// Return Value
 39 ://
 40 :// char**
 41 ://
 42 :// File list under the lpcstr_path, which has fullpath.
 43 ://
 44 :char** FileControler::get_files ( char* lpcstr_path, int* file_num ) {
 45 :	WIN32_FIND_DATA ffd;
 46 :	HANDLE hFind = INVALID_HANDLE_VALUE;
 47 :	char* ffdc_filename;
 48 :	int dummy_filenum = 0;
 49 :	char* char_asterisk_path = (char*)"\0"; // .\abc\* if .\abc\*.php
 50 :	char* char_attr = (char*)"\0";
 51 :	int cnt = 0;
 52 :	int cnt_char_asterisk_path = 0;
 53 :
 54 :	// this doesn't have asterisk.
 55 :	if ( m_contains ( lpcstr_path, "*" ) == 0 ) {
 56 :		*file_num = 1;
 57 :		this->put_string ( lpcstr_path );
 58 :		return get_strings();
 59 :	}
 60 :
 61 :    // Prepare string for use with FindFile functions.  First, copy the
 62 :    // string to a buffer, then append '\*' to the directory name.
 63 :	cnt = array_count(lpcstr_path);
 64 :	int search_length = m_last_with( lpcstr_path, (char*)".");
 65 :	if ( search_length <= 0 ) {
 66 :
 67 :		hFind = FindFirstFile( TEXT( lpcstr_path ), &ffd );
 68 :		char_asterisk_path = lpcstr_path;
 69 :		printf("char_asterisk_path: %s \r\n", char_asterisk_path );
 70 :	} else {
 71 :
 72 :		// In this case, lpcstr_path is like ".\abc\*.php"
 73 :		char_asterisk_path = substring( lpcstr_path, 0, search_length );
 74 :		cnt_char_asterisk_path = array_count( char_asterisk_path );
 75 :		char_attr = substring( lpcstr_path, search_length, cnt - cnt_char_asterisk_path ); // max_count:cnt
 76 :		hFind = FindFirstFile( TEXT( char_asterisk_path ), &ffd );
 77 :	}
 78 :
 79 :	//ffdc_filename = copyof( (char*)ffd.cFileName );
 80 :	//exit(-1);
 81 :
 82 :	int index = 0;
 83 :    // List all the files in the directory with some info about them.
 84 :	do
 85 :	{
 86 :		ffdc_filename = copyof( (char*)ffd.cFileName );
 87 :		printf("ffdc_filename: %s \r\n", ffdc_filename );
 88 :		index++;
 89 :		char* char_directory = to_directory ( char_asterisk_path, ffdc_filename );
 90 :
 91 :		if ( char_directory == NULL || m_compare( ffdc_filename, (char*)".") == 1 || m_compare( ffdc_filename, (char*)"..") == 1 ) {
 92 :
 93 :			printf ( "skip away : %s\r\n", ffdc_filename );
 94 :
 95 :		} else {
 96 :
 97 :			if ( is_attribute ( ffdc_filename, char_attr ) == 1 ) {
 98 :				this->put_string ( char_directory );
 99 :			}
100 :
101 :			if ( ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
102 :				char* listfiles = (char*) m_concat( (char*)char_directory, (char*)"\\*" );
103 :				listfiles = (char*) m_concat( (char*)listfiles, (char*)char_attr );
104 :				this->get_files( (char*) listfiles, &dummy_filenum );
105 :			}
106 :
107 :		}
108 :	}
109 :	while ( FindNextFile(hFind, &ffd ) != 0);
110 :
111 :	FindClose(hFind);
112 :	*file_num = this->get_strings_number () ;
113 :
114 :	return get_strings();
115 :}
116 :