powershell - How to mirror files with given pattern (wildcard) -


i've got folder lots of installation files inside. take of these (found given pattern) , mirror them flash drive.

source:

d:\ccleaner124.exe d:\dfc221.exe d:\abc.exe 

destination:

h:\ccleaner123.exe h:\dfc221.exe 

pattern: (stored directly in script or @ .txt file)

d:\ccleaner*.exe d:\dfc*.exe 

result:

source: unchanged

destination:

h:\ccleaner124.exe // deleted 123 had lower version number in pattern [a-za-z_-]*([0-9]*) , copied 124 instead h:\dfc221.exe // current, kept same (no copy) 

i looked copy-item function properties, haven't found "mirror" parameter there. possible power shell?

bellow working script (i wrote simmliar sometime ago). put in copy.ps1, modify sourcedir, destdir, , 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     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -