2
votes

I am trying to script a solution copying some files from one location to another..

I have a list of files in a .csv format, with headers

"ParentFolder, Name, FullName, lastwritetime."

Content of file is, which has hundreds of lines, and different paths, but same drive letter:

"X:\clients\A90\201AA3.05\","2012.08 RAP Proposal.xlsm","X:\clients\A90\201AA3.05\2012.08 RAP Proposal.xlsm","20/05/2016 10:41:08"

What i would like to do is copy the above.. "X:\clients\A90\201AA3.05\2012.08 RAP Proposal.xlsm" to a new location with differnet drive, but same directory structure. So in the csv file i have the filename and path, but am unsure how to split the drive from there and make a variable.

I have a foreach loop..

$ToCopy = Import-Csv "c:\temp\log.csv"
foreach($Line in $ToCopy)
{
    $FullPath = $Line.ParentFolder
    $File = $Line.Name
    $FullName = $Line.FullName   

    $file = "$FullPath\$FullName"
    $DestPath = Split-Path $FullPath -NoQualifier
    Copy-Item "$FullName" -Destination c:\test\$DestPath
}

Error message that i am getting is :

+ CategoryInfo          : NotSpecified: (:) [Copy-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Could not find a part of the path 'C:\test\clients\A90\Support\_index0901\'.
At line:9 char:9
+         Copy-Item "$FullName" -Destination c:\test\$DestPath
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Copy-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.CopyItemCommand
3

3 Answers

2
votes

You get the error because the directory structure of your target path probably does not exist

To solve that you can create a 'temporary' file with New-Item ... -Force which creates the missing directories if necessary and then overwrite that file with Copy-Item like so

$ToCopy = Import-Csv "c:\temp\log.csv"
foreach($Line in $ToCopy)
{
    $FullPath = $Line.ParentFolder
    $File = $Line.Name
    $FullName = $Line.FullName   

    $file = "$FullPath\$FullName"
    $DestPath = Split-Path $FullPath -NoQualifier
    $DestFile = c:\test\$DestPath
    New-Item -ItemType File -Force $DestFile
    Copy-Item "$FullName" -Destination $DestFile -Force
}
0
votes

You are trying to copy files into c:\test\ directory which does not exist. Create this directory before loop:

mkdir c:\test\

or, in case directory may exist

mkdir c:\test\ -Force
0
votes

You need to create the folders before attempting to copy files in them.

Here's a way to do it, simplified from what you have, but with an added line to take care of the folders' creation.

foreach($File in $ToCopy)
{
    $DestPath = Join-Path -Path 'c:\test' -ChildPath ( Split-Path $File.ParentFolder -NoQualifier )
    If ( -not ( Test-Path -Path $DestPath ) ) { New-Item -Path $DestPath -Force -ItemType Directory }
    Copy-Item $File.FullName -Destination $DestPath -WhatIf
}

(Be careful, I change the iteration variable from $Line to $File)