Skip to content

Instantly share code, notes, and snippets.

@jonsagara
jonsagara / iPhoneMediaSorter.cs
Created October 13, 2013 23:26
Copy a flat directory of iPhone pictures and videos into a YYYYMM folder structure, based on the date the picture/video was taken.
class Program
{
private const string SourceDirectory = @"D:\iPhoneMedia";
private const string DestDirectory = @"D:\iPhoneMediaSorted";
static void Main(string[] args)
{
var sourceDir = new DirectoryInfo(SourceDirectory);
// Group by YYYYMM
@jonsagara
jonsagara / AddTempdbFiles.sql
Created January 18, 2014 21:00
Add more files to tempdb, make them the same size, and use a fixed size for autogrowth. This is especially important when READ_COMMITTED_SNAPSHOT, which causes SQL Server to the sh*t out of tempdb. Be sure to restart SQL Server after making this configuration change.
USE [master]
GO
ALTER DATABASE [tempdb] MODIFY FILE ( NAME = N'tempdev', SIZE = 102400KB , FILEGROWTH = 102400KB )
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev2', FILENAME = N'D:\Path\To\Your\DATA\tempdb2.mdf' , SIZE = 102400KB , FILEGROWTH = 102400KB )
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev3', FILENAME = N'D:\Path\To\Your\DATA\tempdb3.mdf' , SIZE = 102400KB , FILEGROWTH = 102400KB )
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev4', FILENAME = N'D:\Path\To\Your\DATA\tempdb4.mdf' , SIZE = 102400KB , FILEGROWTH = 102400KB )
GO
@jonsagara
jonsagara / OpmlStats.cs
Created May 24, 2014 17:31
Count OPML folders and feeds
using System;
using System.Linq;
using System.Xml.Linq;
namespace OpmlStats.ConsoleApp
{
class Program
{
private const string OpmlFile = @"C:\Path\To\Your\OpmlFile.opml";
@jonsagara
jonsagara / AnyKeyQuit.xml
Created October 30, 2014 16:30
Snippet for a "Press any key to quit..." prompt in a Visual Studio C# console application
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>AnyKeyQuit</Title>
<Author>Jon Sagara</Author>
<Description>Inserts a &quot;Press any key to quit...&quot; prompt</Description>
<Shortcut>quit</Shortcut>
</Header>
@jonsagara
jonsagara / Examples.cs
Created September 4, 2016 15:59 — forked from NickCraver/Examples.cs
SQL Exception handy helpers
// Here are some example usages for unimportant jobs we have where crap happens occasionally:
/// <summary>
/// intended for database commands that might deadlock, but are just "nice to haves"; we don't care if they deadlock every now and then
/// and we DON'T want them to block execution of the rest of /daily or /hourly! this returns -1 if deadlocked, otherwise, returns
/// the # of rows that the SQL command affected
/// </summary>
private int ExecuteIgnoreDeadlocks(string sql, object param = null, bool logDeadlock = false)
{
try
@jonsagara
jonsagara / QuickProfiler.cs
Last active September 21, 2016 21:14
Quick-and-dirty C# profiler. Really, it's just a wrapper for Stopwatch. Replace Console.WriteLine with whatever output makes sense.
public class QuickProfiler : IDisposable
{
private readonly Stopwatch _sw = Stopwatch.StartNew();
private readonly string _name;
public QuickProfiler(string name)
{
_name = name;
}
@jonsagara
jonsagara / CreateMediatRClasses.xml
Last active December 10, 2021 21:04
Inserts Query, Command, CommandResult, QueryHandler, and CommandHandler classes for MediatR 5.x. Save with the extension .snippet instead of .xml.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>CreateMediatRClasses</Title>
<Author>Jon Sagara</Author>
<Description>Inserts Query, Command, and QueryHandler classes for MediatR</Description>
<Shortcut>mediator</Shortcut>
</Header>
@jonsagara
jonsagara / Program.cs
Last active February 1, 2018 17:09
How many Friday the 13ths occur in a year?
void Main()
{
var startYear = 2000;
var endYear = DateTime.UtcNow.Year;
var spookyYears = new List<(int year, int occurrences)>();
for (var ixYear = startYear; ixYear <= endYear; ixYear++)
{
var spookyDaysThisYear = 0;
@jonsagara
jonsagara / PwnedPasswords.cs
Last active October 19, 2023 11:09
C# Pwned Passwords helper
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Pwnage
{
public static class PwnedPasswords
@jonsagara
jonsagara / start_elasticsearch.sh
Last active January 30, 2021 19:05
Start elasticsearch and kibana on macOS
#!/bin/bash
#
# Start elasticsearch in a new Terminal window.
#
echo "Starting elasticsearch in a new Terminal.app window..."
# See: https://stackoverflow.com/a/989357/731
osascript -e 'tell application "Terminal" to do script "~/Downloads/elasticsearch-7.10.2/bin/elasticsearch"'