Skip to content

Commit 858f58e

Browse files
authored
Add files via upload
1 parent c68f659 commit 858f58e

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from pathlib import Path
2+
3+
# Defines a function to print the directory structure into a file
4+
def print_directory_structure_to_file(root_dir, file_handle, prefix=''):
5+
# Sets of directories and file extensions to exclude/include
6+
excluded_dirs = {'.git', '.vs', 'bin', 'obj', 'Debug', 'Release', 'packages'}
7+
included_file_extensions = {'.sln', '.csproj', '.vbproj', '.cs', '.html', '.cshtml', '.css', '.js'}
8+
9+
# Converts root_dir to a Path object for easy path manipulation
10+
root_path = Path(root_dir)
11+
# Skip the directory if it's in the excluded list
12+
if root_path.name in excluded_dirs:
13+
return
14+
15+
# Writes the directory name to the file
16+
file_handle.write(f"{prefix}{root_path.name}/\n")
17+
# Increases the prefix for nested items
18+
prefix += ' '
19+
20+
# Iterates through each item in the directory
21+
for entry in root_path.iterdir():
22+
if entry.is_dir():
23+
# Recursively prints the structure of subdirectories
24+
print_directory_structure_to_file(entry, file_handle, prefix)
25+
elif entry.suffix in included_file_extensions:
26+
# Writes the file name if it has a relevant extension
27+
file_handle.write(f"{prefix}{entry.name}\n")
28+
29+
# Gets the location of the current script and resolves it to an absolute path
30+
script_location = Path(__file__).resolve().parent
31+
32+
# Sets the project folder path to the script location
33+
project_folder_path = script_location
34+
# Defines the output file path within the same directory as the script
35+
output_file_path = script_location / 'project_structure.txt'
36+
37+
# Attempts to write the directory structure to the specified file
38+
try:
39+
with open(output_file_path, 'w') as output_file:
40+
print_directory_structure_to_file(project_folder_path, output_file)
41+
print(f"The project structure has been written to '{output_file_path}'.")
42+
except Exception as e:
43+
print(f"Failed to write the file: {e}")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pathlib import Path
2+
3+
# Defines a function to print the directory structure into a file. This function recursively traverses the directory structure of a given root directory and writes the structure to a file, filtering based on specified criteria.
4+
def print_directory_structure_to_file(root_dir, file_handle, prefix=''):
5+
# A set of directory names to exclude from the output. Common directories not relevant to the project structure, such as version control directories and build output directories, are excluded.
6+
excluded_dirs = {'.git', '.vs', 'bin', 'obj', 'Debug', 'Release', 'packages'}
7+
# A set of file extensions to include in the output. This filters the files to only include those relevant to the project, such as source code and project files.
8+
included_file_extensions = {'.sln', '.csproj', '.vbproj', '.cs', '.html', '.cshtml', '.css', '.js'}
9+
10+
# Converts the root directory path string to a Path object for easy manipulation and checks.
11+
root_path = Path(root_dir)
12+
# Checks if the current directory is in the list of excluded directories and returns early if so.
13+
if root_path.name in excluded_dirs:
14+
return
15+
16+
# Writes the current directory name to the file, prefixed by the current indentation level to reflect the structure depth.
17+
file_handle.write(f"{prefix}{root_path.name}/\n")
18+
# Increases the indentation for the contents of this directory.
19+
prefix += ' '
20+
21+
# Iterates over each item (file or directory) in the current directory.
22+
for entry in root_path.iterdir():
23+
if entry.is_dir():
24+
# If the item is a directory, recursively call this function to print its structure.
25+
print_directory_structure_to_file(entry, file_handle, prefix)
26+
elif entry.suffix in included_file_extensions:
27+
# If the item is a file with an included extension, write its name to the file.
28+
file_handle.write(f"{prefix}{entry.name}\n")
29+
30+
# Hardcoded paths for the project directory and output file. These should be replaced or made configurable for different projects or environments.
31+
project_folder_path = 'C:/Project'
32+
output_file_path = 'C:/Users/Public/Desktop/project_structure.txt'
33+
34+
# Attempts to open the output file and write the directory structure to it. Error handling is included to catch and report any issues that occur during this process.
35+
try:
36+
with open(output_file_path, 'w') as output_file:
37+
print_directory_structure_to_file(project_folder_path, output_file)
38+
print(f"The project structure has been written to '{output_file_path}'.")
39+
except Exception as e:
40+
print(f"Failed to write the file: {e}")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from pathlib import Path
2+
3+
# Defines a function to print the directory structure into a file
4+
def print_directory_structure_to_file(root_dir, file_handle, prefix=''):
5+
# Sets of directories and file extensions to exclude/include
6+
excluded_dirs = {'.git', '.vs', 'bin', 'obj', 'Debug', 'Release', 'packages'}
7+
included_file_extensions = {'.sln', '.csproj', '.vbproj', '.cs', '.html', '.cshtml', '.css', '.js'}
8+
9+
# Converts root_dir to a Path object for easy path manipulation
10+
root_path = Path(root_dir)
11+
# Skip the directory if it's in the excluded list
12+
if root_path.name in excluded_dirs:
13+
return
14+
15+
# Writes the directory name to the file
16+
file_handle.write(f"{prefix}{root_path.name}/\n")
17+
# Increases the prefix for nested items
18+
prefix += ' '
19+
20+
# Iterates through each item in the directory
21+
for entry in root_path.iterdir():
22+
if entry.is_dir():
23+
# Recursively prints the structure of subdirectories
24+
print_directory_structure_to_file(entry, file_handle, prefix)
25+
elif entry.suffix in included_file_extensions:
26+
# Writes the file name if it has a relevant extension
27+
file_handle.write(f"{prefix}{entry.name}\n")
28+
29+
# Prompts the user to enter the project folder path
30+
project_folder_path = input("Enter the project folder path: ")
31+
32+
# Prompts the user to enter the output file path or folder
33+
output_file_path = input("Enter the output file path or folder: ").strip('"')
34+
output_path = Path(output_file_path)
35+
36+
# Checks if the provided output path is a directory or needs modification
37+
if output_path.is_dir():
38+
file_name = input("Enter the name of the output txt file: ")
39+
if not file_name.endswith('.txt'):
40+
file_name += '.txt'
41+
output_file_path = output_path.joinpath(file_name)
42+
elif not output_path.suffix:
43+
file_name = "project_structure.txt"
44+
output_file_path = output_path.joinpath(file_name)
45+
elif not output_path.name.endswith('.txt'):
46+
output_file_path = output_path.with_suffix('.txt')
47+
48+
# Attempts to write the directory structure to the specified file
49+
try:
50+
with open(output_file_path, 'w') as output_file:
51+
print_directory_structure_to_file(project_folder_path, output_file)
52+
print(f"The project structure has been written to '{output_file_path}'.")
53+
except Exception as e:
54+
print(f"Failed to write the file: {e}")

0 commit comments

Comments
 (0)