Added more options to scanner

This commit is contained in:
Kevin Veen-Birkenbach 2023-09-14 07:41:37 +02:00
parent 3ac9e52206
commit fbfae9e400
3 changed files with 87 additions and 63 deletions

View File

@ -1,59 +1,57 @@
# Directory Content Scanner
Directory Content Scanner is a tool designed for the recursive scanning of directories, providing outputs for both file paths and their associated content. This tool is especially handy for rapid content exploration and debugging within nested file structures.
The Directory Content Scanner is a Python-based utility designed to recursively scan directories, presenting both file paths and their content. It's an invaluable tool for in-depth content exploration and debugging within nested file structures.
## Getting Started
## 🚀 Getting Started
### Prerequisites
### 📋 Prerequisites
- Bash shell (commonly available on Linux and macOS)
- Python 3.x
### Installation
### 🛠️ Installation
1. Clone this repository:
```
1. Clone the repository:
```bash
git clone https://github.com/kevinveenbirkenbach/directory-content-scanner.git
```
2. Navigate to the cloned directory:
```
```bash
cd directory-content-scanner
```
3. Make the script executable:
```
chmod +x scan.sh
```
## 📖 Usage
## Usage
Run the Python script, specifying the target directory and any desired filters:
Run the script with a directory as its argument:
```
./scan.sh /path/to/directory
```bash
python3 scan.py /path/to/directory [options]
```
This will print each file's path followed by its content, separated by equal signs for clarity.
### Options:
- `-f, --filetype`: Filter by file types (e.g., `.txt`, `.log`).
- `-i, --ignore`: Ignore files and folders containing specific strings.
- `--ignore-hidden`: Omit hidden directories from the scan.
- `-v, --verbose`: Enable verbose mode for detailed output.
## Warning
## ⚠️ Caution
Use this tool responsibly. If some of the target directory files are extensive, the script might produce a vast output. Always be aware of the volume of data you're dealing with before executing the script.
Exercise caution when scanning directories with large files. The script may produce extensive output. Always assess the data volume before executing the script.
## Contributing
## 🤝 Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Contributions via pull requests are welcome. For significant modifications, kindly open an issue first to discuss the proposed changes.
## Author
## ✍️ Author
Kevin Veen-Birkenbach
- 📧 Email: [kevin@veen.world](mailto:kevin@veen.world)
- 🌍 Website: [https://www.veen.world/](https://www.veen.world/)
**Kevin Veen-Birkenbach**
- 📧 [Email Kevin](mailto:kevin@veen.world)
- 🌐 [Website](https://www.veen.world/)
## License
## 📜 License
This project is licensed under the GNU Affero General Public License v3.0. The full license text is available in the `LICENSE` file of this repository.
This project is licensed under the GNU Affero General Public License v3.0. The complete license text is available in the `LICENSE` file.
## Acknowledgement
These scripts were created with the assistance of OpenAI's ChatGPT model. You can learn more about how they were created [here](https://chat.openai.com/share/71e9bc9e-d34a-4b03-bf68-4f9e994d156a).
## 🙏 Acknowledgements
Special thanks to OpenAI's ChatGPT model for assistance. Dive deeper into the creation process [here](https://chat.openai.com/share/71e9bc9e-d34a-4b03-bf68-4f9e994d156a) and [here](https://chat.openai.com/share/3fc66009-ff4e-425e-a4a3-fc703534885d).

58
scan.py Normal file
View File

@ -0,0 +1,58 @@
import os
import argparse
def filter_directories(dirs, ignore_strings, ignore_hidden, verbose):
"""Filter out directories based on ignore criteria."""
if ignore_hidden:
dirs[:] = [d for d in dirs if not d.startswith('.')]
dirs[:] = [d for d in dirs if not any(ig in d for ig in ignore_strings)]
if verbose:
print(f"Filtered directories: {dirs}")
def should_print_file(file, file_filters, ignore_strings):
"""Determine if a file should be printed based on filters."""
if file_filters:
if not any(file.endswith(file_type) for file_type in file_filters):
return False
return not any(ignore_str in file for ignore_str in ignore_strings)
def scan_and_print(directory, file_filters, ignore_strings, ignore_hidden, verbose):
for root, dirs, files in os.walk(directory):
filter_directories(dirs, ignore_strings, ignore_hidden, verbose)
for file in files:
if should_print_file(file, file_filters, ignore_strings):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r') as f:
content = f.read()
print(f"======== File Path: {file_path} ========")
print(content)
print("==================================\n")
except UnicodeDecodeError:
print(f"Warning: Could not read file due to encoding issues: {file_path}")
exit(1)
elif verbose:
print(f"Skipped file: {file}")
def main():
parser = argparse.ArgumentParser(description="Scan directories and print file contents.")
parser.add_argument("directories", nargs='+', help="List of directories to scan.")
parser.add_argument("-f", "--filetype", nargs='+', default=[], help="Filter by file types (e.g., .txt .log).")
parser.add_argument("-i", "--ignore", nargs='+', default=[], help="Ignore files and folders containing these strings.")
parser.add_argument("--ignore-hidden", action='store_true', help="Ignore hidden directories.")
parser.add_argument("-v", "--verbose", action='store_true', help="Enable verbose mode.")
args = parser.parse_args()
for directory in args.directories:
if not os.path.isdir(directory):
print(f"Error: {directory} is not a valid directory.")
exit(1)
scan_and_print(directory, args.filetype, args.ignore, args.ignore_hidden, args.verbose)
if __name__ == "__main__":
main()

32
scan.sh
View File

@ -1,32 +0,0 @@
#!/bin/bash
# Check if at least one directory argument is provided
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <directory1> [<directory2> ...]"
exit 1
fi
# Recursive function to scan folders and print file paths and content
function scan_and_print() {
local dir="$1"
for item in "$dir"/*; do
if [ -d "$item" ]; then
scan_and_print "$item"
elif [ -f "$item" ]; then
echo "======== File Path: $item ========"
cat "$item"
echo -e "\n==================================\n"
fi
done
}
# Loop through all provided directories and start the scan
for DIR in "$@"; do
# Ensure the directory exists
if [ ! -d "$DIR" ]; then
echo "Error: $DIR is not a valid directory."
exit 1
fi
scan_and_print "$DIR"
done