-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organize Files, Rename Files, and Search Text Files (#342)
* Added Pomodoro Timer * Update README.md * Update README.md * Added FileOrganizer,FileRenamer and FileTextSearch
- Loading branch information
1 parent
0671816
commit 88ee0ad
Showing
7 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os | ||
import shutil | ||
|
||
# Prompt the user for the directory path to organize files | ||
path = input("Enter path: ") | ||
|
||
# List all files in the specified directory | ||
files = os.listdir(path) | ||
|
||
# Iterate through each file in the directory | ||
for file in files: | ||
# Split the filename and extension | ||
filename, extension = os.path.splitext(file) | ||
|
||
# Remove the leading dot from the extension for folder naming | ||
extension = extension[1:] | ||
|
||
# Check if a directory for the file extension already exists | ||
if os.path.exists(path + '/' + extension): | ||
# Move the file to the corresponding extension folder | ||
shutil.move(path + '/' + file, path + '/' + extension + '/' + file) | ||
else: | ||
# If the directory does not exist, create it | ||
os.makedirs(path + '/' + extension) | ||
# Move the file to the newly created extension folder | ||
shutil.move(path + '/' + file, path + '/' + extension + '/' + file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# File Organizer Script | ||
## Description | ||
The `FileOrganizer.py` script is a simple utility for organizing files in a specified directory. It automatically sorts files into subdirectories based on their file extensions, making it easier to manage and locate files. | ||
## Features | ||
- *Automatic Organization:* Files are moved into subfolders named after their extensions (e.g., all `.jpg` files go into a folder named `jpg`). | ||
- *Dynamic Folder Creation:* If a folder for a specific file type doesn't exist, it will be created automatically. | ||
## Usage | ||
1. Ensure you have Python installed on your machine. | ||
2. Download the FileOrganizer.py script. | ||
3. Open a terminal or command prompt and navigate to the directory where the script is located. | ||
4. Run the script using the following command: | ||
python FileOrganizer.py | ||
5. When prompted, enter the path of the directory you want to organize. | ||
Enter path: /path/to/your/directory | ||
6. The script will process the files in the specified directory and create folders for each file type, moving the corresponding files into their respective folders. | ||
## Requirements | ||
- Python 3.x | ||
- Basic understanding of file paths in your operating system. | ||
## License | ||
This script is open-source and free to use. Feel free to modify and distribute as needed. | ||
## Contributing | ||
If you'd like to contribute to this project, please open an issue or submit a pull request with your proposed changes or enhancements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import os | ||
import re | ||
|
||
# Prompt the user for the directory path where files need to be renamed | ||
directory = input("Enter path: ") | ||
|
||
# Provide instructions on how to use regex for searching specific files | ||
print("To look for specific files, enter what you know, using .* for any characters you don't know.") | ||
print("For example: IMG.* will filter files that start with IMG") | ||
|
||
# Get the regex pattern to match files and the new base name for renaming | ||
pattern = input("Enter pattern: ") | ||
new_name = input("Enter the new name: ") | ||
|
||
def rename_files(directory, pattern, new_name): | ||
# List all files in the specified directory | ||
files = os.listdir(directory) | ||
counter = 0 # Initialize a counter for unique naming | ||
|
||
# Iterate over each file in the directory | ||
for file in files: | ||
# Check if the file matches the given pattern | ||
if re.match(pattern, file): | ||
# Get the file extension | ||
filetype = file.split('.')[-1] | ||
# Rename the file with the new base name and counter | ||
os.rename(directory + '/' + file, directory + '/' + new_name + str(counter) + '.' + filetype) | ||
counter += 1 # Increment the counter for the next file | ||
|
||
# Call the function to rename files | ||
rename_files(directory, pattern, new_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# File Renamer Script | ||
## Description | ||
The `FileRenamer.py` script is a utility designed to rename multiple files in a specified directory based on a given regex pattern. This script helps users quickly standardize file names, making file management easier. | ||
## Features | ||
- *Batch Renaming:* Rename multiple files that match a specified regex pattern. | ||
- *Custom Naming:* Users can provide a new base name for the renamed files, with a counter appended to ensure unique names. | ||
## Usage | ||
1. Ensure you have Python installed on your machine. | ||
2. Download the `FileRenamer.py` script. | ||
3. Open a terminal or command prompt and navigate to the directory where the script is located. | ||
4. Run the script using the following command: | ||
python `FileRenamer.py` | ||
5. When prompted, enter the path of the directory containing the files you want to rename. | ||
Enter path: /path/to/your/directory | ||
6. Enter the regex pattern for the files you want to rename. For example: | ||
Enter pattern: IMG.* | ||
7. Enter the new base name for the renamed files. | ||
Enter the new name: NewImageName | ||
8. The script will rename all matching files in the specified directory according to the new name format. | ||
## Requirements | ||
- Python 3.x | ||
- Basic understanding of file paths in your operating system. | ||
- Familiarity with regex patterns for filtering files. | ||
## License | ||
This script is open-source and free to use. Feel free to modify and distribute as needed. | ||
## Contributing | ||
If you'd like to contribute to this project, please open an issue or submit a pull request with your proposed changes or enhancements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import os | ||
import uuid | ||
|
||
# Initialize a list to store files containing the keyword | ||
contains = [] | ||
|
||
# Generate a unique identifier for this search session (not currently used) | ||
id_ = uuid.uuid4() | ||
|
||
# List of file extensions to search within | ||
extensions = [".txt", ".docx", ".pdf"] | ||
|
||
def change_direct(): | ||
# Prompt the user to enter the directory path | ||
path = input("Enter path: ") | ||
# Prompt the user to enter the keyword or phrase to search for | ||
keyword = input("Keyword/Phrase: ") | ||
# Start searching the specified folder | ||
search_folder(path, keyword) | ||
|
||
def search_folder(path, keyword): | ||
global contains # Declare the global variable to store found files | ||
# Check if the given path is a directory | ||
if os.path.isdir(path): | ||
# List all files and directories in the specified path | ||
files = os.listdir(path) | ||
# Iterate over each file in the directory | ||
for file in files: | ||
# Construct the full path to the file or directory | ||
full_path = os.path.join(path, file) | ||
|
||
# If the current path is a directory, recursively search inside it | ||
if os.path.isdir(full_path): | ||
search_folder(full_path, keyword) | ||
else: | ||
# Get the file extension and convert it to lowercase | ||
filetype = os.path.splitext(file)[-1].lower() | ||
# Check if the file type is in the allowed extensions | ||
if filetype in extensions: | ||
try: | ||
# Open the file and read its content | ||
with open(full_path, 'r', encoding='utf-8', errors='ignore') as file_: | ||
# Check if the keyword is found in the file content | ||
if keyword in file_.read(): | ||
contains.append(file) # Add the file to the list of found files | ||
print(keyword, "found in", file) # Print the result | ||
except Exception as e: | ||
# Print any errors encountered while reading the file | ||
print(f"Error reading {full_path}: {e}") | ||
else: | ||
# Print an error message if the provided path is not a directory | ||
print(f"{path} is not a directory.") | ||
|
||
# Start the process by calling the change_direct function | ||
change_direct() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# File Text Search Script | ||
## Description | ||
The `FileTextSearch.py` script is a utility designed to search for a specific keyword or phrase within text-based files located in a specified directory. The script searches through files with the following extensions: `.txt`, `.docx`, and `.pdf`. It can recursively search through subdirectories, making it easier to locate documents containing the desired information. | ||
## Features | ||
- Recursive Searching: The script can search within specified subdirectories. | ||
- Keyword Matching: Users can input a keyword or phrase to find within the text files. | ||
- Supported Formats: The script can read `.txt`, `.docx`, and `.pdf` file formats. | ||
## Usage | ||
1. Ensure you have Python installed on your machine. | ||
2. Download the `FileTextSearch.py` script. | ||
3. Open a terminal or command prompt and navigate to the directory where the script is located. | ||
4. Run the script using the following command: | ||
python FileTextSearch.py | ||
5. When prompted, enter the path of the directory you want to search. | ||
Enter path: /path/to/your/directory | ||
6. Enter the keyword or phrase you want to search for in the files. | ||
Keyword/Phrase: your_keyword | ||
7. The script will search through the specified directory and print out the names of any files where the keyword is found. | ||
## Requirements | ||
- Python 3.x | ||
- Basic understanding of file paths in your operating system. | ||
- The script will read text-based files, so ensure the files are not encrypted or password protected. | ||
## License | ||
This script is open-source and free to use. Feel free to modify and distribute as needed. | ||
## Contributing | ||
If you'd like to contribute to this project, please open an issue or submit a pull request with your proposed changes or enhancements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters