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