Home Dashboard Directory Help
Search

PowerShell 2.0 - Cannot get "Copy-Item" to copy files if files have brackets in their filename by CJ Manning


Status: 

Active


2
0
Sign in
to vote
Type: Bug
ID: 776371
Opened: 1/11/2013 1:02:11 PM
Access Restriction: Public
0
Workaround(s)
view
2
User(s) can reproduce this bug

Description



Basically what I'm trying to do is:

I'm pulling information from a .csv spreadsheet that contains a list of files that I need. The problem is, the directory they are in contains 16,000+ files! I only need around 4600 of them (as the spreadsheet column only contains that many). I've actually gotten all that part of the code figured out... but where I'm running into issues is when it gets to the "Copy-Item" portion of my code. It runs fine until it hits files that have brackets as part of the filename, so of course it throws errors and doesn't give me what I need. How can I fix this? Mind you, the files needing/being copied CAN'T be renamed. I'm not sure how to use "-LiteralPath" here, if that's a solution. :( Here's my code below:

$Directory = gci D:\Documents\15075_32\
$Destination = "D:\CleanReview"
$ReviewSheet = import-csv 'C:\Users\7cm\Desktop\Internal Review - Emails Removed per Attorney Request.csv'
$BaseItem = foreach($li in $ReviewSheet){$li.Base}
foreach($File in $Directory){
    $File.BaseName
foreach($Item in $BaseItem){
    if($Item -like $File.BaseName){
        $Item
            Copy-Item $File.FullName $Destination -Force
}
}
}

So as you can see, it won't copy items that are declared since the files are going to be variable. If this was a single file, or if I could easily list them as an array I could take care of this... but even -LiteralPath won't work!
Details
Sign in to post a comment.
Posted by Mark Smith - Hyperoceanic on 10/3/2013 at 8:35 AM
I have the same problem, just using

Copy-Item     $source $destinationPath -recurse
Sign in to post a workaround.