Nuget Powershell重命名内容文件

本文关键字:文件 重命名 Powershell Nuget | 更新日期: 2023-09-27 17:57:59

我想重命名通过nuget包加载的文件:

我知道我可以得到这样的文件:

$f = $project.ProjectItems | ForEach-Object { $_.ProjectItems } | where { $_.Name -eq "MyFile.cs.custom" } 

我也知道我可以删除这样的文件:

$f.Delete()

我只是在寻找重命名该文件的语法。我试过做:

$project.ProjectItems | ForEach { $_.ProjectItems } | Where { $_.Name -eq "MyFile.cs.custom" } | Foreach {Rename-Item $_.Name ($_.FileNames -replace "'.custom", "")}

没有这么做

install.ps1文件的整体是:

param($installPath, $toolsPath, $package, $project)
#save the project file first - this commits the changes made by nuget before this     script runs.
$project.Save()
$project.ProjectItems | ForEach { $_.ProjectItems } | Where { $_.Name -eq "MyFile.cs.custom" } | Foreach {Rename-Item $_.Name ($_.FileNames -replace "'.custom", "")}
$project.Save()

通过将csproj文件的项目启动对象更改为项目中不存在的类,我最终解决了这个问题。

Nuget Powershell重命名内容文件

我希望它对某人有用,至少对我有用。在这段代码中,我在根文件夹中查找文件夹"FolderConfig"(例如C:''projects''SomeProject''FolderConfig''),然后重命名文件。你可以按文件夹"FolderConfig"删除过滤器,并在所有项目中搜索这些文件。

$project.ProjectItems | where { $_.Name -eq "FolderConfig" } |
ForEach { $_.ProjectItems } |  where { $_.Name -eq "Test.config"} |
ForEach { $_.Name = "NewTest.config" }

我怀疑ProjectItems的类型是FileInfo,所以试试这个:

$project.ProjectItems | ForEach { $_.ProjectItems } | 
    Where { $_.Name -eq "MyFile.cs.custom" } |  
    Foreach {Rename-Item $_.Name ($_.FileNames -replace "'.custom", "")}

我认为你需要在项上找到一个与文件的全名(路径)相对应的属性(用作重命名项的路径),除非你能保证PowerShell被cd到文件的包含目录中。

另外,请注意-replace使用regex元字符。

注意,FileNames可以在单个字符串中返回多个文件名。

我们的install.ps1文件应该是这样的。

param($installPath, $toolsPath, $package, $project)
$file1 = $project.ProjectItems.Item("test.exe")
$file2 = $project.ProjectItems.Item("test.config")
// set 'Copy To Output Directory' to 'Copy if newer'
$copyToOutput1 = $file1.Properties.Item("CopyToOutputDirectory")
$copyToOutput1.Value = 2
$copyToOutput2 = $file2.Properties.Item("CopyToOutputDirectory")
$copyToOutput2.Value = 2