Skip to content

Commit

Permalink
version 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sank96 committed Feb 3, 2020
1 parent 920d0c6 commit 09b9327
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 43 deletions.
59 changes: 40 additions & 19 deletions README.md
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@

# Rename Beep File
This small script removes all the `+` characters and replaces them with a space contained in the file name.
This script is useful to rename all files inside a folder.
Indeed it replaces all the instances of a target string (`target`) in the base name of each file with the character specified (`char`).

It is a very simple tool but very useful when directly downloading zip files from folders from the [Beep site](https://beep.metid.polimi.it).
## Instructions
![GitHub release (latest by date)](https://img.shields.io/github/v/release/mett96/rename-beep-files)
![PyPI](https://img.shields.io/pypi/v/rename-beep-files?color=gre&logoColor=green)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/rename-beep-files)
![GitHub stars](https://img.shields.io/github/stars/mett96/rename-beep-files?style=social)

Install the package.
```
The default values are:
* target = `+`
* char = ` ` (space character)

It is a very simple tool but very useful when directly downloading zip files from folders from the [Beep site](https://beep.metid.polimi.it). But its usage can be extended.
## Instructions
Install the script.
```bash
pip install rename-beep-files
```

To show the help menu digit
```
rename-beep-files -h
Use it in the terminal
```bash
rename-beep-file
```

> usage: renamebeepfiles [-h] [-r] [paths [paths ...]]
>
>Process renames all the file into the target directory.
>
>positional arguments:
> paths Specify all the folders in which execute the renaming
>
>optional arguments:
> -h, --help show this help message and exit
> -r, --recursive Execute the rename action recursively into all
> subdirectories of targeted folder
You may include several options:
* recursive
* target
* char
* folder path

```bash
$ rename-beep-files -h ░▒▓ 2.12G  | 2.50 
usage: rename-beep-files [-h] [-r] [-t TARGET] [-c CHAR] [paths [paths ...]]

Process renames all the file into the target directory.

positional arguments:
paths Specify all the folders in which execute the renaming

optional arguments:
-h, --help show this help message and exit
-r, --recursive Execute the rename action recursively into all
subdirectories of targeted folder
-t TARGET, --target TARGET
Specify the character to replace
-c CHAR, --char CHAR Specify the character with which you want to replace

```
54 changes: 31 additions & 23 deletions rename-beep-files
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
#!/usr/bin/env python

# !/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import argparse


def reformat(path, recursive):
if not os.path.exists(path):
def reformat(path, recursive, char, target):
if not os.path.exists(path) or not os.path.isdir(path):
print('Error in path: {}'.format(path))
print('The specified path not exist')
return

os.chdir(path)
walks = os.walk(path)
dirpath, dirnames, filenames = next(walks)
dirpath, dirnames, filenames = next(os.walk(path))

print(dirpath)
print('-'*len(dirpath))
for file in filenames:
print(file)
i = file.rfind('.')
name = file[:i]
ext = file[i:]
if '+' in name:
rename = name.replace('+', ' ')
while rename[-1] == ' ':
rename = rename[:-1]
while rename[0] == ' ':
rename = rename[1:]
print(rename + ext)
os.rename(file, rename + ext)
name, ext = os.path.splitext(file)
print(name, '+', ext)

rename = name
while target in rename:
rename = rename.replace(target, char)

rename += ext
print(rename)
# os.rename(file, rename)
print()

print()

if recursive:
for directory in dirnames:
reformat(os.path.join(dirpath, directory), recursive)
reformat(os.path.join(dirpath, directory), recursive, char, target)

return

Expand All @@ -45,20 +42,31 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process renames all the file into the target directory.')
parser.add_argument('-r', '--recursive', action='store_true', default=False,
help='Execute the rename action recursively into all subdirectories of targeted folder')
parser.add_argument('-t', '--target', action='store', default='+', type=str,
help='Specify the character to replace')
parser.add_argument('-c', '--char', action='store', default=' ', type=str,
help='Specify the character with which you want to replace')
parser.add_argument('paths', type=str, nargs='*',
help='Specify all the folders in which execute the renaming')

args = parser.parse_args()

recursive = args.recursive
target = args.target
char = args.char
print('recursive:', recursive)
print('target:', target)
print('char:', char)

if args.paths:
paths = args.paths
print(paths)
else:
path = input('drag and drop the folder: \n')
path = path.replace('\\', '')
path = path[:-1]
# path = input('Insert the path of the target folder: \n')
path = '/Users/jarvis/Library/Mobile Documents/3L68KQB4HG~com~readdle~CommonDocuments/Documents/Universita/Machine Learning/Exercise sessions'
path = path.replace('\\', '').strip()
paths = [path]

for path in paths:
reformat(path, recursive)
reformat(path, recursive, char, target)
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import setuptools

CLIENT_VERSION = "1.0"
CLIENT_VERSION = "2.0"

with open("README.md", "r") as fh:
long_description = fh.read()
Expand Down

0 comments on commit 09b9327

Please sign in to comment.