Author Posts

December 8, 2016 at 10:21 pm


I am attempting to conceptually understand how to do this and it is leaving me stumped. Still new to scripting in general but am eking my way forward.

I am attempting to run a commandline tool that accepts arguments. If I define variables as the arguments with static values it is simple enough to make it work. However, What I am attempting to do is the following:

(And yeah, I know there may be a better way to do this.. but I am learning as I go)

#List Log files in directory
$Dir = Get-ChildItem .\
$List1 = $Dir | where {$_.Extension -eq ".log"}
$List1 = $
#Obtain Output filenames
$reg1 = "SSE[0-9]{8}"
$List2 = $ | select-string -Pattern $reg1 -AllMatches | % { $_.Matches.Value }
$List2 = $List2 | ForEach-Object { $_+".conv" }

#Define command line values
$app = ".\Converter.exe"
$arg1 = '-c'
$arg2 = $list1
$arg3 = $list2

#Convert log files to conv files
$output = cmd /c $app $arg1 $arg2 > $arg3

So List1 contains all of the .log files in the directory. List2 removes the .log extension and changes it to .conv. These are both working. What I cannot figure out is how to make output iterate through each of the "pairs" so the EXE file then performs the action.

If output is a For/foreach type object how do I make the 2 arguments be the pipeline values and ensure they match up.

December 8, 2016 at 11:22 pm

Sorry, but I didn't really get what you try to do. But ...
Your code:

#List Log files in directory
$Dir = Get-ChildItem .\
$List1 = $Dir | where {$_.Extension -eq ".log"}
$List1 = $

Can be a little more readable:

$List1 = (Get-ChildItem ".\*.log").Name

Your '#Obtain Output filenames' does not make that much sense to me. You're using variables you did not define or fill in in the code you posted.
Then ... why do you do this:

$arg2 = $list1
$arg3 = $list2

.. just use the variables $list1 and $list2 where you need.

Do you really need cmd.exe?? You have Powershell!!

December 9, 2016 at 7:26 am

if you want to get all .log files with name matched some pattern,
generate .conv file matched with .log file and process with converter.exe
you should use pipeline

#you want 'all log files'
Get-ChildItem ./ -Filter '*.log' | Foreach-Object {
  # you want 'with name matched some pattern'
  if ($_.Name -match 'SSE[0-9]{8}') {
    #you want 'generate .conv file matched with .log file'
    $newname = $_.Name -replace '\.log$','.conv'
    #you want 'process with converter.exe'
    ./Converter.exe -c $_.Name > $newname

December 9, 2016 at 2:43 pm

You can accomplish this more or less in one line assuming that you don't need to do anything else with the file names in the rest of your script. When you're changing your extensions, don't forget to cast them to lower to accommodate files that might have all capital extensions like .LOG

get-childitem .\ -Filter '*.log' | where {$ -Match 'SSE[0-9]{8}'} | % {.\converter.exe -c "$($_.Name)" > "$($_.Name.tolower().Replace('.log','.conv'))"  }

December 9, 2016 at 6:06 pm

Olaf, Thanks for the tip.

>Then ... why do you do this:
Because I am new to this. So, even though I know some things are redundant I do them to keep things separate so I can work on each problem one at a time.

>Do you really need cmd.exe?? You have Powershell!!
Yes, the executable I am calling converts a very complex pipe delimited CSV that has no headers (And no real documentation on what the obscure values mean) into a proper CSV with over 100 headers that defines the values.

December 9, 2016 at 6:13 pm

Thanks Max,

Looks like you and Monte have slightly different methods however, I am following what you guys are doing. I will get to playing around with it.

December 9, 2016 at 6:29 pm

>Do you really need cmd.exe?? You have Powershell!!
Yes, the executable I am calling converts a very complex

What I meant was: You don't need cmd.exe anymore. Now you have Powershell!!! 😉 To run a command line tool you can do something like this:

Start-Process -FilePath '...path...\Converter.exe' -ArgumentList (-c $List1 > $List2)