Is there a single PowerShell command to copy and rename files?

前端 未结 2 533
傲寒
傲寒 2021-02-03 21:25

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-ite         


        
2条回答
  •  萌比男神i
    2021-02-03 22:03

    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.

提交回复
热议问题