windows PowerShell 中的复制项;不支持给定路径的格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6344561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:03:18  来源:igfitidea点击:

Copy-Item in PowerShell; The given path's format is not supported

windowsstringpowershell

提问by ajnatural

I'm relatively new to PowerShell, and I was trying to copy files using a text file formatted like this:

我对 PowerShell 比较陌生,我试图使用格式如下的文本文件复制文件:

file1.pdf
dir1\dir2\dir3\dir 4

file2.pdf
dir1\dir5\dir7\di r8

file3.pdf
...etc.

Where the first line of each entry is a file name, and the second is the file path from C:\Users. For example, the full path to the first entry in the file would be:

其中每个条目的第一行是文件名,第二行是来自 C:\Users 的文件路径。例如,文件中第一个条目的完整路径为:

C:\Users\dir1\dir2\dir3\dir 4\file1.pdf

The code below is what I currently have but I'm getting the error: 'The given path's format is not supported.' and another error after that telling me it can't find the path, which I assume a result of the first error. I've played around with it a bit, and I'm getting the impression that it's something to do with passing a string to Copy-Item.

下面的代码是我目前拥有的代码,但出现错误:“不支持给定路径的格式。” 之后的另一个错误告诉我它找不到路径,我认为这是第一个错误的结果。我玩过它一点,我得到的印象是它与将字符串传递给 Copy-Item 有关。

    $file = Get-Content C:\Users\AJ\Desktop\gdocs.txt
    for ($i = 0; $i -le $file.length - 1; $i+=3)
    {
        $copyCommand = "C:\Users\" + $file[$i+1] + "\" + $file[$i] 
        $copyCommand = $copyCommand +  " C:\Users\AJ\Desktop\gdocs\"
        $copyCommand
        Copy-Item $copyCommand

    }

回答by Shay Levy

You can read the file in chunks of three lines, join the first two elements to form a path and use copy-item to copy the files.

您可以分三行读取文件,将前两个元素连接起来形成路径,然后使用 copy-item 复制文件。

$to = "C:\Users\AJ\Desktop\gdocs\"

Get-Content C:\Users\AJ\Desktop\gdocs.txt -ReadCount 3 | foreach-object{
    $from = "C:\Users\" + (join-path $_[1] $_[0] )
    Copy-Item -Path $from -Destination $to
}

回答by stej

Try this (inside the cycle):

试试这个(在循环内):

$from = "C:\Users\" + $file[$i+1] + "\" + $file[$i] 
$to = "C:\Users\AJ\Desktop\gdocs\"
Copy-Item $from $to

$fromand $toare arguments for Copy-Itemcmdlet. They are bound to parameters -Pathand -Destinattion. You can check this via this code:

$from$toCopy-Itemcmdlet 的参数。它们绑定到参数-Path-Destination。您可以通过以下代码进行检查:

Trace-Command -pshost -name parameterbinding { 
   Copy-Item $from $to
}