Windows 10 How to copy files into corresponding folders?

q988988

New Member
Joined
Nov 21, 2019
Messages
19
I have a list of csv files that have to be copy into a list of folders with same names. Rather than manually cutting each one into the corresponding folder, is there a trick of doing this in windows?
An image from 'How to copy files into corresponding folders?'. The images show three Excel files on the left being grouped into three folders named A, B, and C on the right.
An image from 'How to copy files into corresponding folders?'. The images show three Excel files on the left being grouped into three folders named A, B, and C on the right.
 

Last edited by a moderator:
Solution
Yeah a script would be the most efficient especially if you have a large number of files. In powershell it would be like this
Code:
$CopyFromPath = "C:\Some\Path"
$CopyToRootDirectory = "C:\Some\Other\Path"
$FileTypeFilter = "*.xlsx"

$Files = Get-ChildItem -Path $CopyFromPath -Filter $FileTypeFilter

foreach($File in $Files)
{
    $DirectoryName = "\$File.BaseName"
    $CopyToDirectory = "$CopyToRootDirectory$DirectoryName"

    if(-not(Test-Path $CopyToDirectory))
    {
        New-Item -Path $CopyToDirectory -ItemType Directory
    }
    Move-Item $File.FullName -Destination $CopyToDirectory -Forc
}

or it could be created as a function so that you can pass it different values for reuse
Code:
Function Move-ItemToDirectory
{
    param...
Most likely a script run via Command Prompt or PowerShell.

Here is a Python script that does what you want:

- you can get Python for Windows from here: Download Python
- create a file with extension .py (example: copy_files.py), copy the code below and replace the paths and the names of files and folders.
- let's say you want to copy files a,b,c (s0,s1,s2) to folders A,B,C (t0,t1,t2)
- you MUST provide the FULL PATH to the files and folders and make sure you have permissions to execute files and create folders

Code:
import shutil

t0 = "/home/liviu/A"
t1 = "/home/liviu/B"
t2 = "/home/liviu/C"

s0 = "/home/liviu/Desktop/a"
s1 = "/home/liviu/Desktop/b"
s2 = "/home/liviu/Desktop/c"

target = [t0,t1,t2]
source = [s0,s1,s2]

x = 0
for item in target:
    shutil.copy(source[x],target[x])
    x += 1

print("done!")

You can also do it like this:

Code:
import shutil

target = ["/home/liviu/A","/home/liviu/B","/home/liviu/C"]
source = ["/home/liviu/Desktop/a","/home/liviu/Desktop/b","/home/liviu/Desktop/c"]

x = 0
for item in target:
    shutil.copy(source[x],target[x])
    x += 1

print("done!")

Thank you for giving me an extra reason to learn programming!
 

Last edited:
Yeah a script would be the most efficient especially if you have a large number of files. In powershell it would be like this
Code:
$CopyFromPath = "C:\Some\Path"
$CopyToRootDirectory = "C:\Some\Other\Path"
$FileTypeFilter = "*.xlsx"

$Files = Get-ChildItem -Path $CopyFromPath -Filter $FileTypeFilter

foreach($File in $Files)
{
    $DirectoryName = "\$File.BaseName"
    $CopyToDirectory = "$CopyToRootDirectory$DirectoryName"

    if(-not(Test-Path $CopyToDirectory))
    {
        New-Item -Path $CopyToDirectory -ItemType Directory
    }
    Move-Item $File.FullName -Destination $CopyToDirectory -Forc
}

or it could be created as a function so that you can pass it different values for reuse
Code:
Function Move-ItemToDirectory
{
    param
    (
        [Parameter(Mandatory=$true)][string]$SourceDirectory,
        [Parameter(Mandatory=$true)][string]$DestinationDirectory,
        [string]$Filter="*.xlsx"
    )

    $CopyFromPath = $SourceDirectory
    $CopyToRootDirectory = $DestinationDirectory
    $FileTypeFilter = $Filter

    $Files = Get-ChildItem -Path $CopyFromPath -Filter $FileTypeFilter

    foreach($File in $Files)
    {
        $DirectoryName = "\$File.BaseName"
        $CopyToDirectory = "$CopyToRootDirectory$DirectoryName"

        if(-not(Test-Path $CopyToDirectory))
        {
            New-Item -Path $CopyToDirectory -ItemType Directory
        }
        Move-Item $File.FullName -Destination $CopyToDirectory -Forc
    }
}

Then you could load it and run Move-ItemDirectory -SourceDirectory = "C:\Some\Path" -DestinationDirectory = "C:\Some\Other\Path" -Filter "*.xlsx"
 

Solution
Thanks for the help! I am going to try Python first. Does either of these two methods have some sort of UI that allows me to choose the folder path each time before I run it? Because the folder path will be different every time.
 

You can take advantage of the .net openfiledialog in powershell. I'm not at a computer but I can write it out later.
 

Does either of these two methods have some sort of UI that allows me to choose the folder path each time before I run it? Because the folder path will be different every time.
Unfortunately you must type the full path to files and folders every time you use it.
I am a beginner in programming and most likely this is not the best code in Python.
You should ask or search on programming forums for better code.
 

Revised copy with two prompts.

Code:
Add-Type -AssemblyName System.Windows.Forms

$SourceLocation = New-Object System.Windows.Forms.FolderBrowserDialog
$SourceLocation.Description = "Select a Source Directory"
$SourceLocation.ShowDialog()

$DestinationLocation = New-Object System.Windows.Forms.FolderBrowserDialog
$DestinationLocation.Description = "Select a Destination Directory"
$DestinationLocation.ShowDialog()

$CopyFromPath = $SourceLocation.SelectedPath
$CopyToRootDirectory = $DestinationLocation.SelectedPath
$FileTypeFilter = "*.xlsx"

$Files = Get-ChildItem -Path $CopyFromPath -Filter $FileTypeFilter

foreach($File in $Files)
{
    $DirectoryName = "\$($File.BaseName)"
    $CopyToDirectory = "$CopyToRootDirectory$DirectoryName"

    if(-not(Test-Path $CopyToDirectory))
    {
        New-Item -Path $CopyToDirectory -ItemType Directory
    }
    $File.FullName
    $CopyToDirectory
    Move-Item $File.FullName -Destination $CopyToDirectory -Force
}
 

Last edited:
I created a text file (not sure how to create Python file) then changed the extension to ".py" and copied your code into it using Notepad++, but it did not seem to run as expected? Or I have to run it under a specific folder path? I would expect a prompt to show up to let me choose the folder/file paths?
 

I would expect a prompt to show up to let me choose the folder/file paths?
Nope, it doesn't give you the possibility to choose file/folder.
You must type in the full path to the file/folder and you can use copy/paste to make it easier.
 

Last edited:
Nope, it doesn't give you the possibility to choose file/folder.
You must type in the full path to the file/folder and you can use copy/paste to make it easier.
So all I need to do is to replace "Select a Source Directory" and "Select a Destination Directory" with "Z:\Files" and "Z:\Folders" respectively, then run the .py file?
 

Following is what I have in the py file, but it does not seem to work:

Add-Type -AssemblyName System.Windows.Forms

$SourceLocation = New-Object System.Windows.Forms.FolderBrowserDialog
$SourceLocation.Description = "Z:\Files"
$SourceLocation.ShowDialog()

$DestinationLocation = New-Object System.Windows.Forms.FolderBrowserDialog
$DestinationLocation.Description = "Z:\Folders"
$DestinationLocation.ShowDialog()

$CopyFromPath = $SourceDirectory.SelectedPath
$CopyToRootDirectory = $DestinationDirectory.SelectedPath
$FileTypeFilter = "*.xlsx"

$Files = Get-ChildItem -Path $CopyFromPath -Filter $FileTypeFilter

foreach($File in $Files)
{
$DirectoryName = "\$File.BaseName"
$CopyToDirectory = "$CopyToRootDirectory$DirectoryName"

if(-not(Test-Path $CopyToDirectory))
{
New-Item -Path $CopyToDirectory -ItemType Directory
}
Move-Item $File.FullName -Destination $CopyToDirectory -Forc
}
 

Attachments

The code I provided is powershell not python and should run as provided
 

I ran the code with PowerShell with two prompts showed up to choose two paths and ended with OK, but the files did not move across?
An image from 'How to copy files into corresponding folders?'. PowerShell script automates copying Excel files into new directories based on filename, shown with empty target folder.
 

Attachments

  • 1574724301095.webp
    1574724301095.webp
    83.1 KB · Views: 173
Last edited by a moderator:
A couple typos in my code. Code has been updated in reply 8
 

An image from 'How to copy files into corresponding folders?'. PowerShell script with error messages creating folders and moving .xlsx files by name.

It seems like it does not like the "} " at the end?
 

Last edited by a moderator:
It worked! Is is possible to copy the files into those folders rather than moving them?
 

Does this code work for the file name like 22B_1? Also the folder already contains files before I run the code. Will these two conditions affect the code? Because the code worked for the example I had previously (file A->Folder A, etc) but it did not seem to work for a bit more complicated case.
 

Back
Top