Skip to content

Commit be70b4b

Browse files
committed
updates
1 parent a248cb7 commit be70b4b

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed
2.73 KB
Binary file not shown.

main.ipynb

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,60 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 1,
6+
"id": "95d2558a",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from transfer import transfer_recent_files"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 3,
616
"id": "ce53e9e0",
7-
"metadata": {
8-
"vscode": {
9-
"languageId": "plaintext"
17+
"metadata": {},
18+
"outputs": [
19+
{
20+
"name": "stdout",
21+
"output_type": "stream",
22+
"text": [
23+
"===================================\n",
24+
"Current Time: 2025-08-05 11:29:13\n",
25+
"Extracting files modified since: 2025-07-06 11:29:13\n",
26+
"===================================\n",
27+
"\n",
28+
"Finished transferring files!!!\n",
29+
"14 file(s) transferred to: D:\\1_Postdoc\\2_Projects\\202508_Data_Auto_Transfering\\Data_Auto_Transfer\n"
30+
]
1031
}
11-
},
12-
"outputs": [],
32+
],
1333
"source": [
14-
"from auto_transfer.transfer import transfer_recent_files\n",
34+
"source = r\"D:\\1_Postdoc\\2_Projects\\202508_Data_Auto_Transfering\\Data\"\n",
35+
"destination = r\"D:\\1_Postdoc\\2_Projects\\202508_Data_Auto_Transfering\\Data_Auto_Transfer\"\n",
36+
"days = 30\n",
1537
"\n",
16-
"source_sd = \"E:/SD_CARD\" # The mount point of the SD card\n",
17-
"destination = \"D:/DataBackup\" # Your local storage\n",
18-
"recent_days = 7 # Number of recent days of data to copy\n",
19-
"\n",
20-
"transfer_recent_files(source_sd, destination, recent_days)\n"
38+
"transfer_recent_files(source, destination, days)"
2139
]
2240
}
2341
],
2442
"metadata": {
43+
"kernelspec": {
44+
"display_name": "MachineLearn",
45+
"language": "python",
46+
"name": "python3"
47+
},
2548
"language_info": {
26-
"name": "python"
49+
"codemirror_mode": {
50+
"name": "ipython",
51+
"version": 3
52+
},
53+
"file_extension": ".py",
54+
"mimetype": "text/x-python",
55+
"name": "python",
56+
"nbconvert_exporter": "python",
57+
"pygments_lexer": "ipython3",
58+
"version": "3.12.7"
2759
}
2860
},
2961
"nbformat": 4,

transfer.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from datetime import datetime, timedelta
44
from pathlib import Path
55

6-
def is_recent(file_path, days):
7-
"""Check if file is modified within N days."""
6+
def is_recent(file_path, cutoff_time):
7+
"""Check if file was modified after the cutoff time."""
88
file_mod_time = datetime.fromtimestamp(file_path.stat().st_mtime)
9-
return datetime.now() - file_mod_time <= timedelta(days=days)
9+
return file_mod_time >= cutoff_time
1010

1111
def generate_unique_filename(dest_folder, filename):
12-
"""Generate a unique filename if one already exists."""
12+
"""Generate a unique filename if one already exists in the destination."""
1313
base, ext = os.path.splitext(filename)
1414
counter = 1
1515
new_name = filename
@@ -19,16 +19,25 @@ def generate_unique_filename(dest_folder, filename):
1919
return new_name
2020

2121
def transfer_recent_files(source_dir, dest_dir, days):
22+
# Get the current time and calculate the cutoff time
23+
current_time = datetime.now()
24+
cutoff_time = current_time - timedelta(days=days)
25+
26+
print("===================================")
27+
print(f"Current Time: {current_time.strftime('%Y-%m-%d %H:%M:%S')}")
28+
print(f"Extracting files modified since: {cutoff_time.strftime('%Y-%m-%d %H:%M:%S')}")
29+
print("===================================")
30+
2231
source_path = Path(source_dir)
2332
dest_path = Path(dest_dir)
2433
if not dest_path.exists():
2534
dest_path.mkdir(parents=True, exist_ok=True)
2635

2736
count = 0
2837
for file_path in source_path.rglob('*'):
29-
if file_path.is_file() and is_recent(file_path, days):
38+
if file_path.is_file() and is_recent(file_path, cutoff_time):
3039
dest_file_name = generate_unique_filename(dest_path, file_path.name)
3140
shutil.copy2(file_path, dest_path / dest_file_name)
3241
count += 1
33-
34-
print(f"{count} file(s) transferred to: {dest_path}")
42+
print("\nFinished transferring files!!!")
43+
print(f"{count} file(s) transferred to: {dest_path}")

0 commit comments

Comments
 (0)