Sep-20-2023, 03:00 PM
I want to move files to folders having same name or having part of the file name. I have 60000 files, in the folder (D:/Source) . , there names look like 5000.pdf, 5000..pdf, 5000...pdf till 30000. I want to move all files containing 5000 in its name to the folder D:/Destination/5000. I tried the below script generated by ChatGPT, but does not work.
import os
import shutil
source_folder = "D:\Source"
destination_folder = "D:\Destination"
for filename in os.listdir(source_folder):
source_file = os.path.join(source_folder, filename)
if os.path.isfile(source_file) and destination_folder in filename:
shutil.move(source_file, os.path.join(destination_folder, filename))
import os
import shutil
source_folder = "D:\Source"
destination_folder = "D:\Destination"
for filename in os.listdir(source_folder):
source_file = os.path.join(source_folder, filename)
if os.path.isfile(source_file) and destination_folder in filename:
shutil.move(source_file, os.path.join(destination_folder, filename))