Author Posts

September 14, 2016 at 2:38 pm

Hello guys,

I have Folder Country with subfolders USA, Europe, Asia. In every subfolder I have cities e.g C:\Country\USA\New York
I would like to copy all the files what are in subfolders to another destination. D:\Country\U\USA\New York. I have a lot of these files and try to figure out how to copy the files to the matching destination. So everything what is here C:\Country\USA\New York\ to D:\Country\U\USA\New York. Is there any easy way how to do it with powershell?

Thanks a lot for any advice

September 14, 2016 at 2:51 pm

Peter,

define 'easy way'! 😉

You could start with get-help get-childitem, get-help about_foreach or get-help foreach-object, get-help copy-Item or maybe robocopy /?.

If you stuck on a certain point ask here for more.

Have a lot of fun!

September 14, 2016 at 3:27 pm

Peter,

This is how I might accomplish that with PowerShell:
1. Use Get-ChildItem -directory -recurse to get all of the folders I wanted to copy
2. Use the Split('\') method to separate the path of each folder
3. Build the new path from that split path
4. Use the new-item cmdlet to make a folder for each new path
5. Use get-childitem -file -recurse to get all of the files I wanted to copy
6. Follow the same steps for the files to determine where the files should be copied to
6. Use the copy-item cmdlet to copy them

September 15, 2016 at 10:14 am

I have following code. The code will go through all folders and subfolders I need, that's fine. I am struggling with definition of destination path. How can I achieve to find matching name in destination? For example at C:\ drive there is folder Country and subfolders Europe, USA, Asia. These subfolders contains other subfolders with City names. And I need that everything will be copied to the D:\ drive with matching names.On D:\ drive there is a different path USA D:\Country\U\USA\New York for Europe it is D:\Country\E\Europe\London etc.

$path = "C:\Country"
$folder = get-Childitem $path --
foreach ($folders in $folder)
{
$subfolders = get-Childitem "$path\$folders"
foreach ($subfolder in $subfolders)
{
copy-item -path $subfolder -destination
}

}

thanks

September 15, 2016 at 11:03 am

Peter,

I assume you want to kind of sort your folder, right?

Try this:

$Source = "C:\Country"
$Destination = "D:\Country"
$Folders = Get-ChildItem -Path $Source -Directory
foreach($Folder in $Folders)
{
    $FirstLetter = $Folder.Name.Substring(0,1)
    $TargetFolder = Join-Path -Path $Destination -ChildPath $FirstLetter
    if(-not (Test-Path -Path $TargetFolder ))
    {
        New-Item -Path $TargetFolder -ItemType Directory
    }
    Copy-Item -Path $Folder.FullName -Destination $TargetFolder -Recurse -Force
}
  • This reply was modified 2 years ago by  Olaf Soyk. Reason: correction of indentation

September 15, 2016 at 7:29 pm

Olaf,

thank you very much. It works like a charm.

September 20, 2016 at 8:01 pm

Hello guys,

one more thing, if I have for example on C:\Country\USA\New York but on "D:\Country\USA\New York – 20.9.2016", how can I achieve to compare the text only before the dash. I need to copy everything from "New York" to "New York – 20.9.2016" I tried IndexOf but it didn't work. Thanks a lot for any advice.

$Source = "C:\Country"
$Destination = "D:\Country"
$Folders = Get-ChildItem -Path $Source -Directory
foreach($Folder in $Folders)
{
$FirstLetter = $Folder.Name.Substring(0,1)
$character = $Folder.IndexOf('')
$NewFolder = $Folder.Name.Substring(0,$character)
$TargetFolder = "$Destination\$FirstLetter\$NewFolder"
$subfolders = get-Childitem "$Source\$folder"
foreach ($subfolder in $subfolders)
{    
Copy-Item $subfolder.FullName -Destination $TargetFolder -Recurse
}
}
}

September 20, 2016 at 8:23 pm

Do you want to add the date to $targetfolder ?

September 20, 2016 at 8:37 pm

I need to ignore the date in the $targetfolder.