powershell - 需要帮助使用 foreach 进行简单的 powershell 命令复制

标签 powershell copy invalid-argument

我是 powershell 的新手,这个问题将证明这一点。我正在尝试从命令行执行一个简单的任务,其中我有一个 txt 文件,其中包含以分号分隔的文件名,例如...

fnameA.ext;fnameB.ext;fnameC.ext;....

我正在尝试运行一个命令来解析这个文件,用分号分割内容,然后将每个文件的复制命令运行到所需的目录。

这是我正在运行的命令:
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

最佳答案

抵制制作单线的冲动,尤其是在您刚开始时。也就是说,问题是您需要将拆分的内容通过管道传输到另一个 ForEach-Object

试试这个:

$File = Get-Content .\MyFile.txt
$File | ForEach-Object {
    $_.Split(';') | ForEach-Object {
        Copy-Item -Path "$_" -Destination 'C:\destination'
    }
}

关于powershell - 需要帮助使用 foreach 进行简单的 powershell 命令复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6433567/

相关文章:

java - 线程中的异常 "main"java.lang.IndexOutOfBoundsException : Source does not fit in dest

postgresql - 如何在 Python 中使用 psql "\copy"?

c++ - 使用 gstreamer 时出现 Eclipse 无效参数错误

android - 无法在文件名中包含特殊字符的 SD 卡中保存文件

powershell - 搜索广告组成员

powershell - 从远程系统上的 Invoke-Command 捕获 Write-Host 输出和退出代码

C# 跟踪复制的文件

regex - PowerShell 正则表达式不工作

Azure DevOps 构建管道,托管代理失败

c++ - cudaMallocPitch 'Invalid Arguments' 尽管复制指南?