-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch_renamer.sh
executable file
·93 lines (71 loc) · 2.75 KB
/
batch_renamer.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
select_item_type() {
user_input=$(zenity --list --radiolist --title="Select Item Type" --text="Select the type of items to rename:" --column="Select" --column="Item Type" TRUE "Files" FALSE "Directories")
case $user_input in
"Files") item_type="file";;
"Directories") item_type="directory";;
*) echo "Invalid selection. Exiting." && exit 1;;
esac
}
rename_files() {
selected_files=$(zenity --file-selection --multiple --separator=" " --title "Select Files to Rename")
if [ -z "$selected_files" ]; then
zenity --error --text "No files selected. Exiting."
exit 1
fi
user_input=$(zenity --forms --title="Rename Files" --text="Enter rename options:" \
--add-entry="Prefix" \
--add-entry="Suffix" \
--add-entry="Incremental Order (e.g., %03d)" \
--add-entry="Counter Start Value (e.g., 1)")
IFS='|' read -r prefix suffix incremental_format start_counter <<< "$user_input"
counter=$start_counter
for file in $selected_files; do
file_extension="${file##*.}"
new_filename="${prefix}$(printf "$incremental_format" $counter)${suffix}.$file_extension"
if [ "$file" != "$new_filename" ]; then
mv "$file" "$(dirname "$file")/$new_filename"
fi
((counter++))
done
zenity --info --text "Files have been renamed successfully."
}
rename_directories() {
selected_directories=$(zenity --file-selection --multiple --directory --separator="|" --title "Select Directories to Rename")
if [ -z "$selected_directories" ]; then
zenity --error --text "No directories selected. Exiting."
exit 1
fi
user_input=$(zenity --forms --title="Rename Directories" --text="Enter rename options:" \
--add-entry="Prefix" \
--add-entry="Suffix" \
--add-entry="Incremental Order (e.g., %03d)" \
--add-entry="Counter Start Value (e.g., 1)")
IFS='|' read -r prefix suffix incremental_format start_counter <<< "$user_input"
counter=$start_counter
IFS='|' read -ra directories <<< "$selected_directories"
for directory in "${directories[@]}"; do
if [ ! -d "$directory" ]; then
echo "Skipping non-directory item: $directory"
continue
fi
new_directory_name="${prefix}$(printf "$incremental_format" $counter)${suffix}"
if [ "$directory" != "$(dirname "$directory")/$new_directory_name" ]; then
mv "$directory" "$(dirname "$directory")/$new_directory_name"
echo "Renamed Successfully: $directory to $new_directory_name"
else
echo "No renaming needed: $directory"
fi
((counter++))
done
zenity --info --text "Directories have been renamed successfully."
}
select_item_type
if [ "$item_type" == "file" ]; then
rename_files
elif [ "$item_type" == "directory" ]; then
rename_directories
else
echo "Unknown item type. Exiting."
exit 1
fi