?
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
?
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

765 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