其他方法:方法简单,一行命令即可,只能生成目录,但无法直接打开对应文件,略有不便。
tree /f>content.txt
保存为bat
该可执行文件为python编写,无任何夹带,源码如下:
import os
from openpyxl import Workbook
from openpyxl.styles import Font
from datetime import date
# 获取目录信息
def get_dirs_files(path, depth=0):
data = []
for entry in os.scandir(path):
if entry.is_file():
data.append([depth, entry.path])
elif entry.is_dir():
data.append([depth, entry.path])
data.extend(get_dirs_files(entry.path, depth + 1))
return data
# 写入Excel
def write_to_excel(data, filename):
wb = Workbook()
ws = wb.active
for row in data:
depth = row[0]
path = row[1]
row_num = ws.max_row + 1
for i in range(depth):
cell = ws.cell(row=row_num, column=i + 1)
cell.value = ''
cell = ws.cell(row=row_num, column=depth + 1)
cell.value = os.path.basename(path)
cell.hyperlink = path
red_font = Font(color="FFFF0000")
if os.path.isdir(path):
cell.font = red_font
today = date.today().strftime("%Y-%m-%d")
suffix = ""
index = 1
while os.path.exists(f"{filename}_{today}_{suffix}.xlsx"):
suffix = f"_{index}"
index += 1
wb.save(f"{filename}_{today}_{suffix}.xlsx")
data = get_dirs_files('.')
write_to_excel(data, '目录')
很简单,欢迎取用