CreateFolderAndFileCommand.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. namespace App\Console\Commands\Export;
  3. use Illuminate\Console\Command;
  4. class CreateFolderAndFileCommand extends Command
  5. {
  6. /**
  7. * The name and signature of the console command.
  8. *
  9. * @var string
  10. */
  11. protected $signature = 'create:folder_file';
  12. /**
  13. * The console command description.
  14. *
  15. * @var string
  16. */
  17. protected $description = '在指定目录下创建文件夹和文件';
  18. /**
  19. * Create a new command instance.
  20. *
  21. * @return void
  22. */
  23. public function __construct()
  24. {
  25. parent::__construct();
  26. }
  27. /**
  28. * Execute the console command.
  29. *
  30. * @return mixed
  31. */
  32. public function handle()
  33. {
  34. $directory = "E:\\美团数据抓取\\三九胃泰颗粒"; // 指定目录路径
  35. $this->createFileEx($directory);
  36. //$directory = "E:\\鸭题库数据初始化历年真题"; // 指定目录路径
  37. //$this->createFile($directory);
  38. //$this->deleteTxtFilesRecursively($directory);
  39. $directory = "E:\\鸭题库数据初始化考点_第二次\\中药二"; // 指定目录路径
  40. //$this->clearTxtFilesContent($directory);
  41. // $this->deleteTxtFilesRecursively($directory);
  42. $baseDirectory = "E:\\鸭题库数据\\中药二";
  43. $firstFolderName = [
  44. '第一节 中药药物警戒',
  45. '第二节 中药不良反应',
  46. '第三节 中药用药错误',
  47. '第四节 医疗用毒性中药的中毒反应和基本救治原则',
  48. ];
  49. //$this->createFirstFolder($firstFolderName, $baseDirectory);
  50. $firstAndSecondFolderNames = [
  51. [
  52. 'firstName' => '历年真题',
  53. 'secondNames' => [
  54. '2015',
  55. '2016',
  56. '2017',
  57. '2018',
  58. '2019',
  59. '2020',
  60. '2021',
  61. '2022-1',
  62. '2022-2',
  63. '2023',
  64. '2024',
  65. ]
  66. ],
  67. // [
  68. // 'firstName' => '第二章 处方审核与调剂',
  69. // 'secondNames' => []
  70. // ],
  71. // [
  72. // 'firstName' => '第三章 用药咨询与药物治疗管理',
  73. // 'secondNames' => [
  74. // '第一节 药学信息咨询服务',
  75. // '第二节 疾病管理与健康宣教',
  76. // '第三节 药物治疗管理',
  77. // '第四节 常用医学检查',
  78. // ]
  79. // ],
  80. // [
  81. // 'firstName' => '第四章 用药安全',
  82. // 'secondNames' => [
  83. // '第一节 药物警戒',
  84. // '第二节 药品不良反应',
  85. // '第三节 药源性疾病',
  86. // '第四节 用药错误',
  87. // '第五节 特殊人群用药'
  88. // ]
  89. // ],
  90. // [
  91. // 'firstName' => '第五章 急救、中毒解救及职业防护',
  92. // 'secondNames' => [
  93. // '第一节 急救的意义与原则',
  94. // '第二节 常见急症及处置',
  95. // '第三节 中毒解救',
  96. // '第四节 细胞毒性药物的职业防护'
  97. // ]
  98. // ],
  99. // [
  100. // 'firstName' => '第六章 常见病症的健康管理',
  101. // 'secondNames' => [
  102. // '第一节 发热',
  103. // '第二节 疼痛',
  104. // '第三节 咳嗽',
  105. // '第四节 感冒',
  106. // '第五节 急性扁桃体炎',
  107. // '第六节 鼻窦炎',
  108. // '第七节 过敏性鼻炎',
  109. // '第八节 急性气管-支气管炎',
  110. // '第九节 社区获得性肺炎',
  111. // '第十节 口腔溃疡',
  112. // '第十一节 消化不良',
  113. // '第十二节 胆石症与胆囊炎',
  114. // '第十三节 便秘',
  115. // '第十四节 腹泻',
  116. // '第十五节 痔疮',
  117. // '第十六节 阴道炎',
  118. // '第十七节 乳腺炎',
  119. // '第十八节 尿路感染',
  120. // '第十九节 男性性功能障碍',
  121. // '第二十节 尿失禁',
  122. // '第二十一节 绝经综合征',
  123. // '第二十二节 多囊卵巢综合征',
  124. // '第二十三节 避孕',
  125. // '第二十四节 昆虫叮咬',
  126. // '第二十五节 烫伤',
  127. // '第二十六节 带状疱疹',
  128. // '第二十七节 单纯疱疹',
  129. // '第二十八节 冻伤 ( 疮 )',
  130. // '第二十九节 白癜风',
  131. // '第三十节 银屑病',
  132. // '第三十一节 痤疮',
  133. // '第三十二节 荨麻疹',
  134. // '第三十三节 湿疹',
  135. // '第三十四节 甲沟炎',
  136. // '第三十五节 特应性皮炎',
  137. // '第三十六节 真菌感染性皮肤病',
  138. // '第三十七节 雄激素性脱发',
  139. // '第三十八节 疖痈',
  140. // '第三十九节 视疲劳',
  141. // '第四十节 急性结膜炎',
  142. // '第四十一节 干眼症',
  143. // '第四十二节 沙眼',
  144. // '第四十三节 麦粒肿',
  145. // '第四十四节 贫血',
  146. // '第四十五节 超重和肥胖',
  147. // '第四十六节 晕动病'
  148. // ]
  149. // ],
  150. // [
  151. // 'firstName' => '第七章 慢性病管理',
  152. // 'secondNames' => [
  153. // ]
  154. // ],
  155. // [
  156. // 'firstName' => '第八章 呼吸系统常见疾病',
  157. // 'secondNames' => [
  158. // '第一节 哮喘',
  159. // '第二节 慢性阻塞性肺疾病'
  160. // ]
  161. // ],
  162. // [
  163. // 'firstName' => '第九章 心血管系统常见疾病',
  164. // 'secondNames' => [
  165. // '第一节 高血压',
  166. // '第二节 血脂异常',
  167. // '第三节 冠状动脉粥样硬化',
  168. // '第四节 心房颤动'
  169. // ]
  170. // ],
  171. // [
  172. // 'firstName' => '第十章 神经精神系统常见疾病',
  173. // 'secondNames' => [
  174. // '第一节 焦虑抑郁',
  175. // '第二节 睡眠障碍',
  176. // '第三节 脑卒中',
  177. // '第四节 帕金森病',
  178. // '第五节 癫痫',
  179. // '第六节 痴呆'
  180. // ]
  181. // ],
  182. // [
  183. // 'firstName' => '第十一章 消化系统常见疾病',
  184. // 'secondNames' => [
  185. // '第一节 胃食管反流病',
  186. // '第二节 消化性溃疡',
  187. // '第三节 溃疡性结肠炎',
  188. // '第四节 慢性病毒性肝炎'
  189. // ]
  190. // ],
  191. // [
  192. // 'firstName' => '第十二章 内分泌系统常见疾病',
  193. // 'secondNames' => [
  194. // '第一节 甲状腺功能亢进症',
  195. // '第二节 甲状腺功能减退症',
  196. // '第三节 糖尿病',
  197. // '第四节 骨质疏松症',
  198. // '第五节 高尿酸血症与痛风'
  199. // ]
  200. // ],
  201. // [
  202. // 'firstName' => '第十三章 免疫系统常见疾病',
  203. // 'secondNames' => [
  204. // '第一节 类风湿关节炎',
  205. // '第二节 系统性红斑狼疮'
  206. // ]
  207. // ],
  208. // [
  209. // 'firstName' => '第十四章 泌尿系统常见疾病',
  210. // 'secondNames' => [
  211. // '第一节 良性前列腺增生症',
  212. // '第二节 慢性肾脏病'
  213. // ]
  214. // ],
  215. // [
  216. // 'firstName' => '第十五章 肿瘤',
  217. // 'secondNames' => [
  218. // '第一节 肿瘤的临床基础',
  219. // '第二节 肿瘤的治疗与预防'
  220. // ]
  221. // ],
  222. ];
  223. //$this->createFirstAndSecondFolder($firstAndSecondFolderNames, $baseDirectory);
  224. $this->info('success!');
  225. }
  226. public function createFile($directory)
  227. {
  228. $files = scandir($directory);
  229. $folders = [];
  230. foreach ($files as $key=> $file) {
  231. if ($file != "." && $file != "..") { // 排除当前目录"."和上级目录".."
  232. $fullPath = $directory . '/' . $file;
  233. if (is_dir($fullPath)) {
  234. $folders[] = $file;
  235. $tempFilePath = $directory . '\\' . $file . '\\' . $file . '.txt';
  236. //dd($tempFilePath);
  237. //在目录下创建文件
  238. $tempFile = fopen($tempFilePath, "w");
  239. fclose($tempFile);
  240. }
  241. }
  242. }
  243. dd($folders);
  244. }
  245. public function createFileEx($baseDirectory)
  246. {
  247. if (!is_dir($baseDirectory)) {
  248. mkdir($baseDirectory, 0777, true); // 创建文件夹,0777 是权限设置,true 表示递归创建
  249. }
  250. for ($i = 11; $i <= 100; $i++) {
  251. $fileName = $baseDirectory . '/' . 'file_' . $i . '.txt'; // 文件名格式为 file_1.txt, file_2.txt, ...
  252. $file = fopen($fileName, 'w'); // 打开文件用于写入,如果文件不存在会自动创建
  253. if ($file) {
  254. fwrite($file, ''); // 写入内容,这里写入空内容
  255. fclose($file); // 关闭文件
  256. echo "文件 $fileName 创建成功!<br>";
  257. } else {
  258. echo "文件 $fileName 创建失败!<br>";
  259. }
  260. }
  261. }
  262. public function createFirstFolder($fileNames, $baseDirectory) {
  263. foreach ($fileNames as $key => $fileName) {
  264. $realKey = $key + 1;
  265. $dirPath = $baseDirectory . '\\' . $realKey . $fileName;
  266. if (!is_dir($dirPath)) {
  267. mkdir($dirPath, 0x777, true);
  268. }
  269. //再创建txt文件
  270. $filePath = $dirPath . '\\' . $realKey . $fileName . '.txt';
  271. $fd = fopen($filePath, 'w+');
  272. fclose($fd);
  273. }
  274. }
  275. public function createFirstAndSecondFolder($firstAndSecondFolderNames, $baseDirectory)
  276. {
  277. foreach ($firstAndSecondFolderNames as $key => $firstAndSecondFolderName) {
  278. $firstKey = $key + 1;
  279. $firstDirPath = $baseDirectory . '\\' . $firstKey . $firstAndSecondFolderName['firstName'];
  280. if (!is_dir($firstDirPath)) {
  281. mkdir($firstDirPath, 0x777, true);
  282. }
  283. //创建二级文件夹
  284. if (!empty($firstAndSecondFolderName['secondNames'])) {
  285. foreach ($firstAndSecondFolderName['secondNames'] as $k => $item) {
  286. $secondKey = $k + 1;
  287. $secondDirPath = $firstDirPath . '\\' . $secondKey . $item;
  288. if (!is_dir($secondDirPath)) {
  289. mkdir($secondDirPath, 0x777, true);
  290. }
  291. //再创建txt文件
  292. $filePath = $secondDirPath . '\\' . $secondKey . $item . '.txt';
  293. $fd = fopen($filePath, 'w+');
  294. fclose($fd);
  295. }
  296. } else {
  297. //创建txt文件
  298. $filePath = $firstDirPath . '\\' . $firstKey . $firstAndSecondFolderName['firstName'] . '.txt';
  299. $fd = fopen($filePath, 'w+');
  300. fclose($fd);
  301. }
  302. }
  303. }
  304. function deleteTxtFilesRecursively($directory) {
  305. // 检查目录是否存在
  306. if (!is_dir($directory)) {
  307. return "目录不存在";
  308. }
  309. // 获取目录下的所有文件和子目录
  310. $files = scandir($directory);
  311. // 过滤掉 "." 和 ".."(当前目录和上一级目录)
  312. $files = array_diff($files, array('.', '..'));
  313. foreach ($files as $file) {
  314. // 拼接完整的文件路径
  315. $filePath = $directory . DIRECTORY_SEPARATOR . $file;
  316. // 判断是否为文件
  317. if (is_file($filePath)) {
  318. // 检查文件扩展名是否为 .txt
  319. if (pathinfo($filePath, PATHINFO_EXTENSION) === 'xlsx') {
  320. // 删除文件
  321. if (unlink($filePath)) {
  322. echo "已删除文件: $filePath\n";
  323. } else {
  324. echo "无法删除文件: $filePath\n";
  325. }
  326. }
  327. } elseif (is_dir($filePath)) {
  328. // 如果是目录,则递归调用
  329. $this->deleteTxtFilesRecursively($filePath);
  330. }
  331. }
  332. }
  333. public function clearTxtFilesContent($directory)
  334. {
  335. // 检查目录是否存在
  336. if (!is_dir($directory)) {
  337. return "目录不存在";
  338. }
  339. // 获取目录下的所有文件和子目录
  340. $files = scandir($directory);
  341. // 过滤掉 "." 和 ".."(当前目录和上一级目录)
  342. $files = array_diff($files, array('.', '..'));
  343. foreach ($files as $file) {
  344. // 拼接完整的文件路径
  345. $filePath = $directory . DIRECTORY_SEPARATOR . $file;
  346. // 判断是否为文件
  347. if (is_file($filePath)) {
  348. // 检查文件扩展名是否为 .txt
  349. if (pathinfo($filePath, PATHINFO_EXTENSION) === 'txt') {
  350. file_put_contents($filePath, '');
  351. echo "清空文件内容:$filePath\n";
  352. // 删除文件
  353. // if (unlink($filePath)) {
  354. // echo "已删除文件: $filePath\n";
  355. // } else {
  356. // echo "无法删除文件: $filePath\n";
  357. // }
  358. }
  359. } elseif (is_dir($filePath)) {
  360. // 如果是目录,则递归调用
  361. $this->clearTxtFilesContent($filePath);
  362. }
  363. }
  364. }
  365. }