Skip to content

Commit 88ee0ad

Browse files
authored
Organize Files, Rename Files, and Search Text Files (#342)
* Added Pomodoro Timer * Update README.md * Update README.md * Added FileOrganizer,FileRenamer and FileTextSearch
1 parent 0671816 commit 88ee0ad

File tree

7 files changed

+190
-0
lines changed

7 files changed

+190
-0
lines changed

FileOrganizer/FileOrganizer.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
import shutil
3+
4+
# Prompt the user for the directory path to organize files
5+
path = input("Enter path: ")
6+
7+
# List all files in the specified directory
8+
files = os.listdir(path)
9+
10+
# Iterate through each file in the directory
11+
for file in files:
12+
# Split the filename and extension
13+
filename, extension = os.path.splitext(file)
14+
15+
# Remove the leading dot from the extension for folder naming
16+
extension = extension[1:]
17+
18+
# Check if a directory for the file extension already exists
19+
if os.path.exists(path + '/' + extension):
20+
# Move the file to the corresponding extension folder
21+
shutil.move(path + '/' + file, path + '/' + extension + '/' + file)
22+
else:
23+
# If the directory does not exist, create it
24+
os.makedirs(path + '/' + extension)
25+
# Move the file to the newly created extension folder
26+
shutil.move(path + '/' + file, path + '/' + extension + '/' + file)

FileOrganizer/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# File Organizer Script
2+
## Description
3+
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.
4+
## Features
5+
- *Automatic Organization:* Files are moved into subfolders named after their extensions (e.g., all `.jpg` files go into a folder named `jpg`).
6+
- *Dynamic Folder Creation:* If a folder for a specific file type doesn't exist, it will be created automatically.
7+
## Usage
8+
1. Ensure you have Python installed on your machine.
9+
2. Download the FileOrganizer.py script.
10+
3. Open a terminal or command prompt and navigate to the directory where the script is located.
11+
4. Run the script using the following command:
12+
python FileOrganizer.py
13+
5. When prompted, enter the path of the directory you want to organize.
14+
Enter path: /path/to/your/directory
15+
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.
16+
## Requirements
17+
- Python 3.x
18+
- Basic understanding of file paths in your operating system.
19+
## License
20+
This script is open-source and free to use. Feel free to modify and distribute as needed.
21+
## Contributing
22+
If you'd like to contribute to this project, please open an issue or submit a pull request with your proposed changes or enhancements.

FileRenamer/File_Renamer.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
import re
3+
4+
# Prompt the user for the directory path where files need to be renamed
5+
directory = input("Enter path: ")
6+
7+
# Provide instructions on how to use regex for searching specific files
8+
print("To look for specific files, enter what you know, using .* for any characters you don't know.")
9+
print("For example: IMG.* will filter files that start with IMG")
10+
11+
# Get the regex pattern to match files and the new base name for renaming
12+
pattern = input("Enter pattern: ")
13+
new_name = input("Enter the new name: ")
14+
15+
def rename_files(directory, pattern, new_name):
16+
# List all files in the specified directory
17+
files = os.listdir(directory)
18+
counter = 0 # Initialize a counter for unique naming
19+
20+
# Iterate over each file in the directory
21+
for file in files:
22+
# Check if the file matches the given pattern
23+
if re.match(pattern, file):
24+
# Get the file extension
25+
filetype = file.split('.')[-1]
26+
# Rename the file with the new base name and counter
27+
os.rename(directory + '/' + file, directory + '/' + new_name + str(counter) + '.' + filetype)
28+
counter += 1 # Increment the counter for the next file
29+
30+
# Call the function to rename files
31+
rename_files(directory, pattern, new_name)

FileRenamer/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# File Renamer Script
2+
## Description
3+
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.
4+
## Features
5+
- *Batch Renaming:* Rename multiple files that match a specified regex pattern.
6+
- *Custom Naming:* Users can provide a new base name for the renamed files, with a counter appended to ensure unique names.
7+
## Usage
8+
1. Ensure you have Python installed on your machine.
9+
2. Download the `FileRenamer.py` script.
10+
3. Open a terminal or command prompt and navigate to the directory where the script is located.
11+
4. Run the script using the following command:
12+
python `FileRenamer.py`
13+
5. When prompted, enter the path of the directory containing the files you want to rename.
14+
Enter path: /path/to/your/directory
15+
6. Enter the regex pattern for the files you want to rename. For example:
16+
Enter pattern: IMG.*
17+
7. Enter the new base name for the renamed files.
18+
Enter the new name: NewImageName
19+
8. The script will rename all matching files in the specified directory according to the new name format.
20+
## Requirements
21+
- Python 3.x
22+
- Basic understanding of file paths in your operating system.
23+
- Familiarity with regex patterns for filtering files.
24+
## License
25+
This script is open-source and free to use. Feel free to modify and distribute as needed.
26+
## Contributing
27+
If you'd like to contribute to this project, please open an issue or submit a pull request with your proposed changes or enhancements.

FileTextSearch/FileTextSearch.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
import uuid
3+
4+
# Initialize a list to store files containing the keyword
5+
contains = []
6+
7+
# Generate a unique identifier for this search session (not currently used)
8+
id_ = uuid.uuid4()
9+
10+
# List of file extensions to search within
11+
extensions = [".txt", ".docx", ".pdf"]
12+
13+
def change_direct():
14+
# Prompt the user to enter the directory path
15+
path = input("Enter path: ")
16+
# Prompt the user to enter the keyword or phrase to search for
17+
keyword = input("Keyword/Phrase: ")
18+
# Start searching the specified folder
19+
search_folder(path, keyword)
20+
21+
def search_folder(path, keyword):
22+
global contains # Declare the global variable to store found files
23+
# Check if the given path is a directory
24+
if os.path.isdir(path):
25+
# List all files and directories in the specified path
26+
files = os.listdir(path)
27+
# Iterate over each file in the directory
28+
for file in files:
29+
# Construct the full path to the file or directory
30+
full_path = os.path.join(path, file)
31+
32+
# If the current path is a directory, recursively search inside it
33+
if os.path.isdir(full_path):
34+
search_folder(full_path, keyword)
35+
else:
36+
# Get the file extension and convert it to lowercase
37+
filetype = os.path.splitext(file)[-1].lower()
38+
# Check if the file type is in the allowed extensions
39+
if filetype in extensions:
40+
try:
41+
# Open the file and read its content
42+
with open(full_path, 'r', encoding='utf-8', errors='ignore') as file_:
43+
# Check if the keyword is found in the file content
44+
if keyword in file_.read():
45+
contains.append(file) # Add the file to the list of found files
46+
print(keyword, "found in", file) # Print the result
47+
except Exception as e:
48+
# Print any errors encountered while reading the file
49+
print(f"Error reading {full_path}: {e}")
50+
else:
51+
# Print an error message if the provided path is not a directory
52+
print(f"{path} is not a directory.")
53+
54+
# Start the process by calling the change_direct function
55+
change_direct()

FileTextSearch/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# File Text Search Script
2+
## Description
3+
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.
4+
## Features
5+
- Recursive Searching: The script can search within specified subdirectories.
6+
- Keyword Matching: Users can input a keyword or phrase to find within the text files.
7+
- Supported Formats: The script can read `.txt`, `.docx`, and `.pdf` file formats.
8+
## Usage
9+
1. Ensure you have Python installed on your machine.
10+
2. Download the `FileTextSearch.py` script.
11+
3. Open a terminal or command prompt and navigate to the directory where the script is located.
12+
4. Run the script using the following command:
13+
python FileTextSearch.py
14+
5. When prompted, enter the path of the directory you want to search.
15+
Enter path: /path/to/your/directory
16+
6. Enter the keyword or phrase you want to search for in the files.
17+
Keyword/Phrase: your_keyword
18+
7. The script will search through the specified directory and print out the names of any files where the keyword is found.
19+
## Requirements
20+
- Python 3.x
21+
- Basic understanding of file paths in your operating system.
22+
- The script will read text-based files, so ensure the files are not encrypted or password protected.
23+
## License
24+
This script is open-source and free to use. Feel free to modify and distribute as needed.
25+
## Contributing
26+
If you'd like to contribute to this project, please open an issue or submit a pull request with your proposed changes or enhancements.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ More information on contributing and the general code of conduct for discussion
6565
| File Encryption Decryption | [File Encryption Decryption](https://github.com/DhanushNehru/Python-Scripts/tree/master/File%20Encryption%20Decryption) | Encrypts and Decrypts files using AES Algorithms for Security purposes. |
6666
| File Search | [File_search](https://github.com/debojit11/Python-Scripts/tree/master/File_Search) | A python script that searches a specified folder for all files, regardless of file type, within its directory and subdirectories.
6767
| Font Art | [Font Art](https://github.com/DhanushNehru/Python-Scripts/tree/master/Font%20Art) | Displays a font art using Python. |
68+
| File Organizer | [FileOrganizer](https://github.com/DhanushNehru/Python-Scripts/tree/master/FileOrganizer) | Organizes files into different folders according to their file type
69+
| File Renamer | [FileRenamer](https://github.com/DhanushNehru/Python-Scripts/tree/master/FileRenamer) | Bulk renames files with the same start/end
70+
| File Text Search | [FileTextSearch](https://github.com/DhanushNehru/Python-Scripts/tree/master/FileTextSearch) | Searches for a keyword/phrase accross different files
6871
| Freelance Helper Program | [freelance-helper](https://github.com/DhanushNehru/Python-Scripts/tree/master/freelance-help-program) | Takes an Excel file with working hours and calculates the payment. |
6972
| Get Hexcodes From Websites | [Get Hexcodes From Websites](https://github.com/DhanushNehru/Python-Scripts/tree/master/Get%20Hexcodes%20From%20Websites) | Generates a Python list containing Hexcodes from a website. |
7073
| Hand_Volume | [Hand_Volume](https://github.com/DhanushNehru/Python-Scripts/tree/master/Hand%20Volume) | Detects and tracks hand movements to control volume. |

0 commit comments

Comments
 (0)