4

I am currently trying to convert a series of batch files to powershell scripts. I would like to run a compiler for the source files that exist in a directory, recursively. The compiler requires a long list of arguments. The catch is, I want the arguments to be variable so I can change them as needed. This is a typical call from the batch file (simplified for readability and length):

"C:\PICC Compilers\picc18.exe" --pass1 "C:\Src Files\somefile.c" "-IC:\Include Files" "-IC:\Header Files" -P --runtime=default,+clear,+init,-keep,+download,+stackwarn,-config,+clib,-plib --opt=default,+asm,-speed,+space,9 --warn=0 --debugger=realice -Blarge --double=24 --cp=16 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" --OBJDIR="C:\Built Files" "--warnformat=Warning [%n] %f; %l.%c %s"

This command executes fine when included in a batch file, but I start getting errors when I copy and paste the command into powershell. This is only my second day working with powershell, but I have developed with .NET in the past. I have managed to scrape together the following attempt:

$srcFiles = Get-ChildItem . -Recurse -Include "*.c"
    $srcFiles | % {
    $argList = "--pass1 " + $_.FullName;
    $argList += "-IC:\Include Files -IC:\Header Files -P --runtime=default,+clear,+init,-keep,+download,+stackwarn,-config,+clib,-plib --opt=default,+asm,-speed,+space,9 --warn=0 --debugger=realice -Blarge --double=24 --cp=16 -g --asmlist '--errformat=Error   [%n] %f; %l.%c %s' '--msgformat=Advisory[%n] %s' '--warnformat=Warning [%n] %f; %l.%c %s"
    $argList += "--OBJDIR=" + $_.DirectoryName;
    &"C:\PICC Compilers\picc18.exe" $argList }

I know that I probably have multiple issues with the above code, namely how to pass arguments and how I am dealing with the quotes in the argument list. Incorrect as it is, it should illustrate what I am trying to achieve. Any suggestions on where to start?

2 Answers 2

7

Calling command line applications from PowerShell might be really tricky. Several weeks ago @Jaykul wrote great blog post The problem with calling legacy/native apps from PowerShell where he describes gotchas which people will meet in this situations. And there is of course solution too ;)

edit - correct url

The article is no more available, so it's only possible to see that through web.archive.org - see cached article

Sign up to request clarification or add additional context in comments.

2 Comments

I was actually having this problem, powershell was using commas to delineate arguments where the compiler program was using spaces. Great link.
Unfortunately the blog post is gone, making this answer useless
4

Make $arglist an array instead of a string. A single string will always be passed as a single argument which is what you don't want here.

3 Comments

Imho that's not enough - in original version -IC:\Header Files is in double quotes, whereas in Posh version it is not. This is one of problems that cause errors.
Joey is right, need to make this an array. It also wouldn't hurt to stop using spaces in your folder names. :)
@Jason: Folder names can contain spaces and whenever a tool cannot cope with that I consider it broken ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.