Python Forum
Move Files based on partial Match
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Move Files based on partial Match
#1
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))
Reply
#2
That's the trouble with AI, it requires a detailed description of the problem. The required level of detail is such that describing the program often takes more time than writing the program.

I think your approach is too limiting. You may as well make this a useful tool that you can use for moving other groups of files. Make a program that copies files matching a pattern to the specified directory. Something like "python copy.py pattern folder" where pattern would be something like "5000*.pdf". But wait, this is what the XCOPY command does in windows. Do you need to write a program at all? If XCOPY doesn't do it for you, take a look at robocopy.
Reply
#3
Use Code tags.
Your description most have been basic and leaving out the 5000 part.
An other problem if don't know Python,then it can be hard to fix or suggest a update from ChatGPT.

Something like this and using pathlib
from pathlib import Path
import shutil

dir_find = '5000'
source_folder = Path("G:/div_code/Source")
des_folder = Path("G:/div_code")
des_subfolder = des_folder / dir_find
for file in source_folder.glob(f"{dir_find}*"):
    # Create the des subfolder if it doesn't exist
    des_subfolder.mkdir(parents=True, exist_ok=True)
    des_file = des_subfolder / file.name
    # move file can also use .copy
    shutil.move(file, des_file)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 455 Feb-07-2024, 12:24 PM
Last Post: Viento
  partial functions before knowing the values mikisDeWitte 4 617 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Making a question answering chatbot based on the files I upload into python. Joejones 1 1,240 May-19-2023, 03:09 PM
Last Post: deanhystad
  Partial KEY search in dict klatlap 6 1,291 Mar-28-2023, 07:24 AM
Last Post: buran
  python move specific files from source to destination including duplicates mg24 3 1,103 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  remove partial duplicates from csv ledgreve 0 796 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Webhook, post_data, GPIO partial changes DigitalID 2 997 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  Optimal way to search partial correspondence in a large dict genny92c 0 1,005 Apr-22-2022, 10:20 AM
Last Post: genny92c
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,532 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Rename Files based on XML file klturi421 3 2,215 Oct-22-2021, 07:37 PM
Last Post: klturi421

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020