本文目录导读:

- Python 脚本(最灵活)
- Bash 脚本(Linux/Mac)
- PowerShell 脚本(Windows)
- 配置文件示例(structure.json)
- 文件夹列表文件(folders.txt)
- 使用方法:
我来帮你生成一个批量创建文件夹结构的脚本,以下是几种常用方式:
Python 脚本(最灵活)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
批量生成文件夹结构脚本
支持自定义层级结构,自动创建嵌套文件夹
"""
import os
import json
from pathlib import Path
from datetime import datetime
class FolderGenerator:
def __init__(self, base_path="."):
"""
初始化文件夹生成器
:param base_path: 基础路径,默认为当前目录
"""
self.base_path = Path(base_path)
self.created_folders = []
self.log_file = None
def create_folder_structure(self, structure_dict):
"""
根据字典结构创建文件夹
:param structure_dict: 文件夹结构字典,
{
"项目名称": {
"docs": {},
"src": {
"components": {},
"utils": {}
},
"tests": {}
}
}
"""
for folder_name, sub_structure in structure_dict.items():
self._create_recursive(folder_name, sub_structure)
self._log_results()
def _create_recursive(self, folder_path, sub_structure):
"""递归创建文件夹"""
full_path = self.base_path / folder_path
if not full_path.exists():
full_path.mkdir(parents=True, exist_ok=True)
self.created_folders.append(str(full_path))
print(f"✅ 创建: {full_path}")
# 递归创建子文件夹
if isinstance(sub_structure, dict):
for sub_name, sub_content in sub_structure.items():
sub_path = f"{folder_path}/{sub_name}"
self._create_recursive(sub_path, sub_content)
def create_from_json(self, json_file):
"""从JSON文件读取结构创建"""
with open(json_file, 'r', encoding='utf-8') as f:
structure = json.load(f)
self.create_folder_structure(structure)
def create_default_project(self, project_name, include_git=False):
"""创建默认的项目结构"""
default_structure = {
project_name: {
"src": {
"components": {},
"utils": {},
"assets": {},
"styles": {},
"templates": {}
},
"docs": {},
"tests": {},
"config": {},
"scripts": {}
}
}
self.create_folder_structure(default_structure)
if include_git:
gitignore_content = """# Python
__pycache__/
*.py[cod]
*.so
# Node
node_modules/
dist/
build/
# IDE
.idea/
.vscode/
*.swp
# 系统文件
.DS_Store
Thumbs.db
# 环境变量
.env
.env.local
"""
gitignore_path = self.base_path / project_name / ".gitignore"
gitignore_path.write_text(gitignore_content, encoding='utf-8')
print(f"✅ 创建: .gitignore")
readme_content = f"# {project_name}\n\n项目说明文档\n"
readme_path = self.base_path / project_name / "README.md"
readme_path.write_text(readme_content, encoding='utf-8')
print(f"✅ 创建: README.md")
def create_bulk_directories(self, folder_names, location="."):
"""批量创建同级文件夹"""
target_path = self.base_path / location
for name in folder_names:
folder_path = target_path / name
folder_path.mkdir(parents=True, exist_ok=True)
self.created_folders.append(str(folder_path))
print(f"✅ 创建: {folder_path}")
self._log_results()
def _log_results(self):
"""记录创建日志"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_file = self.base_path / f"folder_structure_log_{timestamp}.txt"
with open(log_file, 'w', encoding='utf-8') as f:
f.write(f"文件夹创建日志 - {datetime.now()}\n")
f.write("=" * 50 + "\n\n")
f.write(f"共创建 {len(self.created_folders)} 个文件夹:\n\n")
for folder in self.created_folders:
f.write(f"- {folder}\n")
print(f"\n📄 日志已保存: {log_file}")
# 使用示例
if __name__ == "__main__":
# 创建实例
generator = FolderGenerator("./output")
# 方式1: 创建默认项目结构
# generator.create_default_project("my_project", include_git=True)
# 方式2: 自定义结构
custom_structure = {
"web_project": {
"frontend": {
"src": {
"components": {},
"pages": {},
"assets": {},
"router": {}
},
"public": {},
"tests": {}
},
"backend": {
"api": {},
"models": {},
"services": {},
"middlewares": {}
},
"database": {
"migrations": {},
"seeds": {},
"backups": {}
},
"deployment": {
"docker": {},
"nginx": {},
"scripts": {}
}
}
}
generator.create_folder_structure(custom_structure)
# 方式3: 批量创建同级文件夹
# folder_names = ["images", "videos", "documents", "archives", "temp"]
# generator.create_bulk_directories(folder_names, "files")
# 方式4: 从JSON文件读取
# generator.create_from_json("structure.json")
Bash 脚本(Linux/Mac)
#!/bin/bash
# batch_create_folders.sh
# 批量创建文件夹结构
set -e # 遇到错误立即退出
# 颜色输出
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# 日志文件
LOG_FILE="folder_creation_$(date +%Y%m%d_%H%M%S).log"
# 创建文件夹函数
create_dirs() {
local path=$1
if [ ! -d "$path" ]; then
mkdir -p "$path"
echo "✅ 创建: $path" | tee -a "$LOG_FILE"
else
echo "⏭️ 已存在: $path"
fi
}
# 创建多级目录
create_nested_dirs() {
local base_dir=$1
shift
local dirs=("$@")
for dir in "${dirs[@]}"; do
create_dirs "$base_dir/$dir"
done
}
# 菜单选择
show_menu() {
echo "请选择创建方式:"
echo "1) 创建项目结构"
echo "2) 创建日期文件夹"
echo "3) 自定义嵌套结构"
echo "4) 从文件读取列表"
echo "5) 退出"
}
# 创建日期文件夹
create_dated_folders() {
local base_date_dir=$1
local years=5
for i in $(seq 0 $((years-1))); do
year=$(( $(date +%Y) - i ))
for month in {01..12}; do
create_dirs "$base_date_dir/$year/$month"
done
done
}
# 主函数
main() {
echo "🚀 批量文件夹创建工具"
echo "======================"
while true; do
show_menu
read -p "请输入选项 (1-5): " choice
case $choice in
1)
echo "创建项目结构..."
project_name="demo_project"
create_nested_dirs "$project_name" "docs" "src/components" "src/utils" "src/assets" "tests" "config" "scripts"
echo -e "${GREEN}✅ 项目结构创建完成${NC}"
;;
2)
echo "创建日期文件夹..."
create_dated_folders "archive"
echo -e "${GREEN}✅ 日期文件夹创建完成${NC}"
;;
3)
echo "自定义嵌套结构(用空格分隔):"
echo "示例: src/components src/utils src/assets tests"
read -p "输入目录结构: " custom_dirs
read -p "基础目录名: " base_dir
create_nested_dirs "$base_dir" $custom_dirs
;;
4)
echo "从文件读取文件夹列表..."
echo "请创建 folders.txt 文件,每行一个路径"
if [ -f "folders.txt" ]; then
while IFS= read -r folder; do
if [ -n "$folder" ]; then # 跳过空行
create_dirs "$folder"
fi
done < "folders.txt"
else
echo -e "${RED}未找到 folders.txt 文件${NC}"
fi
;;
5)
echo "👋 再见!"
break
;;
*)
echo -e "${RED}无效的选项${NC}"
;;
esac
done
echo -e "${GREEN}日志已保存到: $LOG_FILE${NC}"
}
main
PowerShell 脚本(Windows)
# batch_create_folders.ps1
# Windows PowerShell 批量创建文件夹脚本
<#
.SYNOPSIS
批量创建文件夹结构脚本
.DESCRIPTION
支持多种方式的文件夹结构创建
#>
# 设置编码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
# 日志函数
function Write-Log {
param([string]$Message, [string]$Type = "INFO")
$time = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "[$time] [$Type] $Message"
Write-Host $logMessage
Add-Content -Path $script:LogFile -Value $logMessage
}
# 创建文件夹函数
function New-FolderStructure {
param(
[string]$BasePath = ".",
[string[]]$FolderList
)
foreach ($folder in $FolderList) {
$fullPath = Join-Path $BasePath $folder
if (-not (Test-Path $fullPath)) {
New-Item -Path $fullPath -ItemType Directory -Force | Out-Null
Write-Log "创建: $fullPath" "OK"
} else {
Write-Log "已存在: $fullPath" "SKIP"
}
}
}
# 创建日期文件夹
function New-DatedFolders {
param(
[string]$BasePath = "archive",
[int]$Years = 5
)
$currentYear = Get-Date -Format "yyyy"
for ($i = 0; $i -lt $Years; $i++) {
$year = [int]$currentYear - $i
for ($month = 1; $month -le 12; $month++) {
$monthStr = "{0:D2}" -f $month
$path = "$BasePath/$year/$monthStr"
New-FolderStructure -BasePath $path -FolderList @()
}
}
}
# 创建项目结构
function New-ProjectStructure {
param([string]$ProjectName)
$folders = @(
"src/components",
"src/utils",
"src/assets",
"src/styles",
"docs",
"tests",
"config",
"scripts",
"public"
)
New-FolderStructure -BasePath $ProjectName -FolderList $folders
# 创建.gitignore
$gitignore = @"
# Python
__pycache__/
*.py[cod]
# Node
node_modules/
dist/
# IDE
.idea/
.vscode/
"@
Set-Content -Path "$ProjectName/.gitignore" -Value $gitignore -Encoding UTF8
Write-Log "创建 .gitignore" "OK"
}
# 从文件读取文件夹列表
function Read-FolderListFile {
param([string]$FilePath = "folders.txt")
if (Test-Path $FilePath) {
$folders = Get-Content $FilePath
New-FolderStructure -BasePath "." -FolderList $folders
} else {
Write-Log "未找到文件: $FilePath" "ERROR"
}
}
# 主菜单
function Show-Menu {
Clear-Host
Write-Host "`n=== 批量文件夹创建工具 ===" -ForegroundColor Cyan
Write-Host "1. 创建项目结构"
Write-Host "2. 创建日期文件夹"
Write-Host "3. 自定义创建"
Write-Host "4. 从文件读取"
Write-Host "5. 退出"
Write-Host "=========================`n"
}
# 自定义创建
function Custom-Create {
Write-Host "输入文件夹列表(用逗号分隔):" -ForegroundColor Yellow
Write-Host " src/components,src/utils,tests,docs"
$input = Read-Host "文件夹列表"
$folderList = $input -split "," | Where-Object { $_.Trim() -ne "" }
New-FolderStructure -BasePath "." -FolderList $folderList
}
# 主程序
$script:LogFile = "folder_creation_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
# 清理旧的日志
if (Test-Path $LogFile) { Remove-Item $LogFile }
while ($true) {
Show-Menu
$choice = Read-Host "请选择选项"
switch ($choice) {
"1" {
$projectName = Read-Host "输入项目名称"
New-ProjectStructure -ProjectName $projectName
Write-Host "✅ 项目结构创建完成" -ForegroundColor Green
}
"2" {
New-DatedFolders
Write-Host "✅ 日期文件夹创建完成" -ForegroundColor Green
}
"3" {
Custom-Create
Write-Host "✅ 自定义文件夹创建完成" -ForegroundColor Green
}
"4" {
$filePath = Read-Host "输入文件夹列表文件路径 (默认: folders.txt)"
if ([string]::IsNullOrEmpty($filePath)) { $filePath = "folders.txt" }
Read-FolderListFile -FilePath $filePath
Write-Host "✅ 从文件创建完成" -ForegroundColor Green
}
"5" {
Write-Host "👋 再见!" -ForegroundColor Cyan
break
}
default {
Write-Host "❌ 无效选项" -ForegroundColor Red
}
}
if ($choice -ne "5") {
Write-Host "`n按任意键继续..." -ForegroundColor Gray
$null = Read-Host
}
}
Write-Host "日志已保存至: $LogFile" -ForegroundColor Yellow
配置文件示例(structure.json)
{
"project_name": {
"src": {
"components": {},
"pages": {},
"utils": {},
"assets": {
"images": {},
"fonts": {},
"icons": {}
},
"styles": {},
"templates": {}
},
"public": {},
"docs": {
"api": {},
"guide": {}
},
"tests": {
"unit": {},
"integration": {},
"e2e": {}
},
"config": {
"development": {},
"production": {},
"test": {}
},
"scripts": {},
"database": {
"migrations": {},
"seeds": {}
}
}
}
文件夹列表文件(folders.txt)
2024/01
2024/02
2024/03
projects/web/src/components
projects/web/src/utils
projects/mobile/android
projects/mobile/ios
backup/2024
backup/2023
docs/api
docs/manual
使用方法:
Python 脚本:
# 运行简单示例 python3 folder_generator.py # 从JSON文件创建 python3 folder_generator.py structure.json
Bash 脚本:
# 添加执行权限 chmod +x batch_create_folders.sh # 运行 ./batch_create_folders.sh
PowerShell 脚本:
# 允许执行脚本(管理员权限) Set-ExecutionPolicy RemoteSigned # 运行 .\batch_create_folders.ps1
选择适合你需求的脚本,直接复制使用即可!
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播