user664481
user664481

Reputation: 2301

Copy and rename file

How to copy then rename the file? Example I have file CSC_KPI_2011201 and I want copy this file to other location follow by rename it to KPI_DATA_2011201. "2011201" is a variable string, i.e. it can be any value.

This is the code I would like to rewrite. $file.FullName contains the original file name and $output_path is the destination directory.

Copy-Item $file.FullName $output_path

Upvotes: 0

Views: 135

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

Just provide the full path (including the new name) as the destination:

$dst = Join-Path $output_path ($file.Name -replace 'CSC_KPI_','KPI_DATA_')
Copy-Item $file.FullName $dst

Upvotes: 1

Related Questions