Question

$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
}
}
}
Était-ce utile?

La solution 2

Ok... after MUCH research through Google, I was able to find that even with the corrections in V3 of PS, and even with the -LiteralPath operator, it was still interpreting the filenames with wildcards within the copy. Through much trial an error on my own, I found that the operator within the IF statement was causing this. Even when -LiteralPath is used, it's within the IF statement and is forgotten when the criteria is true. So in order to combat this and then also make it read the filenames without wildcard interpretation I had to switch my -like to an -eq since -eq doesn't support wildcards. The next problem I ran into with this though was PS kept reporting it couldn't find the files as it was looking in the C drive! It would still do this even though I've declared the directory and even after I would use the declaration in shorthand within the copy command! PS seems to be very tempermental with this, and as a result I found that literally spelling it out completely for the script is what will acheive the results I needed! So to review, I'm taking a csv and reading one of the columns as that column is a list of filenames without extensions. I am comparing that list to a directory list, and when the column list of names matches the basenames within the directory... I needed it to copy those directory files to another folder on the D drive. Here is my working code! Thanks to Keith Hill (above), I started looking at it a little differently! Thanks for your help!

$Directory = gci -LiteralPath 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){
foreach($Item in $BaseItem){
    if($Item -eq $File.BaseName){
        $Item
            Copy-Item -LiteralPath D:\Documents\15075_32\$File -Destination $Destination -Force
}
}
}

Autres conseils

If you want to do bulk copy with the ability to retry files, I would consider using robocopy instead. If you still want to use Copy-Item use its -LiteralPath parameter for the source path.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top