打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C++实现目录下所有文件添加后缀名
  1. #include<iostream>  
  2. #include<stdio.h>  
  3. #include <sys/types.h>  
  4. #include <dirent.h>  
  5. #include <cstring>  
  6. /********************************************************************** 
  7.      created:    2016/01/20 
  8.      created:    20:1:2016 21:04 
  9.      filename:   rename_file.cpp 
  10.      file base:  main 
  11.      file ext:   cpp 
  12.      author:     qianghaohao 
  13.  
  14.      pupose:     The function of this program is to give all 
  15.                  the files under the directory specified by  
  16.                  the user to add the suffix, except catalog file 
  17.      usage:      command dir_name suff_name 
  18. **********************************************************************/  
  19. int main(int argc, char **argv)  
  20. {  
  21.     DIR *dir;  
  22.     struct dirent *ptr;  
  23.     if (argc < 3)  
  24.     {  
  25.         std::cout << "Usage:" << argv[0] << " dir_name suff_name" << std::endl;  
  26.          return -1;  
  27.     }   
  28.     std::cout << "directory:" << argv[1] << std::endl;  //print current dir_name  
  29.     if ((dir = opendir(argv[1])) == NULL)    //open directory  
  30.     {  
  31.         std::cout << "open directory failed..." << std::endl;  
  32.         return -2;  
  33.     }  
  34.     std::string new_name;   
  35.     std::string old_name;  
  36.     while ((ptr = readdir(dir)) != NULL)      
  37.     {  
  38.         if (ptr->d_type != DT_DIR)  //if file is not directory   
  39.         {  
  40.             old_name = argv[1];  
  41.             old_name += "/";  
  42.             old_name += ptr->d_name;  //old name  
  43.   
  44.             new_name = argv[1];  
  45.             new_name += "/";  
  46.             new_name += ptr->d_name;   
  47.             std::cout << new_name << " to ";  
  48.             new_name += argv[2];  //new name include suffix  
  49.             std::cout << new_name;   
  50.             if (rename(old_name.c_str(), new_name.c_str()) == 0)  //rename file not directory  
  51.             {  
  52.                std::cout << " successed..." << std::endl;  
  53.             }  
  54.             else  
  55.             {  
  56.               std::cout << " failed..." << std::endl;  
  57.             }  
  58.         }  
  59.     }  
  60.     closedir(dir);  //close directory  
  61.     std::cout << "     complete!" << std::endl;  
  62.     return 0;  
  63. }  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
用C++ boost asio库写的一个简单command-line扫描器
类内部存储的东西:太简洁了----小话c++(5)
Qt DLL总结【二】-创建及调用QT的 DLL - 柳北风儿~~~~~~~欲宇仙炅 - ITeye技术网站
谈谈C 新标准带来的属性(Attribute)
OpenWRT开发之——对C++11的支持
C++之构造函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服