【发布时间】:2018-12-13 12:54:56
【问题描述】:
我一直想要一个易于使用的脚本,它可以让我在一段时间内替换多个文件中的多个字符串。到目前为止,我已经得到了这个代码:
$replacements = @{
'bCompressDiffuseLocalPlayerCharacterTextures=True' = 'bCompressDiffuseLocalPlayerCharacterTextures=False'
'bCompressDiffuseLocalPlayerVehicleTextures=True' = 'bCompressDiffuseLocalPlayerVehicleTextures=False'
'bCompressDiffuseOtherPlayerCharacterTextures=True' = 'bCompressDiffuseOtherPlayerCharacterTextures=False'
'bCompressDiffuseOtherPlayerVehicleTextures=True' = 'bCompressDiffuseOtherPlayerVehicleTextures=False'
'bCompressNormalTextures=True' = 'bCompressNormalTextures=False'
'bDisablePhysXHardwareSupport=True' = 'bDisablePhysXHardwareSupport=False'
'bEnableMouseSmoothing=True' = 'bEnableMouseSmoothing=False'
'bInitializeShadersOnDemand=True' = 'bInitializeShadersOnDemand=False'
'MaxChannels=32' = 'MaxChannels=64'
'MotionBlur=True' = 'MotionBlur=False'
'm_bCalculateOnServer=True' = 'm_bCalculateOnServer=False'
'OneFrameThreadLag=True' = 'OneFrameThreadLag=False'
'PoolSize=140' = 'PoolSize=1024'
'UseMinimalNVIDIADriverShaderOptimization=True' = 'UseMinimalNVIDIADriverShaderOptimization=False'
'UseTextureFileCache=False' = 'UseTextureFileCache=True'
}
function Update-FileContent {
[cmdletbinding()]
param(
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Mandatory=$true,
Position=0)]
[Alias('PsPath')]
$Path
)
$lines = Get-Content $Path
$lines | ForEach-Object {
foreach($rep in $replacements.Keys)
{
$_ = $_ -replace $rep, $replacements[$rep]
}
$_
} | Set-Content $Path
}
Get-ChildItem -Recurse *.ini | Update-FileContent
只有在文件深度为 1 个目录时才有效。
【问题讨论】:
-
您希望它在管道中工作吗?那么您需要将
process块添加到您的cmdlet。否则它只适用于管道中的第一项。 -
It works but only if a file is 1 directory deep.这是否意味着它没有找到其他 ini 文件,或者是否意味着如果有更多目录级别,脚本根本不起作用?你试过什么了?如果你运行gci -Recurse *.ini | Write-Host会发生什么?您是否尝试过更改函数的绑定并使用“更深”的 INI 文件的路径调用它? -
好点,@Matt,虽然它是管道中唯一处理的 last 对象,因为缺少
process块是隐含的 @987654326 @块。
标签: powershell