33from datetime import datetime , timedelta
44from 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
1111def 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
2121def 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 ( " \n Finished transferring files!!!" )
43+ print (f"{ count } file(s) transferred to: { dest_path } " )
0 commit comments