创建上月文件夹,并将上月文件移动到该文件假中

来源:互联网 发布:删除数据库的命令语句 编辑:程序博客网 时间:2024/06/05 18:51
#include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <sys/types.h>#include <time.h>#include <dirent.h>#include <string.h>#include <errno.h>/*Ver 0.32011-09-02msn: xorg@163.com*/void check_dir(char *);// 移动文件到指定的目录中void move_file(char *filename,char *backdir) {int ret;char cmd[128];// 调用check_dir函数,检测目录check_dir(backdir);// move commandsprintf(cmd,"mv %s %s >/dev/null 2>&1",filename,backdir);printf("cmd=%s\n",cmd);// execute commandret=system(cmd);if(ret==-1) {fprintf(stderr,"system() error.\n");exit(-1);}}//检测目录是否存在,不存在则创建void check_dir(char path[512]) {int n=0;int ret;DIR *dp;char *dir,full_dir[512];char dup_path[512];char mkdir_cmd[128];memset(full_dir,0,sizeof(full_dir));memset(dup_path,0,sizeof(dup_path));setenv("LANG","zh_CN.GBK",1);      printf("path=%s\n",path);// copy duplication to opreationstrcpy(dup_path,path);for(dir=strtok(dup_path,"/");dir;dir=strtok(NULL,"/")) {printf("for dir=%s\n",dir);n++;if(n==1) {strcat(full_dir,dir);} else {strcat(full_dir,"/");strcat(full_dir,dir);}printf("for full_dir=%s\n",full_dir);dp=opendir(full_dir);if(dp==NULL) {sprintf(mkdir_cmd,"mkdir %s",full_dir);//system(mkdir_cmd);ret=system(mkdir_cmd);if(ret==-1) {fprintf(stderr,"system() error.");close(dp);exit(-1);}// setenv设置环境变量后,用mkdir函数创建的目录还是乱码,暂时只能用mkdir命令实现/*ret=mkdir(full_dir,0755);if(ret!=0) {if(errno==ENOENT) {printf("pathname does not exist\n");close(dp);exit(-1);}else {fprintf(stderr,"mkdir() other error.\n");close(dp);exit(-1);}}*/close(dp);}elseclose(dp);}}int main(void) {time_t t;struct tm *p;char january[32];static int month[12]={12,1,2,3,4,5,6,7,8,9,10,11};char filename[32];char backdir[128];t=time(NULL);p=localtime(&t);// move filenamesprintf(filename,"%02d%s.txt",month[p->tm_mon],"*");printf("filename=%s\n",filename);// 当前月份sprintf(january,"%02d",1+p->tm_mon);// 1月份时移动12月份的,只有这个特殊if(!strcmp(january,"01")) {// move dirsprintf(backdir,"%d年巡检文件/%d月",1900+p->tm_year-1,month[p->tm_mon]);move_file(filename,backdir);} else {// move dirsprintf(backdir,"%d年巡检文件/%d月",1900+p->tm_year,month[p->tm_mon]);move_file(filename,backdir);}exit(0);} 
编译方法(支持中文) gcc -finput-charset=utf-8 -o move_file move_file.c
原创粉丝点击