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...
How about the file names? I just did a test... the code worked fine with the file names A,B,C but not for a bit more complex names. And the code also worked fine for the folders with existing non-xlsx files. SO the only issue left would be the file names. Is it possible to make some tweaks on the code so it works for the file name such as 22B_1 or 100_2?
 

This is weird...because all the files with simple names got moved over except the one shown in the screenshot...
1574914200658.png
 

Maybe there is a limitation in the code by default that only allows for the file name to have a certain number of character?
 

I tested a couple of more times...it just did not work the a more complicated file name rather than A,B,C... anyone can help out?
 

That's because the filter is set to xlsx only you'd need to add further types to filter on.
 

Back
Top