0

我正在编写一个 Powershell 脚本,它将获取用户指定文件夹中的所有文件并将它们一个一个传递到命令行,然后将生成的文件 (.CSV) 转储到另一个用户指定的文件夹中。我正在尝试对 CSV 使用与原始文件相同的文件名。该命令必须从特定文件夹 (C:\Program Files (x86)\Reporter) 执行。

这就是我到目前为止所拥有的,但我似乎无法让任何事情过去write-host- 对于循环、迭代、字符串,你可以命名它,Powershell 和我不是 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))

命令行命令必须是program.exe /rptcsv <file being created> <file being used>. 我需要它一次处理一个,等待前一个完成后再处理第二个。原始文件的数量会有所不同,所以我只需要它来处理它们都经历过的。我已经完成了批处理文件和一些 Python,但从未使用过 Powershell。我以前在 Python 中做过这个,只是被告知现在我们需要 Powershell ——这是我第一次尝试 Powershell,而且非常棒。这可以做到吗?

编辑:回答!!

这是工作代码:

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
}
4

1 回答 1

1

只要reporter.exe不异步运行(即在后台处理文件时不立即返回),这样的事情应该可以工作:

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
}
于 2013-06-03T18:28:07.910 回答