user2195503
user2195503

Reputation: 15

How to have Powershell process files in a folder and pass a CMD command with variables

I am writing a Powershell script that will take all the files in a user specified folder and pass them one by one to the command line, which will then dump the resulting files (.CSVs) into another user specified folder. I'm trying to use the same filename for the CSV as the original file. The command has to be executed from a specific folder ( C:\Program Files (x86)\Reporter).

This is what I have so far, but I can't seem to make anything work past write-host - for loops, iterations, strings, you name it, Powershell and I are no bueno.

pushd "C:\Program Files (x86)\Reporter\"

$filename = read-host "Enter the complete path of Original files"
$csvfiles = read-host "Enter the complete path for the CSVs"
write-host $filename
write-host $csvfiles
$reporter = "reporter.exe /rptcsv", $filename + ".csv", $filename

[$filename.string]::join(" ",(gci))

The command line command has to be program.exe /rptcsv <file being created> <file being used> . I need it to process them one at a time, waiting for the previous one to finish before processing the second one. The amount of original files will vary, so I just need it to process them they've all been gone through. I've done batch files and some Python, but have never used Powershell. I had previously done this in Python, only to be told that now we need Powershell instead- this is my first venture into Powershell and it's a doozy. Can this be done?

Edit: Answer!!

This is the working code:

pushd "C:\Program Files (x86)\Reporter"

$src = read-host "Enter the complete path of the folder with original files"
$dst = read-host "Enter the complete path of the folder for the CSVs"
write-host $src
write-host $dst
$reporter = 'reporter.exe /rptcsv'

Get-ChildItem $src | % {
  $outfile = Join-Path $dst ($_.Name + ".csv")
  cmd /c $reporter $outfile $_.FullName
}

Upvotes: 0

Views: 1905

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200373

As long as reporter.exe doesn't run asynchronously (i.e. does not return immediately while processing the file in the background) something like this should work:

pushd "C:\Program Files (x86)\Reporter"

$src = read-host "Enter the complete path of the folder with original files"
$dst = read-host "Enter the complete path of the folder for the CSVs"
write-host $src
write-host $dst

Get-ChildItem $src | % {
  $outfile = Join-Path $dst ($_.Name + ".csv")
  reporter.exe /rptcsv $outfile $_.FullName
}

Upvotes: 1

Related Questions