Skip to content

Commit 4250e34

Browse files
committed
batch rename whatsapp images
1 parent b424e40 commit 4250e34

File tree

1 file changed

+288
-0
lines changed

1 file changed

+288
-0
lines changed
+288
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"source": [
7+
"import os\n",
8+
"import re\n",
9+
"from datetime import datetime\n",
10+
"from IPython.display import display"
11+
],
12+
"outputs": [],
13+
"metadata": {}
14+
},
15+
{
16+
"cell_type": "markdown",
17+
"source": [
18+
"## Collect jpeg files in the PWD"
19+
],
20+
"metadata": {}
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 18,
25+
"source": [
26+
"files = [file for file in os.listdir(os.getcwd()) if file.endswith('.jpeg')]\n",
27+
"files"
28+
],
29+
"outputs": [
30+
{
31+
"output_type": "execute_result",
32+
"data": {
33+
"text/plain": [
34+
"['WhatsApp Image 2021-09-14 at 16.39.23.jpeg',\n",
35+
" 'WhatsApp Image 2021-09-14 at 16.38.42.jpeg',\n",
36+
" 'WhatsApp Image 2021-09-14 at 16.12.02.jpeg',\n",
37+
" 'WhatsApp Image 2021-09-14 at 15.57.20.jpeg',\n",
38+
" 'WhatsApp Image 2021-09-14 at 16.21.01.jpeg',\n",
39+
" 'WhatsApp Image 2021-09-14 at 16.37.10.jpeg',\n",
40+
" 'WhatsApp Image 2021-09-14 at 16.11.41.jpeg',\n",
41+
" 'WhatsApp Image 2021-09-14 at 16.25.40.jpeg']"
42+
]
43+
},
44+
"metadata": {},
45+
"execution_count": 18
46+
}
47+
],
48+
"metadata": {}
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"source": [
53+
"## *Filter out date and time from whatsapp file names*"
54+
],
55+
"metadata": {}
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 28,
60+
"source": [
61+
"pattern = \"(?P<date>\\d{4}-\\d{2}-\\d{1,2}) at (?P<time>\\d{1,2}\\.\\d{1,2}\\.\\d{1,2})\\.jpeg\"\n",
62+
"regex = re.compile(pattern)\n",
63+
"file_dt_time = []\n",
64+
"for file_name in files:\n",
65+
" try:\n",
66+
" match = regex.search(file_name)\n",
67+
" group_dict = match.groupdict()\n",
68+
" file_dt_time.append([\n",
69+
" file_name, \n",
70+
" datetime.strptime(f\"{group_dict['date']} {group_dict['time']}\", \"%Y-%m-%d %H.%M.%S\")\n",
71+
" ])\n",
72+
" except Exception as e:\n",
73+
" print(f\"skipped file: {file_name}\\n{e.__str__()}\") # skip files that are not in same format as whatsapp images.\n",
74+
"\n",
75+
"sorted_files = sorted(file_dt_time, key=lambda x: x[1])\n",
76+
"sorted_file_names = [file[0] for file in sorted_files]\n",
77+
"print(\"Sorted in increasing order: <past time first>\")\n",
78+
"sorted_file_names"
79+
],
80+
"outputs": [
81+
{
82+
"output_type": "stream",
83+
"name": "stdout",
84+
"text": [
85+
"Sorted in increasing order: <past time first>\n"
86+
]
87+
},
88+
{
89+
"output_type": "execute_result",
90+
"data": {
91+
"text/plain": [
92+
"['WhatsApp Image 2021-09-14 at 15.57.20.jpeg',\n",
93+
" 'WhatsApp Image 2021-09-14 at 16.11.41.jpeg',\n",
94+
" 'WhatsApp Image 2021-09-14 at 16.12.02.jpeg',\n",
95+
" 'WhatsApp Image 2021-09-14 at 16.21.01.jpeg',\n",
96+
" 'WhatsApp Image 2021-09-14 at 16.25.40.jpeg',\n",
97+
" 'WhatsApp Image 2021-09-14 at 16.37.10.jpeg',\n",
98+
" 'WhatsApp Image 2021-09-14 at 16.38.42.jpeg',\n",
99+
" 'WhatsApp Image 2021-09-14 at 16.39.23.jpeg']"
100+
]
101+
},
102+
"metadata": {},
103+
"execution_count": 28
104+
}
105+
],
106+
"metadata": {}
107+
},
108+
{
109+
"cell_type": "markdown",
110+
"source": [
111+
"## Batch-rename files (based on sorted order)"
112+
],
113+
"metadata": {}
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 30,
118+
"source": [
119+
"def rename_files(files, prefix=\"\"):\n",
120+
" for count, file in enumerate(files, 1):\n",
121+
" new_name = os.path.join(os.path.dirname(file), f\"{prefix}{count}.jpeg\")\n",
122+
" try:\n",
123+
" os.rename(file,new_name)\n",
124+
" print(\"[i] done!\")\n",
125+
" except Exception as e:\n",
126+
" print(\"[i] failed\")\n",
127+
" display(f\"{file} -> {new_name}\")\n",
128+
"\n",
129+
"rename_files([file[0] for file in sorted_files])\n"
130+
],
131+
"outputs": [
132+
{
133+
"output_type": "stream",
134+
"name": "stdout",
135+
"text": [
136+
"[i] done!\n"
137+
]
138+
},
139+
{
140+
"output_type": "display_data",
141+
"data": {
142+
"text/plain": [
143+
"'WhatsApp Image 2021-09-14 at 15.57.20.jpeg -> 1.jpeg'"
144+
]
145+
},
146+
"metadata": {}
147+
},
148+
{
149+
"output_type": "stream",
150+
"name": "stdout",
151+
"text": [
152+
"[i] done!\n"
153+
]
154+
},
155+
{
156+
"output_type": "display_data",
157+
"data": {
158+
"text/plain": [
159+
"'WhatsApp Image 2021-09-14 at 16.11.41.jpeg -> 2.jpeg'"
160+
]
161+
},
162+
"metadata": {}
163+
},
164+
{
165+
"output_type": "stream",
166+
"name": "stdout",
167+
"text": [
168+
"[i] done!\n"
169+
]
170+
},
171+
{
172+
"output_type": "display_data",
173+
"data": {
174+
"text/plain": [
175+
"'WhatsApp Image 2021-09-14 at 16.12.02.jpeg -> 3.jpeg'"
176+
]
177+
},
178+
"metadata": {}
179+
},
180+
{
181+
"output_type": "stream",
182+
"name": "stdout",
183+
"text": [
184+
"[i] done!\n"
185+
]
186+
},
187+
{
188+
"output_type": "display_data",
189+
"data": {
190+
"text/plain": [
191+
"'WhatsApp Image 2021-09-14 at 16.21.01.jpeg -> 4.jpeg'"
192+
]
193+
},
194+
"metadata": {}
195+
},
196+
{
197+
"output_type": "stream",
198+
"name": "stdout",
199+
"text": [
200+
"[i] done!\n"
201+
]
202+
},
203+
{
204+
"output_type": "display_data",
205+
"data": {
206+
"text/plain": [
207+
"'WhatsApp Image 2021-09-14 at 16.25.40.jpeg -> 5.jpeg'"
208+
]
209+
},
210+
"metadata": {}
211+
},
212+
{
213+
"output_type": "stream",
214+
"name": "stdout",
215+
"text": [
216+
"[i] done!\n"
217+
]
218+
},
219+
{
220+
"output_type": "display_data",
221+
"data": {
222+
"text/plain": [
223+
"'WhatsApp Image 2021-09-14 at 16.37.10.jpeg -> 6.jpeg'"
224+
]
225+
},
226+
"metadata": {}
227+
},
228+
{
229+
"output_type": "stream",
230+
"name": "stdout",
231+
"text": [
232+
"[i] done!\n"
233+
]
234+
},
235+
{
236+
"output_type": "display_data",
237+
"data": {
238+
"text/plain": [
239+
"'WhatsApp Image 2021-09-14 at 16.38.42.jpeg -> 7.jpeg'"
240+
]
241+
},
242+
"metadata": {}
243+
},
244+
{
245+
"output_type": "stream",
246+
"name": "stdout",
247+
"text": [
248+
"[i] done!\n"
249+
]
250+
},
251+
{
252+
"output_type": "display_data",
253+
"data": {
254+
"text/plain": [
255+
"'WhatsApp Image 2021-09-14 at 16.39.23.jpeg -> 8.jpeg'"
256+
]
257+
},
258+
"metadata": {}
259+
}
260+
],
261+
"metadata": {}
262+
}
263+
],
264+
"metadata": {
265+
"orig_nbformat": 4,
266+
"language_info": {
267+
"name": "python",
268+
"version": "3.8.10",
269+
"mimetype": "text/x-python",
270+
"codemirror_mode": {
271+
"name": "ipython",
272+
"version": 3
273+
},
274+
"pygments_lexer": "ipython3",
275+
"nbconvert_exporter": "python",
276+
"file_extension": ".py"
277+
},
278+
"kernelspec": {
279+
"name": "python3",
280+
"display_name": "Python 3.8.10 64-bit ('ds_env': venv)"
281+
},
282+
"interpreter": {
283+
"hash": "895ee0e21364f1282b39e6ac740520bfcf6a24eab6b7204953c2a0936af9aaf0"
284+
}
285+
},
286+
"nbformat": 4,
287+
"nbformat_minor": 2
288+
}

0 commit comments

Comments
 (0)