Determines whether a folder with a known name exists in the specified position. If no folder exists, create a new folder. Passed in VC 6.0.
# Include <iostream> # include <windows. h> # include <string. h> using namespace STD; // determines whether a folder exists. If not, create the folder bool direxist (const tchar * pszdirname) {win32_find_data fileinfo; tchar _ szdir [_ max_path]; strcpy (_ szdir, pszdirname); int nlen = strlen (_ szdir); If (_ szdir [nLen-1] = '//') | (_ szdir [nLen-1] = '/') {_ szdir [nLen-1] = (char) '/0'; // ZX} Handle hfind = :: findfirstfile (_ szdir, & fileinfo); If (hfind = Invalid_handle_value) {return false;} If (fileinfo. dwfileattributes = file_attribute_directory) {: findclose (hfind); Return true; }:: findclose (hfind); Return false;} bool createdir (const tchar * pszdirname) {bool Bret = false; tchar _ szdir [_ max_path]; tchar _ sztmp [_ max_dir]; int nlen = 0; int idx; If (direxist (pszdirname )) = true) return true; strcpy (_ szdir, pszdirname); nlen = strlen (_ SZD IR); If (_ szdir [nLen-1] = '//' | _ szdir [nLen-1] = '/') {_ szdir [nLen-1] = (char) '/0'; // ZX} nlen = strlen (_ szdir); memset (_ sztmp, 0, _ max_dir); tchar _ STR [2]; for (idx = 0; idx <nlen; idx ++) {If (_ szdir [idx]! = '//') {_ STR [0] = _ szdir [idx]; _ STR [1] = 0; strcat (_ sztmp, _ Str );} else {Bret =: createdirectory (_ sztmp, null); If (BRET) {: setfileattributes (_ sztmp, file_attribute_normal );} _ STR [0] = _ szdir [idx]; _ STR [1] = 0; strcat (_ sztmp, _ Str);} If (idx = nLen-1) {Bret =: createdirectory (_ sztmp, null); If (BRET) {: setfileattributes (_ sztmp, file_attribute_normal) ;}} if (direxist (_ sztmp )) return true; Re Turn false;} int main () {char dir [] = "f :\\ 123"; if (false = direxist ("dir ")) // check whether a folder named 123 exists in drive f {cout <"not find! "<Endl; If (true = createdir (DIR) // create the 123 folder cout <" create success! "<Endl; else cout <" create failed! "<Endl;} elsecout <" find it! "<Endl; System (" pause "); Return 0 ;}