How to copy files into corresponding folders?

q988988

New Member
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?Help.png
Help.png
 

livix07

Well-Known Member
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:

Neemobeer

Windows Forum Team
Staff member
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"
 

q988988

New Member
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.
 

Neemobeer

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

livix07

Well-Known Member
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.
 

Neemobeer

Windows Forum Team
Staff member
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:

q988988

New Member
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?
 

livix07

Well-Known Member
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:

q988988

New Member
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?
 

q988988

New Member
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

q988988

New Member
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.
 
Top