0

I've got a folder with a lots of installation files inside. I would like to take some of these (found by given pattern) and mirror them to the flash drive.

Source:

D:\ccleaner124.exe
D:\dfc221.exe
D:\abc.exe

Destination:

H:\ccleaner123.exe
H:\dfc221.exe

Pattern: (stored directly in the script or at some .txt file)

D:\ccleaner*.exe
D:\dfc*.exe

Result:

Source: unchanged

Destination:

H:\ccleaner124.exe // deleted 123 as it had lower version number in pattern [a-zA-Z_-]*([0-9]*) and copied 124 instead
H:\dfc221.exe // current, so kept the same as it was (no copy)

I looked up the Copy-Item function properties, but I haven't found anything like "mirror" parameter there. Is it even possible to do this with Power Shell?

1
  • Copy-Item is not a robocopy replacement. You'll have to write custom checks/filters. Commented Apr 18, 2013 at 2:44

1 Answer 1

0

Bellow working script (I wrote it something simmliar sometime ago). Put it in copy.ps1, modify sourceDir, destDir, and pattern array

function removeName($txt)
{
    $inputPattern = "^[a-z]+"
    $txt = [System.Text.RegularExpressions.Regex]::Replace($txt, $inputPattern, "");
    $inputPattern = "[.][a-z]+"
    $txt = [System.Text.RegularExpressions.Regex]::Replace($txt, $inputPattern, "");
    return [int]$txt;
}

$pattern = @('a*.txt', 'b*.txt')
$sourceDir = 'C:\source'
$destDir = 'C:\dest'

$pattern | foreach{
    $p = $_;
    $s = Get-ChildItem -Filter $p -Path $sourceDir;
    $sName = removeName $s.Name
    $d = Get-ChildItem -Filter $p -Path $destDir;
    $dName = removeName $d.Name
    if($sName -gt $dName)
    {
        Write-Host $s.Name 
        Write-Host $sName
        rm -Force $d.FullName
        Copy-item $s.FullName $destDir
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.