Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

Powershell use pattern matching in filenames

I'm not that great with Powershell and I'm facing a simple problem. I'm building an automatic Nuget package publish script and for that I need to push the nuget package with a file name like this "Name.version.nupkg". The version number changes so I would need to find a file that matches the patter with changing version number.

Currently the script is a simple script:

cd $PSScriptRoot
dotnet nuget push ..inDebugMacroFramework.*.nupgk --api-key --source https://api.nuget.org/v3/index.json

$PSScriptRoot is "MacroFrameworkLibrary/Local"

And the output is:

error: File does not exist (..inDebugMacroFramework.*.nupgk).

Image of the directory content here:

enter image description here

Thanks for the help in advance

EDIT: The final working script is as follows:

cd $PSScriptRoot
$files = Get-ChildItem ..inDebugMacroFramework.*.nupkg | Sort-Object LastWriteTime -Descending | Select-Object -First 1

foreach ($pgk in $files)
{
  dotnet nuget push $pgk.FullName --api-key --source https://api.nuget.org/v3/index.json
}
question from:https://stackoverflow.com/questions/66061102/powershell-use-pattern-matching-in-filenames

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

dotnet nuget push doesn't support wildcards in paths.

You can get around this by using the wildcard in Get-ChildItem, sorting by date descending, then piping the first match to dotnet nuget push.

Here's an example. gci is an alias of Get-ChildItem, | means to send its output as input to the next command, and % { } is a for-each loop. Inside the loop, $_ refers to the current item.

cd $PSScriptRoot
gci ..inDebugMacroFramework.*.nupkg | sort LastWriteTime -Descending | select -First 1 | % { dotnet nuget push $_.FullName --api-key --source https://api.nuget.org/v3/index.json }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...