how to copy all folders and contents by file name

Jimmy wilkins 1 Reputation point
2022-02-03T13:29:37.387+00:00

I need to copy files on a remote drive; the problem is I only need to copy folders and their contents when it contains DONE, but they have dates in the filename. MM-DD-YY DONE.
I have found a way to copy the entire parent folder, but can't remove all unneeded files that don't contain MM-DD-YY DONE so I have that idea on the back burner.

Ideally I would like to just be able to copy all of the MM-DD-YY DONE for T:\XX - foldername* to C:\XX - foldername\ to keep consistency
There are multiple files ending in DONE but it will only copy one or some, and then if it does copy one over it is missing the contents.

I would greatly appreciate any assistance please.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2022-02-03T15:48:44.13+00:00

    Why not use Get-ChildItem on the source directory and pipe the results into Where-Object and test the name (or fullname) for the value "done"? Pipe the results of that into your Copy-Item.

    If you already have a script, post it (use the "Code Sample" icon in the Format Bar -- it's the one with the "101 010" graphic).

    0 comments No comments

  2. Jimmy wilkins 1 Reputation point
    2022-02-03T16:14:05.493+00:00

    Thank you for responding! I have found this way to pull all files with MM-DD-YY DONE with "* DONE", and then once on the local / destination C:\tmp\XX - foldername I try to remove the folders that didn't only match the MM-DD-YY DONE / "* DONE". I just couldn't seem to be able to pull only the required files and had to include the Remove-Item in $strings

    $sourcefolder1 = "T:\XX - foldername"
    Set-Location $sourcefolder1
    $strings=@("z2021 *", "2021 ")
    get-childitem -recurse -include "
    DONE" | foreach-object -process {
    $source1 = $.FullName
    $dest1 = "C:\tmp\XX - foldername" + $Source1.Substring($sourcefolder1.Length, $
    .FullName.Length - $sourcefolder1.Length)
    $source1
    $dest1
    Copy-item -path $source1 -destination $dest1 -recurse -container
    }
    Set-Location "C:\tmp\XX - foldername"; get-childitem -Include ($strings) -Recurse -force | Remove-Item -Force –Recurse

    0 comments No comments