Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Utility to convert file includes to virtual includes

, 4 Nov 2007
A utility to convert file includes to virtual includes.
ConvertFile2Virtual_demo.zip
ConvertFile2Virtual_src.zip
ConvertFile2Virtual
ConvertFile2Virtual
ConvertFile2Virtual.csproj.user
Properties
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;

namespace ConvertFile2Virtual
{
    class Program
    {
        static ArrayList Files2Convert = new ArrayList();
        static string RootPath;

        static void Main(string[] args)
        {

            ShowProgramIntroMessage();

            if (!CheckCommandLineArgs(args))
            {
                ShowIncorrectArgsMessage();
                Environment.Exit(1); 
            };
            ReadCommandLineArgs(args);

            ShowWarningMessage();
            ConsoleKeyInfo key = Console.ReadKey(true);
            if (key.KeyChar.Equals('y')) {

                GetFiles(RootPath);
                foreach (string f in Files2Convert)
                {
                    Convert2Virtual(f);
                }
            }

            ShowExitMessage();
        }

        private static void ReadCommandLineArgs(string[] args)
        {
            RootPath = args[0];
        }

        private static void ShowExitMessage()
        {
            Console.WriteLine("bye!");
        }

        private static void ShowProgramIntroMessage()
        {
            Console.WriteLine("ConvertFile2Virtual v1.0");
        }

        private static void ShowWarningMessage()
        {
            Console.WriteLine("Warning! This utility changes the contents of the files found in the folder you specified.");
            Console.WriteLine("Pleas make sure that you back-up your files before trying it.");

            Console.WriteLine("Are you sure that you want to continue? Press y to continue, n to cancel.");
        }

        private static void ShowIncorrectArgsMessage()
        {
            Console.WriteLine("Missing or incorrect root path argument!");
            Console.WriteLine("Usage: ConvertFile2Virtual.exe path");
            Console.WriteLine("path = Full path to root folder. Remember to include a trailing slash, and to surround with double quotes if it contains spaces.");
        }

        static void Convert2Virtual(string filePath)
        {
            string includeRegex = "include file=\"(\\.\\./.*?)\"";

            string content = ReadFile(filePath);
            MatchCollection mc = Regex.Matches(content, includeRegex);

            if (mc.Count > 0)
            {
                Console.WriteLine("\nProcessing file: {0}", filePath);
                foreach (Match match in mc)
                {
                    string includePath = match.Groups[1].Value;
                    string newincludePath = ConvertRelative2Absolute(includePath, filePath);
                    content = Regex.Replace(content, "include file=\"" + includePath + "\"", "include virtual=\"" + newincludePath + "\"");
                    WriteFile(filePath, content);

                    Console.WriteLine(includePath + " --> " + newincludePath);
                }
            }
            else {
                File.Delete(filePath);
            }

            


        }

        private static void WriteFile(string filePath, string content)
        {
            StreamWriter writer = new StreamWriter(filePath);
            writer.Write(content);
            writer.Close();
        }

        private static string ConvertRelative2Absolute(string includePath, string filePath)
        {
            string parentPathRegex = "\\.\\./";
            int upPathCount = Regex.Matches(includePath, parentPathRegex).Count;
            string virtualPath = FilepathToVirtual(filePath);

            if (Regex.Matches(virtualPath, "/").Count >= upPathCount + 1)
            {
                for (int i = 0; i < upPathCount+1; i++)
                {
                    virtualPath = virtualPath.Substring(0, virtualPath.LastIndexOf("/"));
                }
            }
            else
            {
                throw new Exception("Error found. Cannot convert from relative path to absolute. Cannot use a leading .. to exit above the top directory");

            }

            virtualPath = virtualPath + "/" + includePath.Replace("../","");

            return virtualPath;

        }

        private static string ReadFile(string filePath)
        {
            StreamReader reader = new StreamReader(filePath);
            string content = reader.ReadToEnd();
            reader.Close();
            return content;
        }



        static bool CheckCommandLineArgs(string[] args)
        {
            if (args.Length != 1)
            {
                return false;
            }

            if (!Directory.Exists(args[0]))
            {
                return false;
            }
            return true;
        }


        static string FilepathToVirtual(string Path)
        {
            string virtualPath;

            virtualPath = Path;
            virtualPath = virtualPath.Replace(RootPath, "/");
            virtualPath = virtualPath.Replace("\\", "/");
            return virtualPath;

        }

        static void GetFiles(string sDir)
        {

            foreach (string d in Directory.GetDirectories(sDir))
            {
                foreach (string f in Directory.GetFiles(d, "*.asp"))
                {
                    Files2Convert.Add(f);
                }
                GetFiles(d);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bruno Gazzera
Web Developer
Unknown
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 4 Nov 2007
Article Copyright 2007 by Bruno Gazzera
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid