zoukankan      html  css  js  c++  java
  • 简单的文件清理,继续学习模块划分

    简单的文件清理,继续学习模块划分

    1. 项目介绍


    1. 项目功能

    ​ 通过生成exe文件,该程序可以清理所在目录及子目录中特定格式文件。

    2. 项目思路

    递归 / 队列 篇幅有限,这里就简单讲讲递归方法

    do
    {
    	if(是文件&&是特定格式文件)
    	{
    		删除;
    	}
    	else if(是文件夹)
    	{
    		递归;
    	}
    	删除文件夹;
    }while(查找不到文件)
    
    3.所需函数
    • ​ 关键

      struct _finddata_t fd;  //用于保存文件信息的结构体
      _getcwd(); 				//获取执行exe文件的所在目录
      strstr();   				// 用于检测文件格式
      
      _findfirst()   /   _findnext()   /   _findclose()  //搜索 文件与文件夹
      remove()    /    rmdir()     //删除  文件/文件夹	
      
    • 非必须

      //设置控制台输出颜色
      	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);	
      	SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
      //更快处理字符串
      	sprintf();
      

    2.具体实施

    ​ 项目分为三个模块,客户端

    client.c 客户交互

    `clean.c`		**核心模块**
    
    `print.c`		输出到窗口显示删除状态
    
    `clean.h`		头文件
    
    • clean.c

      #include <io.h>
      #include <string.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <stdbool.h>
      #include <direct.h>
      #include "mydatastruct.h"
      char  g_dir[SIZE];		//清理模块自身数据   , 路径
      char  g_form[SIZE];			//清理模块自身数据  ,	文件格式
      bool shutdown = false;		//是否关机
      bool rancolor = false;			//随机颜色
      
      
      void save(char *dir, char * form,bool color)//保存由客户端传来的数据
      {
      	strcpy_s(g_dir,SIZE,dir);
      	strcpy_s(g_form,SIZE, form);
      	rancolor = color;
      }
      
      bool is_form(const char *name)//  检测是否为要清理的文件
      {
      	if( !name )return 0;
      	while( name&&*name != '.' )
      		++name;
      	return strstr(g_form, name);
      }
      
      
      void delete_file1(char *dir, char * form,bool rancolor)
      {
      	long hand;
      	struct _finddata_t file;
      
      	char info[SIZE];
      	sprintf_s(info, SIZE, "%s\*.*", dir);
      
      	hand = _findfirst(info, &file);//失败返回-1
      	if( hand == -1 )return;
      	_findnext(hand, &file);
      
      	while( _findnext(hand, &file) == 0 )//先删文件
      	{
      		if( file.attrib != _A_SUBDIR )
      		{
      			char temp[SIZE];
      
      			sprintf_s(temp, SIZE,"%s\%s" ,dir, file.name);
      			if( is_form(file.name) )
      			{
      				remove(temp);
      				print(temp, true,rancolor);
      			}
      			else print(temp, false,rancolor);
      		}
      	}
      	_findclose(hand);
      
      
      	hand = _findfirst(info, &file);//失败返回-1
      	if( hand == -1 )return;
      	_findnext(hand, &file);
      
      	while( _findnext(hand, &file) == 0 )//再文件夹
      	{
      		if( file.attrib == _A_SUBDIR )//转换句柄,进入子文件夹
      		{
      			char temp[SIZE];
      
      			sprintf_s(temp, SIZE, "%s\%s", dir, file.name);
      			delete_file1(temp, form,rancolor);
      		}
      	}
      	_findclose(hand);
      
      
      	if( !_rmdir(dir) )
      		print(dir, true,rancolor);
      	else {
      		print(dir, false,rancolor);
      		//printf("%d
      ", errno);
      	}
      }
      
      void delete_file(char *dir, char * form,int color)
      {
      
      	save(dir, form,color);
      
      	delete_file1(g_dir, g_form,rancolor);
      
      }
      
    • client.c

      #include <stdio.h>
      #include "clean.h"
      int main( )
       {
      	char form[SIZE] = ".txt/.bmp";	
      	char dir[SIZE];
      	_getcwd(dir, SIZE);
      
      
      	delete_file(dir, form,1);
      	getchar( );
      }
      
    • print.c

      #include <stdio.h>
      #include <Windows.h>
      void print(const char *str, int flag,int rancolor)
      {
      	int random;
      	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
      	if( !rancolor )
      	{
      		SetConsoleTextAttribute(hConsole, 
      			flag ? FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE 
      			: FOREGROUND_RED | FOREGROUND_INTENSITY);
      	}
      	else
      	{
      	//	srand(time(NULL));
      		random = rand()%16+1;
      		SetConsoleTextAttribute(hConsole, random);
      	}
      	if( flag )
      	{
      		printf("[OK] %s
      ", str);
      		printf("%d
      ", random);
      	}
      	else
      	{
      		
      		printf("[ER] %s
      ", str);
      		printf("%d
      ", random);
      	}
      	SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
      }
      //void print_color(void)
      //{
      //	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
      //	for( int i = 0; i < 33; ++i )
      //	{
      //		SetConsoleTextAttribute(hConsole, i);
      //		printf("%d:		coooooooooooooolor	
      ", i);
      //	}
      //
      //}
      
    • clean.h

      #define SIZE 256
      void delete_file(char *dir, char * form,int rancolor);
      
  • 相关阅读:
    leetcode41
    leetcode32
    leetcode312
    leetcode10
    leetcode85
    leetcode124
    leetcode301
    leetcode84
    一文读懂机器学习大杀器XGBoost原理
    【干货】机器学习中的五种回归模型及其优缺点
  • 原文地址:https://www.cnblogs.com/starrys/p/11297746.html
Copyright © 2011-2022 走看看