我正在尝试替换属性文件中的占位符。因此,替换有效,但它总是在每行后面添加一个空格:
@echo off
REM Loop through all files in both specified directories (non-recursively)
for %%D in ("%propertiesDir1%" "%propertiesDir2%") do (
for %%F in ("%%D\CTL*.properties" "%%D\h*.properties") do (
if exist "%%F" (
echo Processing: %%F
REM Read each line of the file and update it in place
for /f "usebackq delims=" %%L in ("%%F") do (
set "line=%%L"
REM Check if the line contains the target string and replace it if found
echo !line! | findstr /c:"%targetString%" >nul
if not errorlevel 1 (
REM Replace the target string with the new value directly in the file
set "line=!line:%targetString%=%myValue%!"
)
echo !line! >> "%%F.new"
)
REM Replace the original file with the new content
move /y "%%F.new" "%%F" >nul
echo Update completed for: %%F
) else (
echo File not found: %%F, skipping...
)
)
)
例如:
替换前的test.properties:
myCode=代码
替换后的test.properties:
myCode=代码(空格)
为什么会出现这种情况?我该如何解决它?
斯蒂芬的解决方案对我有用:
(echo !line!) >>"%%F.new"