@echo off setlocal EnableDelayedExpansion

set "CurrCD=%~dp0" set "strOld=" set "strNew="

for /f %%i in ('dir /b /s /a:-d *.xml') do ( set "file=%%i" setlocal DisableDelayedExpansion set "newFile=!file:%strOld%=%strNew%!" powershell -Command "(gc '!file!') -replace '%strOld%', '%strNew%' | Out-File '!newFile!'" endlocal )

pause

解决延迟扩展问题

根据你提供的脚本,我注意到你使用了setlocal EnableDelayedExpansion和setlocal DisableDelayedExpansion来启用和禁用延迟扩展。然而,在你的循环中,你没有使用延迟扩展来获取变量的值。

在你的循环中,你使用了%%i来表示文件名,但是你没有使用!来获取延迟扩展的值。你需要将%%i更改为!file!,并将'%%i'更改为'!file!'。

请尝试以下修改后的脚本:

@echo off
setlocal EnableDelayedExpansion

set "CurrCD=%~dp0"
set "strOld=<?xml version='1.0' encoding='utf-8'?><Key xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ProductKey>" 
set "strNew="

for /f %%i in ('dir /b /s /a:-d *.xml') do (
    set "file=%%i"
    setlocal DisableDelayedExpansion
    set "newFile=!file:%strOld%=%strNew%!"
    powershell -Command "(gc '!file!') -replace '%strOld%', '%strNew%' | Out-File '!newFile!'"
    endlocal
)

pause

这样修改后,脚本应该能够成功替换旧字符。


原文地址: https://www.cveoy.top/t/topic/pldc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录