返回列表 发帖

【已解决】【100红包】批量替换指定路径子文件夹和文件名,xml和json里参数

本帖最后由 linfeng_321 于 2022-5-16 10:18 编辑

需求:批量替换指定路径的所有子文件夹名和文件名(包含更多层级子文件夹,也包含里面的文件) ,以及xml、json文件里参数。

1、111  改成 222
2、aaa 改成 bbb
.....
可以设置更多参数替换

注:只要xml和json里包含111和aaa都会被替换
子文件夹名和文件名是111或aaa也都被替换。

<#*,:&cls
@echo off
cd /d "%~dp0"
powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
pause
exit /b
#>
# 替换字符串
$htstr = @{
  '111' = '222'
  'aaa' = 'bbb'
}
# 根路径
$rootPath = ".\*"
function Get-Encoding {
  [CmdletBinding(DefaultParameterSetName = "PathSet")]
  param (
    [Parameter(ParameterSetName = "StreamSet", Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [System.IO.Stream]$Stream,
    [Parameter(ParameterSetName = "PathSet", Mandatory = $true, Position = 0)]
    [ValidateNotNullOrEmpty()]
    [System.String]$Path,
    [Parameter(Mandatory = $false, Position = 1)]
    [System.UInt32]$ReadCount = 1024
  )
  $utf8BOMThrow = New-Object System.Text.UTF8Encoding -ArgumentList @($true, $true)
  $utf8NoBOMThrow = New-Object System.Text.UTF8Encoding -ArgumentList @($false, $true)
  $utf16LEBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($false, $true, $true)
  $utf16LENoBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($false, $false, $true)
  $utf16BEBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($true, $true, $true)
  $utf16BENoBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($true, $false, $true)
  # type encoding,bool bom,bool throw,Text.Encoding encoding,byte[] preamble,string strPreamble
  $arrUTF8Bom = $utf8BOMThrow.GetPreamble()
  $arrUTF16LEBom = $utf16LEBOMThrow.GetPreamble()
  $arrUTF16BEBom = $utf16BEBOMThrow.GetPreamble()
  
  if ($PSCmdlet.ParameterSetName -eq "PathSet") {
    try {
      $Stream = New-Object System.IO.FileStream -ArgumentList @($Path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)
    } catch {
      return $null
    }
  }
  $byteBuff = New-Object byte[] -ArgumentList 3
  $readCount = $Stream.Read($byteBuff, 0, 3)
  if ($byteBuff[0] -eq $arrUTF8Bom[0] -and $byteBuff[1] -eq $arrUTF8Bom[1] -and $byteBuff[2] -eq $arrUTF8Bom[2]) {
    # utf8bom
    $return = $utf8BOMThrow
  } elseif ($byteBuff[0] -eq $arrUTF16LEBom[0] -and $byteBuff[1] -eq $arrUTF16LEBom[1]) {
    # utf16lebom
    $return = $utf16LEBOMThrow
  } elseif ($byteBuff[0] -eq $arrUTF16BEBom[0] -and $byteBuff[1] -eq $arrUTF16BEBom[1]) {
    # utf16bebom
    $return = $utf16BEBOMThrow
  } else {
    # nobom
    if ($ReadCount -gt 0) {
      $charBuff = New-Object char[] -ArgumentList $ReadCount
    }
    # utf16-nobom 都被认为是ANSI编码
    foreach ($encoding in @($utf8NoBOMThrow<# , $utf16LENoBOMThrow, $utf16BENoBOMThrow #>)) {
      try {
        $Stream.Position = 0
        $sr = New-Object System.IO.StreamReader -ArgumentList @($Stream, $encoding, $false)
        if ($ReadCount -gt 0) {
          [void]$sr.Read($charBuff, 0, $ReadCount)
        } else {
          [void]$sr.ReadToEnd()
        }
        $return = $encoding
        break
      } catch {
        
      } finally {
        if ($sr) {
          $sr.Dispose()
        }
      }
    }
  }
  if ($PSCmdlet.ParameterSetName -eq "PathSet") {
    $Stream.Dispose()
  }
  if (!$return) {
    $return = [System.Text.Encoding]::Default
  }
  return $return  
}
Get-ChildItem -Path $rootPath  -Recurse | ForEach-Object {
  if (-not $_.PSIsContainer -and $_.Name -like '*.json' -or $_.Name -like '*.xml') {
    try {
      Write-Host $_.FullName
      $encoding = Get-Encoding -Path $_.FullName
      $txt = [System.IO.File]::ReadAllText($_.FullName, $encoding)
      foreach ($key in $htstr.Keys) {
        $txt = $txt.Replace($key, $htstr[$key])
      }
      [System.IO.File]::WriteAllText($_.FullName, $txt, $encoding)
    } finally {
    }
    trap {}
  }
  $newName = $_.Name
  foreach ($key in $htstr.Keys) {
    $newName = $newName.Replace($key, $htstr[$key])
  }
  if ($newName -ne $_.Name) {
    Rename-Item -LiteralPath $_.FullName -NewName ($newName ) -Force -Verbose -ErrorAction SilentlyContinue
  }
}COPY
微信:flashercs
QQ:49908356

TOP

回复 2# flashercs


    已支付,谢谢大佬

TOP

返回列表