编程技术、软件应用与系统模拟

(Programming, Applicaiton and Simulation)



本站目录

 

首页
ASP/Access/IIS
DELPHI/PASCAL
PASCAL高级编程
C语言编程实例
WORD
Excel
MATLAB
MINITAB讲座
Windows
DOS
SAS
生物系统模拟
土壤水分剖析器
其他



镜像站点

 

主站
北美镜象站
欧洲镜象站(1)
欧洲镜象站(2)

本站 Google

[搜索]  [站内导航]
座右铭:
只做有益人类的事
不做有害人类的事


修改文件建立时间的程序

在一个软件系统开发完成之后,许要将该系统所有的文件的建立时间改为同一个时间,以便于该系统的发布,
DOS操作系统没有提供这样的命令,PCTOOLS虽然提供了修改文件建立时间的功能,但它需要一个一个文件
地进行修改,这是件令人头疼的事。NORTON UTILITIES也有一个实用程序FD.EXE可以用来成批地修改文
件的建立时间,如果用户手上没有这样的软件,那么,可以利用作者提供的这个TURBO C程序TOUCH.C来完成同
样的功能。

一、程序设计的原理

TURBO C 2.0提供了findfirst()和findnext()的库函数,可以用来搜索当前目录下指定类型的文件,
getdate()和gettime()可以用来获得系统的当前时间,setftime()可以用来修改文件的建立时间。
程序设计思路是:
    第1步:用getdate()和gettime()获取系统的当前日期和当前时间;
    第2步:用当前日期和当前时间给文件时间变量myftime(为struct ftime类型)赋值;
    第3步:用findfirst()查找第一个有效的指定类型的文件;
    第4步:用open()打开它,并用setftime()修改文件的建立时间和日期,关闭文件;
    第5步:用findnext()查找下一个文件,重复第4~5步,直到没有有效的文件为止。

二、程序的使用方法

该程序采用命令行格式:
    touch <路径名>
其中,路径名可以含有统配符“*”和“?”,如:
    TOUCH C:\DONG\*.COM
可以将C:\DONG子目录下的所有命令文件(.COM)的建立日期和时间改为机器的当前日期和时间。

三、源程序清单

/********************************************************/
/*  程序名称: TOUCH.C 1.00                              */
/*  作    者: 董占山                                    */
/*  完成日期: 1995                                      */
/*  用    途: 修该文件的日期和时间                      */
/*  编译方法: 用下列命令编译连接可以得到TOUCH.COM:      */
/*  tcc -mt touch                                       */
/*  tlink c:\tc\lib\c0t+touch,touch,,c:\tc\lib\cs\lib /t*/
/********************************************************/

#include <stdio.h>
#include <dos.h>
#include <dir.h>
#include <io.h>
#include <fcntl.h>

/* 显示程序的使用方法 */
void help()
{
  printf("Usage  : change the time of files to current time\n");
  printf("Syntex : TOUCH <filename>\n");
}

/* 显示错误信息 */
void error()
{
  printf("Error : File not found !!!\n");
  help();
  exit(0);
}

/* 修改文件的日期和时间 */
void touch(file)
char *file;
{
  struct ffblk f;
  struct ftime myftime;
  struct date mydate;
  struct time mytime;
  int f1;
  int attrib;
  char p[MAXPATH],*p1;
  getdate(&mydate);
  gettime(&mytime);
  myftime.ft_tsec = mytime.ti_sec / 2;
  myftime.ft_min  = mytime.ti_min;
  myftime.ft_hour = mytime.ti_hour;
  myftime.ft_day  = mydate.da_day;
  myftime.ft_month= mydate.da_mon;
  myftime.ft_year = mydate.da_year - 1980;
  attrib = FA_ARCH | FA_RDONLY;
  strcpy(p,file);
  if ((p1 = strrchr(p,'\\'))==NULL) p1=p;
  if (findfirst(file,&f,attrib)!=0) error();
  do {
    if (p==p1) p1[0]=0; else p1[1]=0;
    f1 = open(strcat(p,f.ff_name),O_RDONLY);
    setftime(f1,&myftime);
    close(f1);
  } while (findnext(&f)!=-1);
}

/* 主程序 */
main(argc,argv)
int argc;
char *argv[];
{
  printf("TOUCH version 1.0 Copyright (c) 1995 Dong Zhanshan\n");
  switch (argc) {
    case 2 : touch(argv[1]);
	     break;
    default: help();
  }
}

© 1998-, 董占山, 版权所有。
转载文章请注明出处(www.sunfinedata.com/articles)。