C + + copyfile,getfileattributes copying files and changing file properties

Source: Internet
Author: User

By using the GetFileAttributes or GetFileAttributes function to get the properties of the file, the CreateFile and SetFileAttributes functions can set the properties of the file.


Example:

#include <windows.h> #include <tchar.h> #include <stdio.h> #include <strsafe.h>void _tmain (int   ARGC, tchar* argv[]) {win32_find_data FileData;   HANDLE Hsearch;   DWORD Dwattrs;       TCHAR Sznewpath[max_path];    BOOL ffinished = FALSE;      if (argc! = 2) {_tprintf (TEXT ("Usage:%s <dir>\n"), argv[0]);   Return     }//Create a new directory. if (!       CreateDirectory (argv[1], NULL)) {printf ("CreateDirectory failed (%d) \ n", GetLastError ());   Return     }//Start searching for text files in the current directory.    Hsearch = FindFirstFile (TEXT ("*.txt"), &filedata);       if (Hsearch = = Invalid_handle_value) {printf ("No text Files found.\n");   Return }//Copy each.     TXT file to the new directory//and change it to read only, if not already. while (!ffinished) {stringcchprintf (Sznewpath, sizeof (Sznewpath)/sizeof (sznewpath[0]), TEXT ("%s\\%s"), Argv[1], Filedata.cFileName);          if (CopyFile (Filedata.cfilename, Sznewpath, FALSE)) {dwattrs = GetFileAttributes (filedata.cfilename);          if (dwattrs==invalid_file_attributes) return; if (! ( Dwattrs & file_attribute_readonly) {setfileattributes (Sznewpath, Dwattrs |          FILE_ATTRIBUTE_READONLY);          }} else {printf ("Could not copy file.\n");      Return } if (! FindNextFile (Hsearch, &filedata)) {if (GetLastError () = = Error_no_more_files) {_t             printf (TEXT ("Copied *.txt to%s\n"), argv[1]);          ffinished = TRUE;             } else {printf ("Could not find Next file.\n");         Return     }}}//Close the search handle. FindClose (Hsearch);}

C + + copyfile,getfileattributes copying files and changing file properties

Related Article

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.