Old count_subdirectories and get_subdirectories functions were unusably inefficient when testing on real directories. Updated the functions so that directories are iterated efficiently
This commit is contained in:
parent
6ad29c3fed
commit
94052659f6
0
CURRENTLY IN DEBUG BRANCH.txt
Normal file
0
CURRENTLY IN DEBUG BRANCH.txt
Normal file
|
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
def append_to_folder_name(folder_path, suffix):
|
def append_to_folder_name(folder_path, suffix):
|
||||||
try:
|
try:
|
||||||
|
|
@ -19,22 +20,20 @@ def append_to_folder_name(folder_path, suffix):
|
||||||
|
|
||||||
def get_subdirectories(folder_path):
|
def get_subdirectories(folder_path):
|
||||||
subdirectories = []
|
subdirectories = []
|
||||||
for root, dirs, files in os.walk(folder_path):
|
for entry in os.scandir(folder_path):
|
||||||
if root.count(os.sep) - folder_path.count(os.sep) < 1:
|
if entry.is_dir():
|
||||||
for dir_name in dirs:
|
subdirectories.append(entry.path)
|
||||||
subdirectories.append(os.path.join(root, dir_name))
|
|
||||||
return subdirectories
|
return subdirectories
|
||||||
|
|
||||||
def count_subdirectories(directory):
|
def count_subdirectories(path):
|
||||||
subdirectory_count = 0
|
return sum(1 for dir in Path(path).iterdir() if dir.is_dir())
|
||||||
for _, dirnames, _ in os.walk(directory):
|
|
||||||
subdirectory_count += len(dirnames)
|
|
||||||
return subdirectory_count
|
|
||||||
|
|
||||||
def count_folder_files(folder_path):
|
def count_folder_files(folder_path):
|
||||||
try:
|
try:
|
||||||
# If no subfolders exist, return the number of files in the folder
|
# If no subfolders exist, return the number of files in the folder
|
||||||
|
print("entering count_subdirectories")
|
||||||
num_subdirectories = count_subdirectories(folder_path)
|
num_subdirectories = count_subdirectories(folder_path)
|
||||||
|
print("exiting count_subdirectories")
|
||||||
if num_subdirectories == 0:
|
if num_subdirectories == 0:
|
||||||
|
|
||||||
# Count the number of files in the folder
|
# Count the number of files in the folder
|
||||||
|
|
@ -44,12 +43,14 @@ def count_folder_files(folder_path):
|
||||||
# If subdirectories do exist, return the number of files in each subdirectory then this directory
|
# If subdirectories do exist, return the number of files in each subdirectory then this directory
|
||||||
else:
|
else:
|
||||||
file_count = ""
|
file_count = ""
|
||||||
|
print("entering get_subdirectories")
|
||||||
subdirectories = get_subdirectories(folder_path)
|
subdirectories = get_subdirectories(folder_path)
|
||||||
for i, directory in enumerate(subdirectories):
|
for i, directory in enumerate(subdirectories):
|
||||||
file_count = file_count + str(sum(1 for entry in os.scandir(directory) if entry.is_file()))
|
file_count = file_count + str(sum(1 for entry in os.scandir(directory) if entry.is_file()))
|
||||||
#If there's another element, add a space (Keep code this way so that append folder files looks right)
|
#If there's another element, add a space (Keep code this way so that append folder files looks right)
|
||||||
if i != len(subdirectories) - 1:
|
if i != len(subdirectories) - 1:
|
||||||
file_count = file_count + " "
|
file_count = file_count + " "
|
||||||
|
print(file_count)
|
||||||
#Append the number of files in the folder unless if there are none
|
#Append the number of files in the folder unless if there are none
|
||||||
if (sum(1 for entry in os.scandir(folder_path) if entry.is_file()) > 0):
|
if (sum(1 for entry in os.scandir(folder_path) if entry.is_file()) > 0):
|
||||||
file_count = file_count + " " + str(sum(1 for entry in os.scandir(folder_path) if entry.is_file()))
|
file_count = file_count + " " + str(sum(1 for entry in os.scandir(folder_path) if entry.is_file()))
|
||||||
|
|
@ -95,6 +96,7 @@ def main():
|
||||||
os.system("pause")
|
os.system("pause")
|
||||||
for arg in args:
|
for arg in args:
|
||||||
directory_name = arg
|
directory_name = arg
|
||||||
|
print("entering count_folder_files")
|
||||||
suffix = count_folder_files(directory_name)
|
suffix = count_folder_files(directory_name)
|
||||||
# If [num] already exists at the end of the string, remove it so we can update it
|
# If [num] already exists at the end of the string, remove it so we can update it
|
||||||
directory_name_cleaned = clean_directory_name(directory_name)
|
directory_name_cleaned = clean_directory_name(directory_name)
|
||||||
|
|
|
||||||
38
Install_CountFolderFiles.ps1
Normal file
38
Install_CountFolderFiles.ps1
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
||||||
|
|
||||||
|
if ($isAdmin) {
|
||||||
|
pyinstaller --onefile CountFolderFiles.py
|
||||||
|
cd dist
|
||||||
|
$directoryPath = "C:\Program Files\Python311\Scripts\"
|
||||||
|
if (Test-Path $directoryPath -PathType Container) {
|
||||||
|
|
||||||
|
# Add CountFolderFiles.exe to Python Scripts
|
||||||
|
Copy-Item -Path .\CountFolderFiles.exe -Destination 'C:\Program Files\Python311\Scripts\' -Force
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Get the name of the SendTo folder for the user of the current directory. (can't use %appdata% since we are logged in as admin)
|
||||||
|
$directoryPath = $PWD.Path
|
||||||
|
$separator = "\"
|
||||||
|
$parts = $directoryPath.Split('\')
|
||||||
|
$extractedString = $parts[0..2] -join $separator
|
||||||
|
$SendToPath = $extractedString + "\AppData\Roaming\Microsoft\Windows\SendTo\CountFolderFiles.lnk"
|
||||||
|
|
||||||
|
|
||||||
|
# Add a shortcut to C:\Program Files\Python311\Scripts\CountFolderFiles.exe in SendTo folder
|
||||||
|
$SourceExe = "C:\Program Files\Python311\Scripts\CountFolderFiles.exe"
|
||||||
|
$DestinationPath = $SendToPath
|
||||||
|
|
||||||
|
$WshShell = New-Object -ComObject WScript.Shell
|
||||||
|
$Shortcut = $WshShell.CreateShortcut($DestinationPath)
|
||||||
|
$Shortcut.TargetPath = $SourceExe
|
||||||
|
$Shortcut.Save()
|
||||||
|
|
||||||
|
Read-Host -Prompt "Done, Press enter to exit."
|
||||||
|
} else {
|
||||||
|
Read-Host -Prompt "The Python311 directory does not exist. Please install manually (read the powershell commands) or use Python 3.11."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Read-Host -Prompt "Please run this script as admin."
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user