Move Some files from different locations

Tagged: 

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Luiz Fabiano Lucena Luiz Fabiano Lucena 4 months, 4 weeks ago.

  • Author
    Posts
  • #46267
    Profile photo of Luiz Fabiano Lucena
    Luiz Fabiano Lucena
    Participant

    Hi everyone, here is the challenge I've been struggling with.
    A user wants to move more than 26.000 files from 3 different network shared folders to another location.
    The point I've been stuck is that they don't want all the files to be copied, just some of them, from just some of the subfolders...

    Example:
    \\server\shared folder\MainFolder1\subfolder1\filename.pdf
    \\server\shared folder\MainFolder1\subfolder2\filename.xls
    \\server\shared folder\MainFolder2\subfolder1\filename.pdf
    \\server\shared folder\MainFolder2\subfolder1\filename2.pdf

    I couldn't find a logic to copy just some of the files and how to make PowerShell "reads" from different locations.

  • #46271
    Profile photo of Dan Potter
    Dan Potter
    Participant

    get-help Move-Item

  • #46304
    Profile photo of Luiz Fabiano Lucena
    Luiz Fabiano Lucena
    Participant

    🙂 Thanks, Dan. That was actually my first stop. Unfortunately there is no example like the one I need.

  • #46350
    Profile photo of Ian Kelso
    Ian Kelso
    Participant

    Luiz,

    Do you have a list of known filenames that will need moving, regardless of what folder/subfolder they reside in?

    Define your filenames in an array.
    Define your file shares in a different array.

    Iterate through your file shares, and for each file share do:

    get-childitem -recurse on the root of the share you're working with

    Check to see if the filename you're looking at is contained in the filename array. If it is, copy it somewhere.

    Something along these lines

    $Filenames = @("Filename1.xls","FileName2.txt","Filename3.jpg")
    $Fileshares = @("\\server1\sharename","server2\sharename")
    $DestinationPath = "\\DestinationServer\Sharename"
    
    ForEach ($Fileshare in $Fileshares) {
            $Files = Get-ChildItem $Fileshare | where {-not $_.PSIsContainer}
            ForEach ($File in $Files) {
                If $Filenames.Contains($File.Name) {
                    Copy-Item $File.FullName $DestinationPath
                }
            }
    }
    
  • #46360
    Profile photo of Luiz Fabiano Lucena
    Luiz Fabiano Lucena
    Participant

    So, I was getting at the same direction.
    I was getting something like this:

    $shareddrive_path = "\\server\filseshare\"
    $dest_path = "\\server\fileshare\"
    $files = Get-Content C:\files.txt
    
    foreach ($file in $files){
          Get-ChildItem -Recurse -Force $sharedrive_path -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -like "*$file*") } | Copy-Item -Destination $dest_path -Container -Confirm 
    }
    

    The first line on your example, would be possible to use Get-Content? The reason is that the file list is huge, 26.000 files.

    I'll give a shot using a block of files and let you know.

  • #46366
    Profile photo of Ian Kelso
    Ian Kelso
    Participant

    Yes, but with an array that large you really want to put it in an ArrayList() instead of a standard powershell array.

    $myArray = New-Object System.Collections.ArrayList
    [void] $myArray.Add(Get-Content $files)
    
  • #46625
    Profile photo of Luiz Fabiano Lucena
    Luiz Fabiano Lucena
    Participant

    Unfortunately is not working.
    So, explaining a little bit better.

    I have the source_path like this:
    "\\server_a\shared_folder\"
    list_of_folders\list_of_subfolders\list_of_filenames

    I have to move 26000 files from source_a to target_b;
    "\\target_b\new_shared_folder\"

    They request to move the files and try to preserve this structure:
    list_of_folders\list_of_subfolders\list_of_filenames

    Would be possible?

You must be logged in to reply to this topic.