renamewin.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #include "renamewin.h"
  2. #include "progresswin.h"
  3. #include <QFileDialog>
  4. #include <QRadioButton>
  5. #include <QMessageBox>
  6. #include <QInputDialog>
  7. //批量修改文件夹下文件名称的功能
  8. ReNameWin::ReNameWin(QWidget *parent)
  9. : QWidget(parent), m_extComBoxNum(0)
  10. {
  11. ui.setupUi(this);
  12. connect(ui.radioButtonAddPrefix, &QRadioButton::toggled, this, [this](bool status) {slot_renameOptionsChange(1,status); });
  13. connect(ui.radioButtonDelPrefix, &QRadioButton::toggled, this, [this](bool status) {slot_renameOptionsChange(2, status); });
  14. connect(ui.radioButtonAddSuffix, &QRadioButton::toggled, this, [this](bool status) {slot_renameOptionsChange(3, status); });
  15. connect(ui.radioButtonDelSuffix, &QRadioButton::toggled, this, [this](bool status) {slot_renameOptionsChange(4, status); });
  16. connect(ui.radioButtonLower, &QRadioButton::toggled, this, [this](bool status) {slot_renameOptionsChange(5, status); });
  17. connect(ui.radioButtonUpper, &QRadioButton::toggled, this, [this](bool status) {slot_renameOptionsChange(6, status); });
  18. }
  19. ReNameWin::~ReNameWin()
  20. {
  21. }
  22. void ReNameWin::slot_selectDir()
  23. {
  24. QString workDir = ui.lineEditDir->text();
  25. QString rootpath = QFileDialog::getExistingDirectory(this, tr("Open Directory"), workDir, QFileDialog::DontResolveSymlinks);
  26. if (!rootpath.isEmpty())
  27. {
  28. ui.lineEditDir->setText(rootpath);
  29. }
  30. }
  31. //改名,前缀后缀名称的修改
  32. void ReNameWin::slot_renameOptionsChange(int id, bool status)
  33. {
  34. switch (id)
  35. {
  36. case 1:
  37. ui.lineEditAddPrefix->setEnabled(status);
  38. break;
  39. case 2:
  40. ui.lineEditDelPrefix->setEnabled(status);
  41. break;
  42. case 3:
  43. ui.lineEditAddSuffix->setEnabled(status);
  44. break;
  45. case 4:
  46. ui.lineEditDelSuffix->setEnabled(status);
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. void ReNameWin::slot_userDefineExt()
  53. {
  54. bool ok = false;
  55. QString text = QInputDialog::getText(this, tr("input file ext()"), tr("ext (Start With .)"), QLineEdit::Normal, QString(".cpp"), &ok);
  56. if (ok && !text.isEmpty())
  57. {
  58. text = text.trimmed();
  59. ui.comboBoxExt->addItem(text);
  60. ++m_extComBoxNum;
  61. ui.comboBoxExt->setCurrentIndex(m_extComBoxNum);
  62. }
  63. }
  64. void ReNameWin::slot_startRename()
  65. {
  66. if (ui.optionsTab->currentIndex() == 0)
  67. {
  68. changeFileName();
  69. }
  70. else if (ui.optionsTab->currentIndex() == 1)
  71. {
  72. changeFileExt();
  73. }
  74. }
  75. void ReNameWin::changeFileName()
  76. {
  77. QString dealDir = ui.lineEditDir->text();
  78. if (dealDir.isEmpty())
  79. {
  80. QMessageBox::warning(this, tr("Notice"), tr("Please Select Dir"));
  81. return;
  82. }
  83. int type = -1;
  84. QString extrenFileName;
  85. bool addPrefix = ui.radioButtonAddPrefix->isChecked();
  86. if (addPrefix && ui.lineEditAddPrefix->text().isEmpty())
  87. {
  88. QMessageBox::warning(this, tr("Notice"), tr("Please Input Add File Prefix"));
  89. return;
  90. }
  91. if (addPrefix)
  92. {
  93. type = 0;
  94. extrenFileName = ui.lineEditAddPrefix->text();
  95. }
  96. bool delPrefix = ui.radioButtonDelPrefix->isChecked();
  97. if (delPrefix && ui.lineEditDelPrefix->text().isEmpty())
  98. {
  99. QMessageBox::warning(this, tr("Notice"), tr("Please Input Del File Prefix"));
  100. return;
  101. }
  102. if (delPrefix)
  103. {
  104. type = 1;
  105. extrenFileName = ui.lineEditDelPrefix->text();
  106. }
  107. bool addSuffix = ui.radioButtonAddSuffix->isChecked();
  108. if (addSuffix && ui.lineEditAddSuffix->text().isEmpty())
  109. {
  110. QMessageBox::warning(this, tr("Notice"), tr("Please Input Add File Suffix"));
  111. return;
  112. }
  113. if (addSuffix)
  114. {
  115. type = 2;
  116. extrenFileName = ui.lineEditAddSuffix->text();
  117. }
  118. bool delSuffix = ui.radioButtonDelSuffix->isChecked();
  119. if (delSuffix && ui.lineEditDelSuffix->text().isEmpty())
  120. {
  121. QMessageBox::warning(this, tr("Notice"), tr("Please Input Del File Suffix"));
  122. return;
  123. }
  124. if (delSuffix)
  125. {
  126. type = 3;
  127. extrenFileName = ui.lineEditDelSuffix->text();
  128. }
  129. bool toLowerFileName = ui.radioButtonLower->isChecked();
  130. if (toLowerFileName)
  131. {
  132. type = 4;
  133. }
  134. bool toUpperFileName = ui.radioButtonUpper->isChecked();
  135. if (toUpperFileName)
  136. {
  137. type = 5;
  138. }
  139. if (type == -1)
  140. {
  141. QMessageBox::warning(this, tr("Notice"), tr("Please Select One Operator"));
  142. return;
  143. }
  144. QList<QString> dirsList;
  145. dirsList.append(dealDir);
  146. int fileNums = 0;
  147. int failNums = 0;
  148. bool dealChildDir = ui.checkBoxDealChildDir->isChecked();
  149. QString oldName;
  150. QString newName;
  151. auto getNewName = [](QString oldName, int type, const QString& addOrDelFix)->QString {
  152. QFileInfo fi(oldName);
  153. QDir dir = fi.absoluteDir();
  154. switch (type)
  155. {
  156. //增加前缀
  157. case 0:
  158. return QString("%1/%2.%3").arg(dir.absolutePath()).arg(addOrDelFix+fi.baseName()).arg(fi.suffix());
  159. case 1:
  160. {//删除前缀
  161. if (fi.baseName().startsWith(addOrDelFix) && (fi.baseName() != addOrDelFix))
  162. {
  163. return QString("%1/%2.%3").arg(dir.absolutePath()).arg(fi.baseName().mid(addOrDelFix.length())).arg(fi.suffix());
  164. }
  165. }
  166. break;
  167. case 2:
  168. {
  169. //增加后缀
  170. return QString("%1/%2.%3").arg(dir.absolutePath()).arg(fi.baseName()+ addOrDelFix).arg(fi.suffix());
  171. }
  172. break;
  173. case 3:
  174. //删除后缀
  175. {
  176. if (fi.baseName().endsWith(addOrDelFix) && (fi.baseName() != addOrDelFix))
  177. {
  178. return QString("%1/%2.%3").arg(dir.absolutePath()).arg(fi.baseName().mid(0, fi.baseName().length() - addOrDelFix.length())).arg(fi.suffix());
  179. }
  180. }
  181. break;
  182. case 4:
  183. return QString("%1/%2.%3").arg(dir.absolutePath()).arg(fi.baseName().toLower()).arg(fi.suffix());
  184. case 5:
  185. return QString("%1/%2.%3").arg(dir.absolutePath()).arg(fi.baseName().toUpper()).arg(fi.suffix());
  186. default:
  187. break;
  188. }
  189. return QString();
  190. };
  191. ProgressWin* m_loadFileProcessWin = new ProgressWin(this);
  192. m_loadFileProcessWin->setWindowModality(Qt::WindowModal);
  193. m_loadFileProcessWin->info(tr("rename file in progress, please wait ..."));
  194. m_loadFileProcessWin->show();
  195. int processTotal = 0;
  196. bool isExistChildDir = false;
  197. while (!dirsList.isEmpty())
  198. {
  199. QString path = dirsList.takeFirst();
  200. /*添加path路径文件*/
  201. QDir dir(path);
  202. //遍历各级子目录
  203. if (dealChildDir)
  204. {
  205. QFileInfoList folder_list = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::NoSymLinks); //获取当前所有目录
  206. for (int i = 0; i != folder_list.size(); ++i) //自动递归添加各目录到上一级目录
  207. {
  208. QString namepath = folder_list.at(i).absoluteFilePath(); //获取路径
  209. //必须放前面,因为以文件夹个数处理为进度条计数
  210. dirsList.push_front(namepath);
  211. }
  212. if ((processTotal == 0) && dirsList.size() > 0)
  213. {
  214. //以目录个数大概去统计进度
  215. processTotal = dirsList.size();
  216. m_loadFileProcessWin->setTotalSteps(processTotal);
  217. isExistChildDir = true;
  218. }
  219. if (dirsList.size() < processTotal)
  220. {
  221. m_loadFileProcessWin->moveStep();
  222. }
  223. }
  224. QDir dir_file(path);
  225. dir_file.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);//获取当前所有文件
  226. QFileInfoList list_file = dir_file.entryInfoList();
  227. //说明没有子文件夹
  228. if ((processTotal == 0) && list_file.size() > 0)
  229. {
  230. processTotal = list_file.size();
  231. m_loadFileProcessWin->setTotalSteps(processTotal);
  232. }
  233. for (int i = 0; i < list_file.size(); ++i)
  234. {
  235. QFileInfo fileInfo = list_file.at(i);
  236. oldName = fileInfo.absoluteFilePath();
  237. newName = getNewName(oldName,type, extrenFileName);
  238. if (!newName.isEmpty() && (newName != oldName))
  239. {
  240. if (!QFile::rename(oldName, newName))
  241. {
  242. failNums++;
  243. m_loadFileProcessWin->info(tr("failed %1 file path %2, please check").arg(failNums).arg(oldName));
  244. }
  245. fileNums++;
  246. }
  247. //没有子文件夹时,按文件数量走
  248. if (!isExistChildDir)
  249. {
  250. m_loadFileProcessWin->moveStep();
  251. }
  252. }
  253. }
  254. QMessageBox::information(this, tr("Notice"), tr("Deal Finished, totol %1 files, failed %2 files").arg(fileNums).arg(failNums));
  255. delete m_loadFileProcessWin;
  256. }
  257. void ReNameWin::changeFileExt()
  258. {
  259. QString dealDir = ui.lineEditDir->text();
  260. QString destExt = ui.lineEditDestExt->text();
  261. if (destExt.startsWith('.'))
  262. {
  263. destExt = destExt.mid(1, destExt.length() - 1);
  264. }
  265. if (dealDir.isEmpty() || destExt.isEmpty())
  266. {
  267. QMessageBox::warning(this, tr("Notice"), tr("Please Select Dir Or Dest Ext"));
  268. return;
  269. }
  270. QString filterExt;
  271. bool isNeedFilterExt = false;
  272. if (0 != ui.comboBoxExt->currentIndex())
  273. {
  274. filterExt = ui.comboBoxExt->currentText();
  275. if (filterExt.startsWith('.'))
  276. {
  277. filterExt = filterExt.mid(1, filterExt.length() - 1);
  278. }
  279. }
  280. //检查是否需要过滤只处理ext类型
  281. if (!filterExt.isEmpty())
  282. {
  283. isNeedFilterExt = true;
  284. }
  285. QList<QString> dirsList;
  286. dirsList.append(dealDir);
  287. int fileNums = 0;
  288. int failNums = 0;
  289. bool dealChildDir = ui.checkBoxDealChildDir->isChecked();
  290. QString oldName;
  291. QString newName;
  292. auto getNewName = [](QString oldName, const QString& destExt)->QString{
  293. QFileInfo fi(oldName);
  294. //没有后缀名,则不修改
  295. if (fi.suffix().isEmpty())
  296. {
  297. return QString();
  298. }
  299. int oldExtSize = fi.suffix().length();
  300. return QString("%1%2").arg(oldName.mid(0, oldName.size()-oldExtSize)).arg(destExt);
  301. };
  302. ProgressWin* m_loadFileProcessWin = new ProgressWin(this);
  303. m_loadFileProcessWin->setWindowModality(Qt::WindowModal);
  304. m_loadFileProcessWin->info(tr("rename file in progress, please wait ..."));
  305. m_loadFileProcessWin->show();
  306. int processTotal = 0;
  307. bool isExistChildDir = false;
  308. while (!dirsList.isEmpty())
  309. {
  310. QString path = dirsList.takeFirst();
  311. /*添加path路径文件*/
  312. QDir dir(path);
  313. //遍历各级子目录
  314. if (dealChildDir)
  315. {
  316. QFileInfoList folder_list = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::NoSymLinks); //获取当前所有目录
  317. for (int i = 0; i != folder_list.size(); ++i) //自动递归添加各目录到上一级目录
  318. {
  319. QString namepath = folder_list.at(i).absoluteFilePath(); //获取路径
  320. //必须放前面,因为以文件夹个数处理为进度条计数
  321. dirsList.push_front(namepath);
  322. }
  323. if ((processTotal == 0) && dirsList.size() > 0)
  324. {
  325. //以目录个数大概去统计进度
  326. processTotal = dirsList.size();
  327. m_loadFileProcessWin->setTotalSteps(processTotal);
  328. isExistChildDir = true;
  329. }
  330. if (dirsList.size() < processTotal)
  331. {
  332. m_loadFileProcessWin->moveStep();
  333. }
  334. }
  335. QDir dir_file(path);
  336. dir_file.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);//获取当前所有文件
  337. QFileInfoList list_file = dir_file.entryInfoList();
  338. //说明没有子文件夹
  339. if ((processTotal == 0) && list_file.size() > 0)
  340. {
  341. processTotal = list_file.size();
  342. m_loadFileProcessWin->setTotalSteps(processTotal);
  343. }
  344. for (int i = 0; i < list_file.size(); ++i)
  345. {
  346. QFileInfo fileInfo = list_file.at(i);
  347. oldName = fileInfo.absoluteFilePath();
  348. if (isNeedFilterExt)
  349. {
  350. if (fileInfo.suffix() != filterExt)
  351. {
  352. continue;
  353. }
  354. }
  355. newName = getNewName(oldName, destExt);
  356. if (!newName.isEmpty() && (newName != oldName))
  357. {
  358. if (!QFile::rename(oldName, newName))
  359. {
  360. failNums++;
  361. m_loadFileProcessWin->info(tr("failed %1 file path %2, please check").arg(failNums).arg(oldName));
  362. }
  363. }
  364. //没有子文件夹时,按文件数量走
  365. if (!isExistChildDir)
  366. {
  367. m_loadFileProcessWin->moveStep();
  368. }
  369. }
  370. fileNums += list_file.size();
  371. }
  372. QMessageBox::information(this, tr("Notice"), tr("Deal Finished, totol %1 files, failed %2 files").arg(fileNums).arg(failNums));
  373. delete m_loadFileProcessWin;
  374. }