Skip to content

Instantly share code, notes, and snippets.

@emanuelgaspar
Last active May 8, 2018 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emanuelgaspar/54fd6bdb10efc8304d2377e53fc9bdaa to your computer and use it in GitHub Desktop.
Save emanuelgaspar/54fd6bdb10efc8304d2377e53fc9bdaa to your computer and use it in GitHub Desktop.
Script to find all 'UmbracoForms.config' files under a given path and write their versions. Results are written to Console and a text file named 'UmbracoFormsVersions.txt'
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UmbracoFormsChecker
{
class Program
{
static void Main(string[] args)
{
try
{
CheckVersions();
}
catch (Exception x)
{
Console.Write("Version check failed: ");
Console.WriteLine(x.Message);
}
Console.WriteLine("Hit Enter to close...");
Console.ReadLine();
}
private static void CheckVersions()
{
Console.WriteLine("Websites folder: ");
var path = Console.ReadLine();
var folder = new DirectoryInfo(path);
if (!folder.Exists)
{
Console.WriteLine("Folder does not exist.");
return;
}
var files = folder.EnumerateFiles("UmbracoForms.config", SearchOption.AllDirectories);
var outputPath = Environment.CurrentDirectory + "\\UmbracoFormsVersions.txt";
var output = new StringBuilder();
foreach (var file in files)
{
var lines = File.ReadLines(file.FullName);
var version = lines.FirstOrDefault(p => p.IndexOf("<settings ",
StringComparison.CurrentCultureIgnoreCase) >= 0);
if (version != null)
{
var properVersion = version
.Replace("<settings ", string.Empty)
.Replace(">", string.Empty)
.Replace("\"", string.Empty)
.Replace("version=", string.Empty)
.Trim();
var directory = file.FullName
.Replace(path, string.Empty)
.ReplaceString(path, string.Empty, StringComparison.CurrentCultureIgnoreCase)
.ReplaceString(@"\App_Plugins\UmbracoForms\UmbracoForms.config",
string.Empty, StringComparison.CurrentCultureIgnoreCase)
.TrimStart('\\');
var text = properVersion + "\t" + directory;
Console.WriteLine(text);
output.AppendLine(text);
}
}
File.WriteAllText(outputPath, output.ToString());
}
}
// taken from https://stackoverflow.com/questions/244531/is-there-an-alternative-to-string-replace-that-is-case-insensitive
public static class StringExtensions
{
public static string ReplaceString(this string originalString,
string oldValue, string newValue, StringComparison comparisonType)
{
int startIndex = 0;
while (true)
{
startIndex = originalString.IndexOf(oldValue, startIndex, comparisonType);
if (startIndex == -1)
break;
originalString = originalString.Substring(0, startIndex) + newValue +
originalString.Substring(startIndex + oldValue.Length);
startIndex += newValue.Length;
}
return originalString;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment