Monthly Archives: December 2013

Powershell’s tricks

Suppose you have a deep and complex folder tree and you need a list of folders containing specific files –like CSS files; say you wanna minify them; what would you do?

a clean dir *.css /s /b returns the files’ bare name and path, but there’s no grouping there… Oh wait! we have powershell! Let’s use it!

ls -Filter *.css -Recurse -Name | group -Property directoryname

^^ this returns a Dictionary> where the key is the unified folder names where .css files reside. easy huh? OK now go and minify those files… DONE? Oh wait! what if you’ve missed some files? I won’t check back manually! I can’t actually act like a ASP:Repeater! here comes the Powershell again!

$list=ls -Filter *.css -Recurse -Exclude *.min.css -Name
$minlist=ls -Filter *.min.css -Recurse -Name
$list | where {$minlist -cnotcontains $_.Replace(".css", ".min.css")}

and yes! there was a file I missed!

Minified the file and now I’m good to go!

Bulk Backup All SQL databases

This is a simple SQL-T script to backup every database you have on an SQL-Server Engine.

USE master;
GO
DECLARE @dbName NVARCHAR(MAX);
DECLARE curDBs CURSOR FAST_FORWARD READ_ONLY FOR SELECT [s].[name] FROM [sys].[sysdatabases] AS s WHERE [s].[dbid] > 4;
OPEN curDBs
FETCH NEXT FROM curDBs INTO @dbName
WHILE @@FETCH_STATUS = 0
BEGIN
	DECLARE @cmd NVARCHAR(MAX);
	SET @cmd = N'BACKUP DATABASE ['+ @dbName +'] TO DISK = N''D:\xData\_backups\'+ @dbName +'.bak'' WITH NOFORMAT, NOINIT, NAME = ''' + @dbName + N'-Full Database Backup'', SKIP, NOREWIND, NOUNLOAD, STATS = 10';
	PRINT @cmd;
	EXEC [sys].[sp_executesql] @cmd;

	FETCH NEXT FROM curDBs INTO @dbName
END
CLOSE curDBs
DEALLOCATE curDBs