Skip to content

Instantly share code, notes, and snippets.

@chriscook
chriscook / move-files-into-subfolders
Created January 29, 2015 14:46
Loop through a series of folders, creating a sub-folder in each and moving any files already present in the folder into that subfolder
SET var_RootPath=D:\folder
SET var_DestinationFolder=newfolder
FOR /D %%G in (%var_RootPath%\*) do (
ECHO Folder is %%G
MD %%G\%var_DestinationFolder%
MOVE /Y %%G\* %%G\%var_DestinationFolder%\
)
@chriscook
chriscook / format-file-size.js
Created October 17, 2014 14:35
Format file size
/**
* Formats a file size as b, Kb, Mb etc
* @param (number) size File size in bytes
* @param (number) dec Number of decimal points to round to
*/
function formatFileSize(size, dec)
{
if(isNaN(dec)) dec = 2;
var multiply = Math.pow(10, dec);
var units = ['b','Kb','Mb','Gb','Tb','Pb'];
@chriscook
chriscook / database-lengths.txt
Last active January 2, 2016 16:59
My own standard lengths for database fields. This is a UK-centric list.
Title 4
Forename 50
Surname 50
Email address 255
Address line 1 (street/building) 100
Address line 2 (street) 100
Address line 3 (town) 50
Address line 4 (region) 50
Address line 5 (post code) 7
Telephone number 12
@chriscook
chriscook / explode-with-quotes.php
Created May 15, 2013 12:47
Explode a string, taking quotation marks into account.
<?php
// Based upon http://stackoverflow.com/a/2202489/189469
function explodeWithQuotes($string) {
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $string, $result);
return $result[0];
}
?>
@chriscook
chriscook / load-rss.php
Created January 18, 2013 10:03
Load an RSS feed.
<?php
$itemTag = 'item';
$tags = array(
'title',
'guid'
);
$url = 'http://feeds.bbci.co.uk/news/rss.xml';
$feed = rss_to_array($itemTag, $tags, $url);
$limit = 10;
@chriscook
chriscook / limit-input-to-numbers.js
Last active December 11, 2015 06:59
Limit input fields to only allow numeric values.
$('input').on('keypress', function (e) {
var code = e.which ? e.which : e.keyCode;
if ((code !== 46 && code > 31 && (code < 48 || code > 57)) || (code === 46 && $(this).val().indexOf('.') > -1)) {
e.preventDefault();
}
});
@chriscook
chriscook / get-parameter-by-name.js
Last active October 13, 2015 03:58
Retrieve a GET parameter by name.
function getParameterByName(name) {
var match = new RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
@chriscook
chriscook / set-cookie.js
Last active October 13, 2015 03:58
Set (or delete) cookie.
@chriscook
chriscook / get-cookie.js
Last active October 13, 2015 03:58
Get the value of a cookie.
@chriscook
chriscook / init-pdo-mysql.php
Last active October 13, 2015 03:57
Initialise a PDO MySQL connection.
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');
} catch (PDOException $e) {
die($e->getMessage());
}
?>