Author Posts

July 8, 2016 at 4:50 pm

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.

July 8, 2016 at 4:52 pm

get-help Move-Item

July 8, 2016 at 6:11 pm

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

July 8, 2016 at 8:12 pm

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
            }
        }
}

July 8, 2016 at 8:27 pm

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.

July 8, 2016 at 11:47 pm

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)

July 12, 2016 at 7:12 pm

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?