Skip to content

Instantly share code, notes, and snippets.

@craigtp
craigtp / LineCount.ps1
Last active March 14, 2022 18:24
Count number of non-blank lines in files with PowerShell
(dir -include *.cs -recurse | select-string .).Count
@craigtp
craigtp / BreakIntegerToConstituentDigits.cs
Created February 2, 2015 08:37
Break an integer number into it's constituent digits.
using System;
public class Program
{
public static void Main()
{
var result = BreakToDigits(56);
}
public static int[] BreakToDigits(int startingNumber)
@craigtp
craigtp / HGAutoUpdate
Created June 18, 2015 14:42
Recurse all folders beneath the current folder for Mercurial repositories and perform a "hg pull -u" on each repo.
dir -Directory -Recurse | ? { test-path (join-path $_.FullName ".hg\hgrc") } | % { $_.FullName; Set-Location -Path $_.FullName; iex "hg pull -u" }
@craigtp
craigtp / CleanUpTemporaryASPNETFiles.ps1
Last active April 7, 2021 22:44
Clean Up Temporary ASP.NET Files with Powershell
# Run this as Administrator
net stop w3svc
Get-ChildItem "C:\Windows\Microsoft.NET\Framework*\v*\Temporary ASP.NET Files" -Recurse | Remove-Item -Recurse -Force
net start w3svc
public static class StringExtentionMethods
{
public static string TruncateAtWord(this string input, int maxlength, bool addEllipsis)
{
if (input == null || input.Length <= maxlength) return input;
var ellipsisLengthDeduction = (addEllipsis ? 3 : 0);
var iLastSpace = input.LastIndexOf(" ", (maxlength - ellipsisLengthDeduction), StringComparison.Ordinal);
var iLength = 0;
if (iLastSpace < 0)
{
@craigtp
craigtp / RandomString.cs
Created April 29, 2016 12:16
RandomString - A class containing methods to produce some handy random string combinations.
using System;
using System.Text;
namespace RandomString
{
public class RandomString : IDisposable
{
// Can use the BetterRandom class here or just use the built-in System.Random class.
private BetterRandom random = new BetterRandom();
private const string alpha_selection = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
@craigtp
craigtp / GetWindowsProductKey.vbs
Created May 30, 2016 18:10
Retrieve Windows Product Key
Option Explicit
Dim objshell,path,DigitalID, Result
Set objshell = CreateObject("WScript.Shell")
'Set registry key path
Path = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
'Registry key value
DigitalID = objshell.RegRead(Path & "DigitalProductId")
Dim ProductName,ProductID,ProductKey,ProductData
'Get ProductName, ProductID, ProductKey
@craigtp
craigtp / NumericHelpers.cs
Last active April 21, 2017 16:48
Provides a function that will allow two imprecise floating point numbers to be compared for "near" equality and also an "IsNumeric" function.
public static class NumericHelpers
{
// Float point maths is whack. We can't check for absolute equality as floating point numbers are imprecise
// and are subject to rounding issues (See: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)
// Therefore, we can't check for absolute equality, but instead we have to need to check for values being
// "nearly" equal, which allows a small margin of error (i.e. something known as the "Epsilon" value for
// the given data type for this CPU that this code is running on).
public static bool NearlyEqual(double a, double b, double epsilon)
{
var absA = Math.Abs(a);
@craigtp
craigtp / touch.ps1
Created September 23, 2017 10:04
Windows Touch - Update Last Write / Modified Date of files recursively.
gci -recu -inc "*.*" | % { $_.LastWriteTime = Get-Date }

Keybase proof

I hereby claim:

  • I am craigtp on github.
  • I am craigtp (https://keybase.io/craigtp) on keybase.
  • I have a public key whose fingerprint is 8FBD 50F4 7D2C E90F 95D6 1B27 3965 4610 07EB B49F

To claim this, I am signing this object: