如何将Powershell进程文件放在文件夹中并传递带有变量的CMD命令
我正在编写一个Powershell脚本,该脚本将获取用户指定文件夹中的所有文件,并将它们逐个传递到命令行,然后命令行将生成的文件(.csv)转储到另一个用户指定的文件夹中。我正在尝试使用与原始文件相同的CSV文件名。必须从特定文件夹(C:\Program Files(x86)\Reporter)执行该命令 到目前为止,这就是我所拥有的,但我似乎无法让任何东西在如何将Powershell进程文件放在文件夹中并传递带有变量的CMD命令,powershell,cmd,Powershell,Cmd,我正在编写一个Powershell脚本,该脚本将获取用户指定文件夹中的所有文件,并将它们逐个传递到命令行,然后命令行将生成的文件(.csv)转储到另一个用户指定的文件夹中。我正在尝试使用与原始文件相同的CSV文件名。必须从特定文件夹(C:\Program Files(x86)\Reporter)执行该命令 到目前为止,这就是我所拥有的,但我似乎无法让任何东西在编写主机之后工作-对于循环、迭代、字符串,你可以说,Powershell和我都不是bueno pushd "C:\Program File
编写主机
之后工作-对于循环、迭代、字符串,你可以说,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/rptcv
。我需要它一次一个地处理它们,等待前一个完成,然后再处理第二个。原始文件的数量会有所不同,所以我只需要它来处理他们都经历过的文件。我做过批处理文件和一些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
}
只要
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
}
我试过了,但出现了以下错误:“reporter.exe”一词不能识别为cmdlet、函数、scri-pt文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。在C:\Users\boo\Desktop\testing.ps1:12 char:12+reporter.exe的连接路径:无法将参数绑定到参数“Path”,因为它为null。在C:\Users\boo\Desktop\testing.ps1:11 char:22+$outfile=加入路径第一个错误:
reporter.exe
不在$env:Path
中。将cd
(或pushd
)放入程序目录,或指定程序及其完整路径。第二个错误:$dst
未初始化。我应该将原始文件cd到目录中,还是应该从运行命令(C:\Program files(x86)\Reporter)所需的目录中?我问这个问题是因为在脚本的顶部,我推送了“C:\ProgramFiles(x86)\Reporter\”。(对不起,我不小心漏掉了那一行。)我已将其添加到原始帖子中。包含reporter.exe
的目录。