You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本疑问:获取原文件名以保存修改后的XML文件

解决方法

直接用 $_.Name 替换脚本里的 ???????? 就能获取当前遍历文件的原文件名。

修改后的完整脚本

New-Item '.\DD1' -ItemType Directory -Force  # 加-Force避免文件夹已存在时报错

Get-ChildItem -Path .\ -Filter *.pws | Foreach-Object {
    [xml]$myXML = Get-Content $_.FullName
    $valueInt = [int]$myXML.settings.TuningMenu.FF
    $newValueInt = $valueInt * 1.25
    $myXML.settings.TuningMenu.FF = [string]$newValueInt
    
    # 用Join-Path更安全地拼接路径,避免手动加分隔符的问题
    $fullName = Join-Path -Path (Join-Path -Path $PWD -ChildPath "DD1") -ChildPath $_.Name
    $myXML.Save($fullName)
}

关键说明

  • $_ 是PowerShell循环里代表当前对象的自动变量,这里就是遍历到的每个.pws文件,$_.Name直接取原文件名,保证修改后的文件和原文件同名。
  • 给New-Item加-Force参数,避免DD1文件夹已存在时脚本报错。
  • 用Join-Path替代手动拼接路径,能自动处理不同系统的路径分隔符,避免出现错误路径。

内容的提问来源于stack exchange,提问作者Cnhoff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 14:00:34