Difference between revisions of "CSharp - CmdletDirGlobbing"

From PeformIQ Upgrade
Jump to navigation Jump to search
(Created page with "=Example= <pre> public static IEnumerable<string> CmdletDirGlobbing(string basePath, string glob) { Runspace runspace = RunspaceFactory.CreateRuns...")
 
 
Line 36: Line 36:
         } // CmdletDirGlobbing
         } // CmdletDirGlobbing
</pre>
</pre>
<pre>
        //---------------------------------------------------------------------
        public static IEnumerable<string> PdfPathGlobber(int testNumber)
        {
            return Utils.CmdletDirGlobbing(TestsWorkingFolder,
                String.Format(@"{0}\PDF\*.pdf", WorkingFolder(testNumber)));;
        } // PdfPathGlobber
</pre>


[[Category:CSharp]]
[[Category:CSharp]]

Latest revision as of 14:20, 11 February 2015

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