Dancia
Dancia

Reputation: 812

Copy versioned file to other directory

I want to find in directory file example-file-5.0.0.0.jar and copy renamed it to another directory. I have written this script:

Get-ChildItem -Path $directoryFrom | Where-Object { $_.Name -match "example-file-\d\.\d\.\d\.\d\.jar$" } | Copy-Item -Destination $directory\"example-file.jar -Recurse -Force

The problem is that, I have another file named example-file-5.0.0.0-debug.jar in same directory and it is copied instead of example-file-5.0.0.0.jar file. How can I fix this?

Upvotes: 1

Views: 24

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

You are missing something else. Your -match is right:

"example-file-5.0.0.0-debug.jar" -match "example-file-\d\.\d\.\d\.\d\.jar$"
# Output: False
"example-file-5.0.0.0.jar" -match "example-file-\d\.\d\.\d\.\d\.jar$"
# Output: True

However, your Copy-Item cmdlet has a random quote inside the destination and misses a closing quote. I would recommend you to use the Join-Path cmdlet so you don't have to worry about a trailing slash:

Copy-Item -Destination (Join-Path $directory "example-file.jar") -Force

Note: I also removed the -recurse parameter since you are copy a single file.

Upvotes: 1

Related Questions