Novice_Techie
Novice_Techie

Reputation: 444

How to copy and rename a file in same directory?

My folder C:\Downloads\Files\ has 3 .zip files.

I need to copy these files into same location and append another extension .dat to the file.

The total files in my folder C:\Downloads\Files\ should have 6 files now (3 .zip and 3.dat)

Can anyone help me get this done?

Upvotes: 2

Views: 4244

Answers (2)

Martin Brandl
Martin Brandl

Reputation: 58931

Use the Get-ChildItem cmdlet to retrieve all zip files and copy them using the Copy-Item cmdlet:

Get-ChildItem -Path 'C:\Downloads\Files\' -Filter '*.zip' | 
    Copy-Item -Destination { "$($_.Name).dat" }

Upvotes: 5

Alex Sarafian
Alex Sarafian

Reputation: 674

The following copies only files from within the $path directory with extension .dat.

Get-ChildItem -Path $path -File | ForEach-Object {Copy-Item -Path $_.FullName -Destination "$($_.FullName).dat"}

Upvotes: 1

Related Questions