簡體   English   中英

Powershell:以遞歸方式替換所有.ini文件中的字符串

[英]Powershell: Replace strings in all .ini files recursively

我一直想要一個易於使用的腳本,該腳本可以讓我暫時替換多個文件中的多個字符串。 到目前為止,我已經獲得了以下代碼:

$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個目錄時。

我會做這樣的事情:

$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
    )

    # read the ini file in as one single string
    $content = Get-Content $Path -Raw
    # copy that string so we can compare at the end of the loop if anything has changed at all
    $newContent = $content
    foreach($rep in $replacements.Keys) {
        $newContent = $newContent -replace $rep, $replacements[$rep]
    }
    # only replace the contents of the ini file if changes are made
    if ($newContent -ne $content) {
        Write-Host "Replacing content of file '$Path'"
        Set-Content $Path -Value $content
    }
}

$rootPath = '<PATH OF THE ROOTFOLDER TO RECURSE WHERE THE INI FILES ARE LOCATED>'
Get-ChildItem -Path $rootPath -Filter '*.ini' -Recurse | ForEach-Object { Update-FileContent $_.FullName }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM