Link to home
Start Free TrialLog in
Avatar of oostwijk
oostwijk

asked on

changing date of file

Hi there,
Can someone help me out ?
I want to make a script which modifies the date of a certain file on the server. Can someone tell me the command to modify the date ?
Avatar of maneshr
maneshr

use

$filename="/tmp/thisfile.htm";
`touch $filename`;
Avatar of oostwijk

ASKER

ok, that's on thing, but by using this the script doesn't know what date it should get..
ASKER CERTIFIED SOLUTION
Avatar of maneshr
maneshr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#include <time.h>
#include <sys/types.h>
#include <utime.h>
#include <errno.h>

int main(int argc, char **argv)
{
  int date  = 21;
  int month = 10;
  int year  = 2001;
  char *filename = "name_of_file";

  time_t t;
  struct tm mktimeb;
  struct utimbuf utimeb;

  if(argc == 2) filename = argv[1];

  mktimeb.tm_sec  = 0;
  mktimeb.tm_min  = 0;
  mktimeb.tm_hour = 0;
  mktimeb.tm_mday = date;
  mktimeb.tm_mon  = month - 1;
  mktimeb.tm_year = year - 1900;
  mktimeb.tm_isdst = -1;

  t = mktime(&mktimeb);

//  utimeb.actime  = t;   /* access time */
  utimeb.modtime = t;   /* modification time */

  if( utime(filename, &utimeb) )
    switch(errno)
    {
      case (EACCES):
        printf("Write to file \'%s\' is denied", filename);
      case (ENOENT):
        printf("File \'%s\' does not exist", filename);
      exit(1);
    }

  exit (0);
}
Well done, keep up the good work