CSharp - CmdletDirGlobbing

From PeformIQ Upgrade
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


        //---------------------------------------------------------------------

        public static IEnumerable<string> PdfPathGlobber(int testNumber)
        {
            return Utils.CmdletDirGlobbing(TestsWorkingFolder,
                String.Format(@"{0}\PDF\*.pdf", WorkingFolder(testNumber)));;
        } // PdfPathGlobber