Nate
Nate

Reputation: 901

How can I retrieve recursive directory name w/ Powershell?

I am attempting to use Powershell to automate the builds of projects using a cli compiler/linker. I want to place the script in the root directory of a project and have it check for all source files recursively and compile them, with the compiler output pointed at the same directory as the source files. I would also like to collect a list of the *.c as a comma-delimited variable as input to the linker. This is the typical situation:

//projects/build_script.ps
//projects/proj_a/ (contains a bunch of source files)
//projects/proj_b/ (contains a bunch of source files)

I wish to scan all the sub directories and compile the source for each *.c file. This is what I have so far:

$compilerLocation = "C:\Program Files (x86)\HI-TECH Software\PICC-18\PRO\9.63\bin\picc18.exe";
$args = "--runtime=default,+clear,+init,-keep";
$Dir = get-childitem C:\projects -recurse
$List = $Dir | where {$_.extension -eq ".c"}
$List | $compilerLocation + "-pass" + $_ + $args + "-output=" + $_.current-directory;

I realize that $_.current-directory isn't a real member, and I likely have other problems with the syntax. I apologize for the vagueness of my question, I am more than willing to further explain what may seem unclear.

Upvotes: 4

Views: 5624

Answers (1)

dugas
dugas

Reputation: 12443

Forgive me if I don't understand your exact requirement. Below is an example of recursively getting all files with the .txt extension, and then listing the file's name and the containing directory's name. To get this I access the DirectoryName property value on the FileInfo object. See FileInfo documentation for more information.

$x = Get-ChildItem . -Recurse -Include "*.txt"
$x | ForEach-Object {Write-Host "FileName: $($_.Name) `nDirectory: $($_.DirectoryName)"}

To take a stab at your current code:

$compilerLocation = "C:\Program Files (x86)\HI-TECH Software\PICC-18\PRO\9.63\bin\picc18.exe";
$args = "--runtime=default,+clear,+init,-keep";
$List = Get-ChildItem C:\project -Recurse -Include *.c
$List | ForEach-Object{#Call your commands for each fileinfo object}

Upvotes: 7

Related Questions