0

OK So I've got a list of files I'm trying to copy from one folder to another.

I have a CSV with a list of partial file names eg: ABCD-EFGH

The files I want copied are in the destination folder named ABCD-EFGH-1.pdf, ABCD-EFGH-2.dxf, ABCD-EFGH-3.dwg etc

If there is a match I'm trying to copy all the files that match the partial name.

Below code doesn't throw errors, it just copies EVERYTHING from the source folder. If I remove the * from the -Path it just copies the folder itself rather than the contents of the folder. What am I missing?

$drawing_list = Import-Csv $importfile

#$drawing | Get-Member -MemberType NoteProperty 

# Loop through all the records in the CSV
foreach($drawing in $drawing_list)
{
        
    # copy all items from source folder to destination that match start of drawing name
    Copy-Item -Path "$($sourceFolderName)*" -Filter "$($drawing.Name)*" -Destination $targetFolderName 
                  
}

1 Answer 1

0

Try $drawing.GetType() it isn't just your string but a [PSCustomObject].
With

$drawing | Get-Member -MemberType NoteProperty 

you get the properties that you can use. They are the same as the header of the CSV.
If the first row in your csv is not supposed to be a header, you can define it yourself with

Import-Csv $importfile -Header 'YourHeader','NextColumn'

You need to use the property of $drawing in the Copy-Item instead of the whole $drawing object:

... -Include "$($drawing.FileName)*"  ...
Sign up to request clarification or add additional context in comments.

2 Comments

This is looking promising! I'm getting a different error now, but I'm pretty sure I know how to fix this one: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
OK thanks @T-me but now I'm getting a different issue. I've updated OP with updated code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.