Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how do you change "file attributes" and "file last modficiation date" of a file in Visual C++ 6.0

Posted on 2003-03-09
4
Medium Priority
?
300 Views
Last Modified: 2010-04-01
i want to do exactly what attrib.exe does and change the file date and time of files too...

i am making an archiver and i'm going to use it as part of my file de-archiving, so that they'll be restored to their original state...
0
Comment
Question by:icedeocampo
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 8098303
You can use the API function SetFileAttributes
0
 
LVL 30

Expert Comment

by:Axter
ID: 8098314
Example:
const char *FileName = "C:\\SomeDirectory\\File.Gid";
SetFileAttributes(FileName,
FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY);
0
 
LVL 1

Accepted Solution

by:
yukapapa earned 400 total points
ID: 8107356
Hi, icedeocampo.

You can use SetFileAttributes() to set the attributes and SetFileTime() to set the time informations of existing files. You can set the attributes for a file on creation by CreateFile() function. The CreateFile() returns a handle for the file and the CloseHandle() closes the handle.

To get the attributes and time informations, you can use GetFileAttributes() and GetFileTime() or GetFileInformationByHandle().

The Set/GetFileAttributes take a file name as their argument, but Set/GetFileTime and GetFileInformationByHandle use a handle for the file.

Sample codes here:

//--------------------------------------------------
//Archiving

const _TCHAR* const file_name = "some file";

//Open the file.
HANDLE h_file = CreateFile(
  file_name,
  GENERIC_READ,          //read access is required
  FILE_SHARE_READ,       //allow other to read this file
  NULL,                  //use default security
  OPEN_EXISTING,         //open an existing file
  FILE_ATTRIBUTE_NORMAL, //open a file without specific attributes
  0);                    //not use template file

if(h_file == INVALID_HANDLE_VALUE/*not NULL*/)
  /*Error*/;

//Get the file informations
BY_HANDLE_FILE_INFORMATION file_info;
if(!GetFileInformationByHandle(h_file, &file_info))
  /*Error*/;
//file_info.dwFileAttributes = bit masks for the attributes
//file_info.ftCreationTime = creation time
//file_info.ftLastAccessTime = last access time
//file_info.ftLastWriteTime = last write time

//Read operation on the file can be done by ReadFile() function.
DWORD read;
if(!ReadFile(
  h_file,
  /*pointer to the buffer for reading*/,
  /*buffer size*/,
  &read,  //receives number of bytes read
  NULL))  //not use overlapped operation
  /*Error*/;

//You need to write the buffer to your archive file.
//The WriteFile() function does it.
//(see de-archiving code below)

//Close the handle.
CloseHandle(h_file);

//----------------------------------------------------
//De-archiving

const _TCHAR* const file_name = "saved file name";
DWORD file_attributes; //saved attributes
FILETIME create, access, modify; //saved time informations

//Create new file with original attributes.
HANDLE h_file = CreateFile(
  file_name,
  GENERIC_WRITE,   //write access is required
  0,               //prohibit other from accessing this file
  NULL,            //use default security
  CREATE_NEW,      //create new file
  file_attributes, //saved attributes
  NULL);           //not use template file

if(h_file == INVALID_HANDLE_VALUE/*not NULL*/)
  /*Error*/;

//You need to read the original content of the file.
//ReadFile() function does it.
//(see archiving code above)

//Write back the content of the file by WriteFile().
DWORD written;
if(!WriteFile(
  h_file,
  /*pointer to the content of the file*/,
  /*size of the content*/,
  &written,  //receives number of bytes written
  NULL))     //not use overlapped operation
  /*Error*/;

//Set the time informations.
if(!SetFileTime(&create, &access, &modify))
  /*Error*/;

//Close the handle.
CloseHandle(h_file);

Good, luck!
0
 

Author Comment

by:icedeocampo
ID: 8108714
ok thanks!

i'm working on it now =)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question