Automate movement between multiple source files to multiple destination folders

Eric Wood 0 Reputation points
2023-01-19T17:13:16.48+00:00

I have many client files that I am trying to move to destination folders, ideally all at once. I have a folder that contains a lot (hundreds to thousands) of client folders. Each client folder contains a file that I want to move into a subfolder within their folder. The file name and subfolder name are the same in each client's file. I am fairly sure that there is a way to automate this and iterate through each client until all of the files are moved. This would save me hours of manual clicking and dragging. I have some basic Python and C++ experience so I am comfortable trying to code a .bat file if I have an idea of where to start. I can't find any answers on Google so I am hoping someone here can help me. TYIA!

Windows for business Windows Client for IT Pros User experience Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jordi Rojas 271 Reputation points
    2023-01-19T18:41:21.7033333+00:00

    There are several ways you can automate this process of moving files from a source folder to a destination folder using Python or C++. Here's one way you can do it using Python:

    1. Use the os module to navigate to the source folder that contains the client folders.
    2. Use the os.listdir() function to get a list of all the client folders in the source folder.
    3. Use a for loop to iterate through each client folder.
    4. Inside the for loop, use the os.listdir() function again to get a list of all the files in the current client folder.
    5. Use another for loop to iterate through each file in the current client folder.
    6. Inside the second for loop, check if the file name matches the subfolder name.
    7. If the file name matches the subfolder name, use the shutil.move() function to move the file from the current client folder to the corresponding subfolder.
    8. Repeat steps 3-7 for all client folders and files.

    Here is the sample code that should help:

    import os
    import shutil
    
    source_folder = 'path/to/source/folder'
    
    for client_folder in os.listdir(source_folder):
        client_folder_path = os.path.join(source_folder, client_folder)
        for file in os.listdir(client_folder_path):
            if file == client_folder:
                subfolder_path = os.path.join(client_folder_path, file)
                file_path = os.path.join(client_folder_path, file)
                shutil.move(file_path, subfolder_path)
    
    

    You can run this script in your command prompt or terminal by navigating to the directory where your script is located and running the command python scriptname.py.

    It is important to test the code with a small number of files before running it on all the files and folders, as to make sure the code works as expected and also to avoid any accidental data loss.

    Alternatively, you can also use Windows Command Line or PowerShell to accomplish the same task, but the steps might be a bit different.

    Please let me know if you need further assistance.

    0 comments No comments

  2. Limitless Technology 44,746 Reputation points
    2023-01-23T08:51:15.7666667+00:00

    Hello there,

    On Windows, you can create a batch script that automatically moves files from your selected source folder to your target folder. This script can check the age of your files to ensure your files are only moved after certain days or months have passed since they were created.

    @echo off

    set X=Days

    set "source=SourceFolder"

    set "destination=DestinationFolder"

    robocopy "%source%" "%destination%" /mov /minage:%X%

    exit /b

    Double-clicking on your newly created batch file should move all your files from one folder to another. To automate this task, you need to put this file into an automated task.

    Task Scheduler will run your batch script at the specified time and frequency. When this happens, the script will start moving files from your source folder over to your destination folder.

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.