Implemented multiple paths

This commit is contained in:
Kevin Veen-Birkenbach 2023-11-17 12:06:33 +01:00
parent 5cbf98a926
commit e6b4f73eaa
2 changed files with 71 additions and 37 deletions

View File

@ -1,45 +1,70 @@
# bulk-string-replacer
bulk-string-replacer is a tool designed to traverse directories and perform bulk string replacement in filenames, folder names, and file contents. Whether you want to target hidden items, preview changes before execution, or recursively navigate through folders, this versatile utility has you covered.
# Bulk String Replacer
`bulk-string-replacer` is a Python-based command-line utility that allows for comprehensive search and replacement operations within file and folder names, as well as within file contents across specified directories. It's designed to handle bulk updates quickly and efficiently, with support for recursive directory traversal, hidden files, and a preview mode to review changes before they're applied.
## Author
Kevin Veen-Birkenbach
Kevin Veen-Birkenbach
- 📧 Email: [kevin@veen.world](mailto:kevin@veen.world)
- 🌍 Website: [https://www.veen.world/](https://www.veen.world/)
## Link to Original Conversation
## Background
For more context on how this tool was developed, you can [view the original conversation here](https://chat.openai.com/share/8567c240-3905-4521-b30e-04104015bb9b).
Learn more about the development and use cases of this tool in the [original conversation with the developer](https://chat.openai.com/share/cfdbc008-8374-47f8-8853-2e00ee27c959).
## Setup and Usage
## Getting Started
1. Clone the repository:
```bash
git clone https://github.com/kevinveenbirkenbach/bulk-string-replacer.git
```
### Prerequisites
2. Navigate to the cloned directory:
```bash
cd bulk-string-replacer
```
- Python 3.x
- Access to a command-line interface
3. Run the script with Python:
```bash
python replace_string.py [path] [old_string] [new_string] [options]
```
### Installation
### Options:
Clone the repository to your local machine:
- `--recursive`: Replace in all subdirectories and files.
- `--folder`: Replace in folder names.
- `--files`: Replace in file names.
- `--content`: Replace inside file contents.
- `--preview`: Preview changes without making actual replacements.
- `--verbose`: Verbose mode - view detailed outputs.
- `--hidden`: Target hidden files and folders.
```bash
git clone https://github.com/kevinveenbirkenbach/bulk-string-replacer.git
```
For more detailed options, refer to the inline script help or the aforementioned conversation.
Change into the project directory:
```bash
cd bulk-string-replacer
```
### Usage
Run the script with Python, specifying your target paths, the string to be replaced, and the new string:
```bash
python replace_string.py old_string --new-string new_string_value --options paths...
```
#### Options
- `old_string`: The string you want to search for and replace.
- `--new-string`: The string that will replace `old_string`. Defaults to an empty string if not specified.
- `--recursive`: Recursively process all subdirectories and files within the given paths.
- `--folder`: Replace string occurrences within folder names.
- `--files`: Replace string occurrences within file names.
- `--content`: Replace string occurrences within the contents of the files.
- `--preview`: Preview changes without applying them. No files will be modified.
- `--verbose`: Output detailed information during the execution of the script.
- `--hidden`: Include hidden files and directories in the operation.
Paths are specified at the end of the command, separated by spaces. For example:
```bash
python replace_string.py "search_string" --new-string "replacement_string" --recursive --verbose /path/to/first/directory /path/to/second/directory
```
Replace `/path/to/first/directory` and `/path/to/second/directory` with the actual paths you want to process.
## Contributions
Contributions are welcome! Please feel free to submit a pull request or open an issue on the GitHub repository.
## 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 open-sourced under the GNU Affero General Public License v3.0. See the `LICENSE` file for more details.

View File

@ -8,13 +8,17 @@ def replace_content(path, old_string, new_string, preview, verbose):
if old_string in content:
new_content = content.replace(old_string, new_string)
if verbose:
print(f"Replacing content in: {path}")
verbose(f"Replacing content in: {path}",verbose)
if not preview:
with open(path, 'w', encoding='utf-8') as f:
f.write(new_content)
def verbose(content,verbose):
if verbose:
print(content)
def process_directory(base_path, old_string, new_string, recursive, folder, files, content, preview, verbose, hidden):
# Eine Liste, um die Pfade der umzubenennenden Ordner zu speichern
directories_to_rename = []
@ -34,8 +38,7 @@ def process_directory(base_path, old_string, new_string, recursive, folder, file
if old_string in f:
old_path = os.path.join(root, f)
new_path = os.path.join(root, f.replace(old_string, new_string))
if verbose:
print(f"Renaming file from: {old_path} to: {new_path}")
verbose(f"Renaming file from: {old_path} to: {new_path}",verbose)
if not preview:
os.rename(old_path, new_path)
@ -52,16 +55,15 @@ def process_directory(base_path, old_string, new_string, recursive, folder, file
# Ordnernamen ändern nach dem os.walk() Durchlauf
for old_path, new_path in directories_to_rename:
if verbose:
print(f"Renaming directory from: {old_path} to: {new_path}")
verbose(f"Renaming directory from: {old_path} to: {new_path}")
if not preview:
os.rename(old_path, new_path)
def main():
parser = argparse.ArgumentParser(description="Replace strings in directories and files.")
parser.add_argument('path', help="Path in which replacements should be made.")
parser.add_argument('paths', nargs='+', help="Paths in which replacements should be made.")
parser.add_argument('old_string', help="The string to be replaced.")
parser.add_argument('new_string', nargs='?', default="", help="The string to replace with. Default is empty string.")
parser.add_argument('--new-string', dest='new_string', default="", help="The string to replace with. Default is empty string.")
parser.add_argument('--recursive', action='store_true', help="Replace in all subdirectories and files.")
parser.add_argument('--folder', action='store_true', help="Replace in folder names.")
parser.add_argument('--files', action='store_true', help="Replace in file names.")
@ -69,9 +71,16 @@ def main():
parser.add_argument('--preview', action='store_true', help="Preview changes without replacing.")
parser.add_argument('--verbose', action='store_true', help="Verbose mode.")
parser.add_argument('--hidden', action='store_true', help="Apply to hidden files and folders.")
args = parser.parse_args()
process_directory(args.path, args.old_string, args.new_string, args.recursive, args.folder, args.files, args.content, args.preview, args.verbose, args.hidden)
# Use os.path.expanduser to expand the tilde to the home directory
expanded_paths = [os.path.expanduser(path) for path in args.paths]
for path in expanded_paths:
verbose("Replacing in path: " + path,verbose)
process_directory(path, args.old_string, args.new_string, args.recursive, args.folder, args.files, args.content, args.preview, args.verbose, args.hidden)
if __name__ == "__main__":
main()