Skip to content

Instantly share code, notes, and snippets.

View raducugheorghe's full-sized avatar

Raducu Gheorghe raducugheorghe

  • Romania
View GitHub Profile
@raducugheorghe
raducugheorghe / Ensure file name does not exist
Created December 8, 2014 09:55
[C#] Ensure file name is unique at a given path (puts <filename (count).ext>)
private string EnsureFileNameDoesNotExist(string storagePath, string fileName)
{
while (System.IO.File.Exists(Path.Combine(storagePath, fileName)))
{
var extension = Path.GetExtension(fileName);
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
var fileCountMarker = new Regex("\\((?<count>[0-9]+)\\)$");
var matchResult = fileCountMarker.Match(fileNameWithoutExtension);
@raducugheorghe
raducugheorghe / Sanitize folder name
Created December 9, 2014 12:51
[C#] Sanitize string to use as folder name
public static string SanitizeFolderName(string name)
{
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
var r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
return r.Replace(name, "");
}
@raducugheorghe
raducugheorghe / AutocompleteTextarea
Last active August 29, 2015 14:14
[Javascript] Autocomplete Textarea from localStorage
var AutocompleteTextarea = function () {
this.lastFocus = {};
};
AutocompleteTextarea.prototype = (function () {
function setSave(textareaId, storageKey) {
$("form").on("submit", function (ev) {
@raducugheorghe
raducugheorghe / NTP Setup
Last active March 1, 2016 08:50
[Powershell][Config] Set internet time server (NTP).ps1
Stop-Service w32time
w32tm /config /manualpeerlist:"0.ro.pool.ntp.org 1.ro.pool.ntp.org pool.ntp.org" /syncfromflags:MANUAL
Start-Service w32time
@raducugheorghe
raducugheorghe / Powershell app backup script
Created February 6, 2015 09:01
[Powershell] Backup website and database from script
Import-Module SQLPS -DisableNameChecking
Import-Module PSCX
$dt = Get-Date -Format yyyyMMddHHmmss
$dbname = 'DBNAME'
Backup-SqlDatabase -ServerInstance .\SQLEXPRESS -Database $dbname -BackupFile "C:\Backups\$($dbname)_db_$($dt).bak"
write-zip "C:\WebSites\SITENAME\*" "C:\Backups\$($dbname)_website_$($dt).zip"
@raducugheorghe
raducugheorghe / Backup app from sql job
Created February 6, 2015 09:05
[TransactSQL] Backup website and database from SQL job (uses winrar)
1. Map network drive
EXEC XP_CMDSHELL 'net use W: \\mainstore\Backup PASSWORD /user:USERNAME'
2. Backup Database
declare
@timestamp varchar(8),
@fileName varchar(1024)
@raducugheorghe
raducugheorghe / KnockoutJs Numeric Extender
Last active August 29, 2015 14:16
[Javascript][Knockout] Numeric extender
ko.extenders.numeric = function (target, params) {
var options = {
precision: 0,
allowNegative: false
};
$.extend(options, params);
//create a writable computed observable to intercept writes to our observable
var result = ko.pureComputed({
read: target, //always return the original observables value
@raducugheorghe
raducugheorghe / Mvc abstract factory model binder
Last active August 29, 2015 14:17
[C#][MVC] Abstract factory model binder
public class AbstractFactoryModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var modelType = bindingContext.ModelType;
if (modelType.IsAbstract)
{
var value = bindingContext.ModelName == "model" ? bindingContext.ValueProvider.GetValue("Type") : bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".Type");
if (value == null)
throw new ApplicationException("View does not contain Type");
@raducugheorghe
raducugheorghe / Mvc DateTime With EditFormat model binder
Created March 13, 2015 09:46
[C#][MVC] DateTime model binder that uses EditFormatString for parsing
public class DateTimeWithEditFormatModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType == typeof(DateTime?) || bindingContext.ModelType == typeof(DateTime))
{
var displayFormat = bindingContext.ModelMetadata.EditFormatString;
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
<#
.Synopsis
Trigger a TeamCity backup using the TeamCity REST API.
.Parameter username
Defines a TeamCity username which has authority to trigger backups.
.Parameter password
Defines the password for the user which will trigger the backup.
.Parameter baseUrl
Defines the URL to the TeamCity server (eg: http://teamcity.example.com).
If not set, the script will attempt to determine it from the TeamCity properties file.