$filter = '*.dll', '*.pdb' $copied = 0; $skipped = 0; $failed = 0 $sourceFiles = $filter | % { Get-ChildItem $sourceDir -Filter $_ -Recurse } ` | sort @{expression={$_.FullName.Length}} -Descending ` | group Name -AsHashTable $targetFiles = $filter | % { Get-ChildItem $targetDir -Filter $_ -Recurse } ` | ? { $sourceFiles.ContainsKey($_.Name) } ` | sort FullName $targetFiles | % { $target = $_ $sourceFiles[$target.Name] | % { $source = $_ if ($target.FullName.EndsWith($source.FullName.Substring($sourceDir.Length), 'InvariantCultureIgnoreCase')) { if ($target.LastWriteTime.AddSeconds(2) -lt $source.LastWriteTime) { Write-Host $target.FullName try { Copy-Item $source.FullName $target.FullName -Force $copied++ } catch { Write-Error $_.Message $failed++ } } else { $skipped++ } return } } } Write-Host "$copied copied, $skipped skipped, $failed failed in $($watch.Elapsed)"
Sunday, November 24, 2013
Recursively search and replace files from one folder into another
Thursday, March 14, 2013
Programmatically configure a simple log4net trace appender
var hierarchy = (Hierarchy) LogManager.GetRepository(); var logger = (Logger) hierarchy.GetLogger(loggerName); logger.Level = Level.Debug; logger.AddAppender(new TraceAppender {Layout = new SimpleLayout()}); hierarchy.Configured = true;log4net
Monday, January 28, 2013
Convert an SQL like pattern into a regular expression pattern
var pattern = Regex.Replace( likePattern, @"[%_]|\[[^]]*\]|[^%_[]+", match => { if (match.Value == "%") { return ".*"; } if (match.Value == "_") { return "."; } if (match.Value.StartsWith("[") && match.Value.EndsWith("]")) { return match.Value; } return Regex.Escape(match.Value); });
Saturday, January 12, 2013
Load test a web site using asynchronous requests and the TPL
var queue = new Queue<Task>(); while (true) { if (queue.Count >= 50) { var task = queue.Dequeue(); task.Wait(); task.Dispose(); } var request = (HttpWebRequest) WebRequest.Create(url); var watch = Stopwatch.StartNew(); queue.Enqueue(Task.Factory .FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null) .ContinueWith(task => { using (var response = (HttpWebResponse) task.Result) { Console.WriteLine("{0}\t{1}\t{2}", response.StatusCode, response.ContentType, watch.Elapsed); } })); }Task Parallel Library (TPL)
Subscribe to:
Posts (Atom)