createFileEx($directory); //$directory = "E:\\鸭题库数据初始化历年真题"; // 指定目录路径 //$this->createFile($directory); //$this->deleteTxtFilesRecursively($directory); $directory = "E:\\鸭题库数据初始化考点_第二次\\中药二"; // 指定目录路径 //$this->clearTxtFilesContent($directory); // $this->deleteTxtFilesRecursively($directory); $baseDirectory = "E:\\鸭题库数据\\中药二"; $firstFolderName = [ '第一节 中药药物警戒', '第二节 中药不良反应', '第三节 中药用药错误', '第四节 医疗用毒性中药的中毒反应和基本救治原则', ]; //$this->createFirstFolder($firstFolderName, $baseDirectory); $firstAndSecondFolderNames = [ [ 'firstName' => '历年真题', 'secondNames' => [ '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022-1', '2022-2', '2023', '2024', ] ], // [ // 'firstName' => '第二章 处方审核与调剂', // 'secondNames' => [] // ], // [ // 'firstName' => '第三章 用药咨询与药物治疗管理', // 'secondNames' => [ // '第一节 药学信息咨询服务', // '第二节 疾病管理与健康宣教', // '第三节 药物治疗管理', // '第四节 常用医学检查', // ] // ], // [ // 'firstName' => '第四章 用药安全', // 'secondNames' => [ // '第一节 药物警戒', // '第二节 药品不良反应', // '第三节 药源性疾病', // '第四节 用药错误', // '第五节 特殊人群用药' // ] // ], // [ // 'firstName' => '第五章 急救、中毒解救及职业防护', // 'secondNames' => [ // '第一节 急救的意义与原则', // '第二节 常见急症及处置', // '第三节 中毒解救', // '第四节 细胞毒性药物的职业防护' // ] // ], // [ // 'firstName' => '第六章 常见病症的健康管理', // 'secondNames' => [ // '第一节 发热', // '第二节 疼痛', // '第三节 咳嗽', // '第四节 感冒', // '第五节 急性扁桃体炎', // '第六节 鼻窦炎', // '第七节 过敏性鼻炎', // '第八节 急性气管-支气管炎', // '第九节 社区获得性肺炎', // '第十节 口腔溃疡', // '第十一节 消化不良', // '第十二节 胆石症与胆囊炎', // '第十三节 便秘', // '第十四节 腹泻', // '第十五节 痔疮', // '第十六节 阴道炎', // '第十七节 乳腺炎', // '第十八节 尿路感染', // '第十九节 男性性功能障碍', // '第二十节 尿失禁', // '第二十一节 绝经综合征', // '第二十二节 多囊卵巢综合征', // '第二十三节 避孕', // '第二十四节 昆虫叮咬', // '第二十五节 烫伤', // '第二十六节 带状疱疹', // '第二十七节 单纯疱疹', // '第二十八节 冻伤 ( 疮 )', // '第二十九节 白癜风', // '第三十节 银屑病', // '第三十一节 痤疮', // '第三十二节 荨麻疹', // '第三十三节 湿疹', // '第三十四节 甲沟炎', // '第三十五节 特应性皮炎', // '第三十六节 真菌感染性皮肤病', // '第三十七节 雄激素性脱发', // '第三十八节 疖痈', // '第三十九节 视疲劳', // '第四十节 急性结膜炎', // '第四十一节 干眼症', // '第四十二节 沙眼', // '第四十三节 麦粒肿', // '第四十四节 贫血', // '第四十五节 超重和肥胖', // '第四十六节 晕动病' // ] // ], // [ // 'firstName' => '第七章 慢性病管理', // 'secondNames' => [ // ] // ], // [ // 'firstName' => '第八章 呼吸系统常见疾病', // 'secondNames' => [ // '第一节 哮喘', // '第二节 慢性阻塞性肺疾病' // ] // ], // [ // 'firstName' => '第九章 心血管系统常见疾病', // 'secondNames' => [ // '第一节 高血压', // '第二节 血脂异常', // '第三节 冠状动脉粥样硬化', // '第四节 心房颤动' // ] // ], // [ // 'firstName' => '第十章 神经精神系统常见疾病', // 'secondNames' => [ // '第一节 焦虑抑郁', // '第二节 睡眠障碍', // '第三节 脑卒中', // '第四节 帕金森病', // '第五节 癫痫', // '第六节 痴呆' // ] // ], // [ // 'firstName' => '第十一章 消化系统常见疾病', // 'secondNames' => [ // '第一节 胃食管反流病', // '第二节 消化性溃疡', // '第三节 溃疡性结肠炎', // '第四节 慢性病毒性肝炎' // ] // ], // [ // 'firstName' => '第十二章 内分泌系统常见疾病', // 'secondNames' => [ // '第一节 甲状腺功能亢进症', // '第二节 甲状腺功能减退症', // '第三节 糖尿病', // '第四节 骨质疏松症', // '第五节 高尿酸血症与痛风' // ] // ], // [ // 'firstName' => '第十三章 免疫系统常见疾病', // 'secondNames' => [ // '第一节 类风湿关节炎', // '第二节 系统性红斑狼疮' // ] // ], // [ // 'firstName' => '第十四章 泌尿系统常见疾病', // 'secondNames' => [ // '第一节 良性前列腺增生症', // '第二节 慢性肾脏病' // ] // ], // [ // 'firstName' => '第十五章 肿瘤', // 'secondNames' => [ // '第一节 肿瘤的临床基础', // '第二节 肿瘤的治疗与预防' // ] // ], ]; //$this->createFirstAndSecondFolder($firstAndSecondFolderNames, $baseDirectory); $this->info('success!'); } public function createFile($directory) { $files = scandir($directory); $folders = []; foreach ($files as $key=> $file) { if ($file != "." && $file != "..") { // 排除当前目录"."和上级目录".." $fullPath = $directory . '/' . $file; if (is_dir($fullPath)) { $folders[] = $file; $tempFilePath = $directory . '\\' . $file . '\\' . $file . '.txt'; //dd($tempFilePath); //在目录下创建文件 $tempFile = fopen($tempFilePath, "w"); fclose($tempFile); } } } dd($folders); } public function createFileEx($baseDirectory) { if (!is_dir($baseDirectory)) { mkdir($baseDirectory, 0777, true); // 创建文件夹,0777 是权限设置,true 表示递归创建 } for ($i = 11; $i <= 100; $i++) { $fileName = $baseDirectory . '/' . 'file_' . $i . '.txt'; // 文件名格式为 file_1.txt, file_2.txt, ... $file = fopen($fileName, 'w'); // 打开文件用于写入,如果文件不存在会自动创建 if ($file) { fwrite($file, ''); // 写入内容,这里写入空内容 fclose($file); // 关闭文件 echo "文件 $fileName 创建成功!
"; } else { echo "文件 $fileName 创建失败!
"; } } } public function createFirstFolder($fileNames, $baseDirectory) { foreach ($fileNames as $key => $fileName) { $realKey = $key + 1; $dirPath = $baseDirectory . '\\' . $realKey . $fileName; if (!is_dir($dirPath)) { mkdir($dirPath, 0x777, true); } //再创建txt文件 $filePath = $dirPath . '\\' . $realKey . $fileName . '.txt'; $fd = fopen($filePath, 'w+'); fclose($fd); } } public function createFirstAndSecondFolder($firstAndSecondFolderNames, $baseDirectory) { foreach ($firstAndSecondFolderNames as $key => $firstAndSecondFolderName) { $firstKey = $key + 1; $firstDirPath = $baseDirectory . '\\' . $firstKey . $firstAndSecondFolderName['firstName']; if (!is_dir($firstDirPath)) { mkdir($firstDirPath, 0x777, true); } //创建二级文件夹 if (!empty($firstAndSecondFolderName['secondNames'])) { foreach ($firstAndSecondFolderName['secondNames'] as $k => $item) { $secondKey = $k + 1; $secondDirPath = $firstDirPath . '\\' . $secondKey . $item; if (!is_dir($secondDirPath)) { mkdir($secondDirPath, 0x777, true); } //再创建txt文件 $filePath = $secondDirPath . '\\' . $secondKey . $item . '.txt'; $fd = fopen($filePath, 'w+'); fclose($fd); } } else { //创建txt文件 $filePath = $firstDirPath . '\\' . $firstKey . $firstAndSecondFolderName['firstName'] . '.txt'; $fd = fopen($filePath, 'w+'); fclose($fd); } } } function deleteTxtFilesRecursively($directory) { // 检查目录是否存在 if (!is_dir($directory)) { return "目录不存在"; } // 获取目录下的所有文件和子目录 $files = scandir($directory); // 过滤掉 "." 和 ".."(当前目录和上一级目录) $files = array_diff($files, array('.', '..')); foreach ($files as $file) { // 拼接完整的文件路径 $filePath = $directory . DIRECTORY_SEPARATOR . $file; // 判断是否为文件 if (is_file($filePath)) { // 检查文件扩展名是否为 .txt if (pathinfo($filePath, PATHINFO_EXTENSION) === 'xlsx') { // 删除文件 if (unlink($filePath)) { echo "已删除文件: $filePath\n"; } else { echo "无法删除文件: $filePath\n"; } } } elseif (is_dir($filePath)) { // 如果是目录,则递归调用 $this->deleteTxtFilesRecursively($filePath); } } } public function clearTxtFilesContent($directory) { // 检查目录是否存在 if (!is_dir($directory)) { return "目录不存在"; } // 获取目录下的所有文件和子目录 $files = scandir($directory); // 过滤掉 "." 和 ".."(当前目录和上一级目录) $files = array_diff($files, array('.', '..')); foreach ($files as $file) { // 拼接完整的文件路径 $filePath = $directory . DIRECTORY_SEPARATOR . $file; // 判断是否为文件 if (is_file($filePath)) { // 检查文件扩展名是否为 .txt if (pathinfo($filePath, PATHINFO_EXTENSION) === 'txt') { file_put_contents($filePath, ''); echo "清空文件内容:$filePath\n"; // 删除文件 // if (unlink($filePath)) { // echo "已删除文件: $filePath\n"; // } else { // echo "无法删除文件: $filePath\n"; // } } } elseif (is_dir($filePath)) { // 如果是目录,则递归调用 $this->clearTxtFilesContent($filePath); } } } }