-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3151 from xinitd/Rename-Multiple-Files
Added script for rename multiple files in folders
- Loading branch information
Showing
2 changed files
with
141 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,113 @@ | ||
### [English](#EN) | ||
|
||
### [Russian](#RU) | ||
|
||
# EN | ||
|
||
### Script for rename multiple files | ||
|
||
Tested in Python version 3.11.2, but it must working in version 3.5 and above. | ||
|
||
### Run | ||
|
||
GNU\Linux: `$(which python3) main.py` | ||
|
||
Windows: `C:\\Path\\to\\Python3\\Python3.exe main.py` | ||
|
||
Folders structure: | ||
|
||
``` | ||
main.py | ||
root/ | ||
| | ||
|--- folder1 | ||
| | | ||
| |- file1.txt | ||
| |- file2.txt | ||
| |- file2.txt | ||
| | ||
|--- folder2 | ||
| | | ||
| |- file1.txt | ||
| |- file2.txt | ||
| |- file2.txt | ||
| | ||
|--- folder3 | ||
| | | ||
| |- file1.txt | ||
| |- file2.txt | ||
| |- file2.txt | ||
``` | ||
|
||
Output files structure: | ||
|
||
``` | ||
folder1/folder1-1.txt | ||
folder1/folder1-2.txt | ||
folder1/folder1-3.txt | ||
folder2/folder2-1.txt | ||
folder2/folder2-2.txt | ||
folder2/folder2-3.txt | ||
folder3/folder3-1.txt | ||
folder3/folder3-2.txt | ||
folder3/folder3-3.txt | ||
``` | ||
|
||
Maybe set outher symbol for `DESTINATION_FILE_DELIMITER` param or just leave blank `DESTINATION_FILE_DELIMITER = ''` | ||
|
||
# RU | ||
|
||
### Скрипт для переименования большого количества файлов | ||
|
||
Тестировался на Python версии 3.11.2, но должен работать на версиях выше 3.5. | ||
|
||
### Запуск | ||
|
||
GNU\Linux: `$(which python3) main.py` | ||
|
||
Windows: `C:\\Path\\to\\Python3\\Python3.exe main.py` | ||
|
||
Структура папок: | ||
|
||
``` | ||
main.py | ||
root/ | ||
| | ||
|--- папка1 | ||
| | | ||
| |- файл1.txt | ||
| |- файл2.txt | ||
| |- файл2.txt | ||
| | ||
|--- папка2 | ||
| | | ||
| |- файл1.txt | ||
| |- файл2.txt | ||
| |- файл2.txt | ||
| | ||
|--- папка3 | ||
| | | ||
| |- файл1.txt | ||
| |- файл2.txt | ||
| |- файл2.txt | ||
``` | ||
|
||
Скрипт переименует файлы в папках так: | ||
|
||
``` | ||
папка1/папка1-1.txt | ||
папка1/папка1-2.txt | ||
папка1/папка1-3.txt | ||
папка2/папка2-1.txt | ||
папка2/папка2-2.txt | ||
папка2/папка2-3.txt | ||
папка3/папка3-1.txt | ||
папка3/папка3-2.txt | ||
папка3/папка3-3.txt | ||
``` | ||
|
||
В случае необходимости можно поменять `DESTINATION_FILE_DELIMITER` на нужный символ или вообще оставить пустым `DESTINATION_FILE_DELIMITER = ''` |
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,28 @@ | ||
import os | ||
import platform | ||
|
||
|
||
ROOT_DIR = os.getcwd() + '/root' | ||
PATH_DELIMITER = '' | ||
DESTINATION_FILE_DELIMITER = '-' | ||
COUNTER = 0 | ||
|
||
|
||
if (platform.system() == 'Linux') or (platform.system() == 'Darwin'): | ||
PATH_DELIMITER = '/' | ||
elif (platform.system() == 'Windows'): | ||
PATH_DELIMITER = '\\' | ||
|
||
for current_dir in os.listdir(ROOT_DIR): | ||
subdir = ROOT_DIR + PATH_DELIMITER + current_dir | ||
if os.path.isdir(subdir): | ||
print('Now working with: "' + subdir + '" directory') | ||
COUNTER = 0 | ||
for current_file in os.listdir(subdir): | ||
COUNTER += 1 | ||
current_file_full_path = subdir + PATH_DELIMITER + current_file | ||
renamed_file_full_path = subdir + PATH_DELIMITER + current_dir + DESTINATION_FILE_DELIMITER + str(COUNTER) + '.' + current_file.split('.')[-1] | ||
try: | ||
os.rename(current_file_full_path, renamed_file_full_path) | ||
except Exception as e: | ||
print('Error occurred because: ' + e) |