Dav*_*ave 3 powershell copy invalid-argument
我是powershell的新手,这个问题将证明这一点.我正在从命令行尝试一个简单的任务,我有一个包含由分号分隔的文件名的txt文件,如...
fnameA.ext;fnameB.ext;fnameC.ext;....
Run Code Online (Sandbox Code Playgroud)
我正在尝试运行一个将解析此文件的命令,以分号分隔内容,然后为每个文件运行一个复制命令到所需的目录.
这是我正在运行的命令:
gc myfile.txt |% {$_.split(";") | copy $_ "C:\my\desired\directory"}
但是我为列表中的每个项目收到这样的错误...
Copy-Item : The input object cannot be bound to any parameters for the command either because the command does not take
pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:36
+ gc bla.txt |% {$_.split(";") | copy <<<< $_ "C:\my\desired\directory"}
+ CategoryInfo : InvalidArgument: (fileA.txt:String) [Copy-Item], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.CopyItemCommand
Run Code Online (Sandbox Code Playgroud)
抵制制造单线的冲动,特别是当你刚开始时.也就是说,问题是您需要将拆分内容传递给另一个ForEach-Object.
试试这个:
$File = Get-Content .\MyFile.txt
$File | ForEach-Object {
$_.Split(';') | ForEach-Object {
Copy-Item -Path "$_" -Destination 'C:\destination'
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9699 次 |
| 最近记录: |