user56512
user56512

Reputation:

How to robocopy using regex matched folder from one server to another

I've a build system which uses robocopy to copy files from one system to our server, and to a specific path. The following has worked well till a new requirement was introduced:

robocopy local\dist \\server01\somepath\dist XF *.* /E

Now, we want to have a changing 'dist' name to include build information. For example, 'dist1', 'dist2', 'distabcd'. Anyhow, the point is, that the folder name is changing. How do I tell robocopy to match on any name beginning with 'dist', but copy to the correct full named dist folder on the remote server

robocopy local\dist* \\server01\somepath\[????] XF *.* /E

I have the option to use PowerShell commands to do this, assuming it may be able to copy to the server location. I know almost nothing about PowerShell, but welcome any tips.

Upvotes: 0

Views: 2262

Answers (1)

Shon Thomas
Shon Thomas

Reputation: 11

Powershell provides RegEx functionality with the '-match' and '-contains' operators. Here would be an example of what capturing changing directories would look like:

$localDirectory = "local\dist"
$directory = "\\server01\somepath\dist"    
$keyword = "dist"
$fileDirectory = Get-ChildItem -Path $directory -Recurse

foreach ($container in $fileDirectory)
{
    # -match is one of the RegEx functions we may utilize for this operation
    # e.g dist1.. dist2.. distabc.. adist.. mynewdistrubition
    if ($container -match $keyword)
    {
        try
        {                               
            Copy-Item -Path "$($directory)\$($container)" -Destination $localDirectory -Force -Recurse
        }
        catch [System.Exception] 
        {
            Write-Output $_.Exception
        }
    }
}

Upvotes: 1

Related Questions