Copy Items from one locations to another with matching names

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Peter Ščurka Peter Ščurka 5 days, 22 hours ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #54166
    Profile photo of Peter Ščurka
    Peter Ščurka
    Participant

    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

    #54167
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    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!

    #54169
    Profile photo of BenH
    BenH
    Participant

    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

    #54174
    Profile photo of Peter Ščurka
    Peter Ščurka
    Participant

    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

    #54176
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    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 1 week, 4 days ago by Profile photo of Olaf Soyk Olaf Soyk. Reason: correction of indentation
    #54191
    Profile photo of Peter Ščurka
    Peter Ščurka
    Participant

    Olaf,

    thank you very much. It works like a charm.

    #54340
    Profile photo of Peter Ščurka
    Peter Ščurka
    Participant

    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
    }
    }
    }
    
    #54343
    Profile photo of random commandline
    random commandline
    Participant

    Do you want to add the date to $targetfolder ?

    #54344
    Profile photo of Peter Ščurka
    Peter Ščurka
    Participant

    I need to ignore the date in the $targetfolder.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.