Copy-Item in PowerShell; The given path's format is not supported
I'm relatively new to PowerShell, and I was trying to copy files using a text file formatted like this:
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\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 err开发者_运维问答or. 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.
$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
}
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.
$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
}
Try this (inside the cycle):
$from = "C:\Users\" + $file[$i+1] + "\" + $file[$i]
$to = "C:\Users\AJ\Desktop\gdocs\"
Copy-Item $from $to
$from
and $to
are arguments for Copy-Item
cmdlet. They are bound to parameters -Path and -Destinattion. You can check this via this code:
Trace-Command -pshost -name parameterbinding {
Copy-Item $from $to
}
精彩评论