Skip to content

Commit 99cb85b

Browse files
committed
Add script to combine files and generate folder structure
- Implemented a function to generate a folder tree structure. - Created a main function to combine all text files in a specified directory into a single output file. - Added metadata header including generation date, OS information, and root folder path. - Included options to skip specific file extensions, filenames, and folders during the combination process. - Handled exceptions for unreadable files and provided feedback in the output file.
1 parent dd7b5e9 commit 99cb85b

File tree

4 files changed

+254
-236
lines changed

4 files changed

+254
-236
lines changed

MyDocs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 67be7b0a93f6c77864ee127bfd1da66a3cab6efe

combine.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import os
2+
import platform
3+
import datetime
4+
5+
# --- Folder Tree Generator ---
6+
def generate_folder_map(root_folder, skip_folders):
7+
lines = []
8+
9+
def walk(folder, prefix=""):
10+
try:
11+
entries = sorted(os.listdir(folder))
12+
except (PermissionError, FileNotFoundError):
13+
return
14+
15+
entries = [e for e in entries if not e.startswith(".")]
16+
entries = [e for e in entries if not any(skip.lower() in e.lower() for skip in skip_folders)]
17+
18+
for i, entry in enumerate(entries):
19+
path = os.path.join(folder, entry)
20+
connector = "└── " if i == len(entries) - 1 else "├── "
21+
if os.path.isdir(path):
22+
lines.append(f"{prefix}{connector}{entry}")
23+
extension = " " if i == len(entries) - 1 else "│ "
24+
walk(path, prefix + extension)
25+
else:
26+
lines.append(f"{prefix}{connector}{entry}")
27+
28+
walk(root_folder)
29+
return "\n".join(lines)
30+
31+
# --- Combine All Files Into One ---
32+
def dump_all_files_to_txt(root_folder, output_file, skip_extensions=None, skip_files=None, skip_folders=None):
33+
skip_extensions = skip_extensions or []
34+
skip_files = skip_files or []
35+
skip_folders = skip_folders or []
36+
37+
with open(output_file, "w", encoding="utf-8", errors="ignore") as outfile:
38+
# --- METADATA HEADER ---
39+
now = datetime.datetime.now().strftime("%B %d, %Y – %I:%M %p")
40+
outfile.write("===== PROJECT METADATA =====\n")
41+
outfile.write(f"Generated on: {now}\n")
42+
outfile.write(f"OS: {platform.system()} {platform.release()}\n")
43+
outfile.write(f"Root Folder: {os.path.abspath(root_folder)}\n\n")
44+
45+
# --- ROUTE MAP (XML WRAPPED) ---
46+
outfile.write("===== ROUTE MAP / FOLDER STRUCTURE =====\n")
47+
outfile.write("<folder-structure>\n")
48+
folder_map = generate_folder_map(root_folder, skip_folders)
49+
outfile.write(folder_map if folder_map.strip() else "(No files found)")
50+
outfile.write("\n</folder-structure>\n")
51+
52+
# --- BEGIN FILE CONTENTS ---
53+
outfile.write("\n\n===== BEGIN FILE CONTENTS =====\n")
54+
55+
for foldername, subfolders, filenames in os.walk(root_folder):
56+
# Skip ignored folders
57+
if any(skip.lower() in foldername.lower() for skip in skip_folders):
58+
continue
59+
60+
for filename in filenames:
61+
file_ext = os.path.splitext(filename)[1].lower()
62+
file_lower = filename.lower()
63+
64+
if file_ext in skip_extensions or file_lower in skip_files:
65+
continue
66+
67+
file_path = os.path.join(foldername, filename)
68+
try:
69+
with open(file_path, "r", encoding="utf-8", errors="ignore") as infile:
70+
outfile.write(f"\n\n===== FILE: {file_path} =====\n\n")
71+
outfile.write(infile.read())
72+
except Exception as e:
73+
outfile.write(f"\n\n===== FILE: {file_path} (Could not read: {e}) =====\n\n")
74+
75+
print(f"\n✅ Combined file created successfully: {output_file}")
76+
77+
# --- MAIN EXECUTION ---
78+
if __name__ == "__main__":
79+
root_folder = r"C:\Users\Ashish jha\Desktop\PERSONAL\CodeToContext" # 👈 Change this
80+
output_file = "project_snapshot.txt"
81+
82+
skip_extensions = [
83+
".jpg", ".jpeg", ".png", ".ico", ".bmp", ".svg", ".mp3",
84+
".avi", ".mov", ".zip", ".rar", ".7z", ".tar", ".gz",
85+
".exe", ".dll", ".bin", ".pdf"
86+
]
87+
88+
skip_files = [".ds_store", "package-lock.json", "yarn.lock", ".env", ".gitignore"]
89+
90+
skip_folders = [
91+
"node_modules", "dist", "build", "target", ".idea", ".vscode",
92+
"__pycache__", ".angular", ".next", ".git", ".svn",
93+
"venv", "env", "logs", "coverage", "out", "tmp"
94+
]
95+
96+
dump_all_files_to_txt(root_folder, output_file, skip_extensions, skip_files, skip_folders)

index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
</div>
3232
</div>
3333
<div class="header-right">
34+
<button class="btn-generate" id="pybutton" onclick="downloadPythonScript()">
35+
<i class="fa-brands fa-python"></i>
36+
Python CODE
37+
</button>
3438
<div class="sidebar-select">
3539
<button class="btn-generate" id="docsbutton" onclick="window.location.href='help.html'">
3640
<span class="material-symbols-outlined">description</span>
@@ -178,4 +182,4 @@ <h3>EXPLORER</h3>
178182
<script src="index.js"></script>
179183
</body>
180184

181-
</html>
185+
</html>

0 commit comments

Comments
 (0)