PHP实现文件夹遍历:找到index.htm时自动添加uploadme.xml
实现指定文件夹遍历并自动创建文件的方案
下面提供跨平台的几种实现方式,满足遍历scan_inside文件夹所有层级目录,检测到index.htm或index.html时自动创建uploadme.xml的需求:
一、Windows 批处理脚本
将以下代码保存为create_uploadme.bat,放在scan_inside同级目录下双击运行即可:
@echo off echo 扫描中... for /r "scan_inside" %%d in (.) do ( if exist "%%d\index.htm" type nul > "%%d\uploadme.xml" if exist "%%d\index.html" type nul > "%%d\uploadme.xml" ) echo 扫描完成!
脚本会递归遍历所有子目录,只要当前目录存在index.htm或index.html,就创建空的uploadme.xml文件。
二、Python 脚本(跨平台)
适合Windows、Linux、macOS所有系统,将代码保存为create_uploadme.py,与scan_inside同级目录,用Python运行:
import os root_dir = "scan_inside" print("扫描中...") for dirpath, _, filenames in os.walk(root_dir): # 检测当前目录是否存在目标文件 if "index.htm" in filenames or "index.html" in filenames: uploadme_path = os.path.join(dirpath, "uploadme.xml") # 若不想覆盖已存在的uploadme.xml,可添加if not os.path.exists(uploadme_path):判断 with open(uploadme_path, "w") as f: pass print("扫描完成!")
三、Linux/macOS Shell 脚本
将代码保存为create_uploadme.sh,执行chmod +x create_uploadme.sh赋予权限后,在终端运行:
echo "扫描中..." find scan_inside -type d -exec sh -c ' for dir do if [ -f "$dir/index.htm" ] || [ -f "$dir/index.html" ]; then touch "$dir/uploadme.xml" fi done ' sh {} + echo "扫描完成!"
内容的提问来源于stack exchange,提问作者Jurgen
相关产品推荐
相关产品推荐

