Determine whether a folder exists and create a folder

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.