CSharp - CmdletDirGlobbing

From PeformIQ Upgrade
Revision as of 14:14, 11 February 2015 by PeterHarding (talk | contribs) (Created page with "=Example= <pre> public static IEnumerable<string> CmdletDirGlobbing(string basePath, string glob) { Runspace runspace = RunspaceFactory.CreateRuns...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Example

        public static IEnumerable<string> CmdletDirGlobbing(string basePath, string glob)
        {
            Runspace runspace = RunspaceFactory.CreateRunspace();

            runspace.Open();

            // cd to basePath

            if (basePath != null)
            {
                var cdPipeline = runspace.CreatePipeline();
                var cdCommand = new Command("cd");

                cdCommand.Parameters.Add("Path", basePath);
                cdPipeline.Commands.Add(cdCommand);
                cdPipeline.Invoke(); // run the cmdlet
            }

            // run the "dir" cmdlet (e.g. "dir C:\*\*\*.txt" )

            Pipeline dirPipeline = runspace.CreatePipeline();

            var dirCommand = new Command("dir");

            dirCommand.Parameters.Add("Path", glob);
            dirPipeline.Commands.Add(dirCommand);

            Collection<PSObject> dirOutput = dirPipeline.Invoke();

            // for each found file

            return from psObject in dirOutput let a = psObject.Properties from psPropertyInfo in psObject.Properties where psPropertyInfo.Name == "FullName" select psPropertyInfo.Value.ToString();
        } // CmdletDirGlobbing