2011-06-14 110 views
3

我是比较新的PowerShell的,我是想用此格式的文本文件来复制文件:PowerShell中的Copy-Item;给定路径的格式不支持

file1.pdf 
dir1\dir2\dir3\dir 4 

file2.pdf 
dir1\dir5\dir7\di r8 

file3.pdf 
...etc. 

其中每一条目的第一行是一个文件名,第二个是C:\ Users的文件路径。例如,要在文件中的第一项的完整路径将是:

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

下面的代码是什么,我现在有,但我得到的错误:“不支持给定路径的格式。”之后的另一个错误告诉我它找不到路径,我认为这是第一个错误的结果。我已经玩了一下,并且我得到的印象是将这个字符串传递给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 

    } 

回答

5

您可以阅读的三行块的文件,加入了前两个元素,以形成通道,并使用复制项目要复制的文件。

$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 
} 
1

试试这个(周期内):

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

$from$toCopy-Item cmdlet的参数。它们绑定到参数-路径-Destinattion。您可以通过此代码检查:

Trace-Command -pshost -name parameterbinding { 
    Copy-Item $from $to 
} 
相关问题