43

I am using the following command to copy files from a network share to my local hard drive based on a CSV file.

import-csv C:\TEST\test.csv | foreach {copy-item -path $_.npath -destination 'C:\TEST\'}

The next step would be to then use a different CSV file to rename these files based on their current file name.

Is there a command that will allow me to copy and item and rename it as well?

3 Answers 3

46

If you have a CSV containing two columns, oldfilepath and newfilename, then you can use the following.

Import-Csv C:\test.csv | % { Copy-Item -Path $_.oldfilepath -Destination "C:\TEST\$($_.newfilename)" }

Notice how the $_.newfilename is encapsulated inside a $(). That's because $_.newfilename is an expression (since we are getting a property out of the variable), and not a variable. $() tells PowerShell to solve the expression before using it in the string. If we don't use it, it would have used the whole csv-object for that row($_) as a string and returned an error.

Sign up to request clarification or add additional context in comments.

Comments

32

If your original CSV also had the new name in it, you would be able to use it to specify a filename onto the end of your copy path. The copy-item CMDlet allows you to specify the destination filename

Copy-Item C:\TEST\file1.jpg C:\TEST\file2.jpg

Will copy file1.jpg and rename it to file2.jpg

To concatenate the variable onto the end of the path string, you'd use double quotes like this:

Copy-Item C:\Path\To\File\file.ext "C:\Path\To\New\File\$newfilename"

Note that the -path and -destination aren't really necessary as they're implied by the position.

4 Comments

So, if I had a maching column in my CSV called "NewName" it would be as simple as modifying my destination string to "-destination 'C:\TEST\' + $_.newname"? I will test this tomorrow, but I have a feeling I've already tried this with no success expecting it to be similar to the Linux mv command.
You would need to do Copy-Item C:\Path\To\File "C:\Path\To\New\File\$newfilename" for the variable to expand properly within the string. That is - encapsulate the whole thing in quotes. Sorry, i'll update the answer a bit better.
This is what I don't understand about PS. Why would what I typed not work? Why are you calling a variable as $_. in one context, but $ in another and WITHIN the quotes? This seems like sloppy implementation, and it's making this language frustrating to learn.
My answer was more about the function and the nature of powershell string-variable concatenation than providing you with the answer without you doing any work. In my example I have a variable i'm concatenating named $newfilename but obviously you would substitute that with your piped column value. $_. syntax is used for getting the piped object's properties, wheras simple $ is just denoting a variable as i'm sure you know. Sorry to throw you off by not being precise but where's the accomplishment if you don't take the function reference and apply it yourself!
0

The good option would be to copy the source using $file.FullName and remember about the $file.Extention included in the -Destination parameter if not a folder.

$file = Get-ChildItem -Path . -Name "name"
Copy-Item -Path $file.FullName -Destination "$($new_name)$($file.Extention)"

Comments

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.