批处理脚本替换JSON文件EikonPath键值对时出现异常的问题
Let's break down why your script is producing that odd duplicate result and fix it step by step.
The Root Problem
Your current script searches for just the substring EikonPath, then replaces it with the full key-value string "EikonPath": "D:\\e5\\desktopframework\\src\\products\\eikon-on-electron". When it hits the original line:
"EikonPath": "%LOCALAPPDATA%\\Refinitiv\\Refinitiv Workspace"
It only replaces the EikonPath part, leaving the rest of the original line (: "%LOCALAPPDATA%\\Refinitiv\\Refinitiv Workspace") intact. That's why you end up with the duplicated content.
The Corrected Script
Instead of doing a partial substring replace, we'll check for lines containing the target key ("EikonPath":) and replace the entire line with your desired key-value pair. This avoids any leftover content from the original line:
@echo off setlocal enableextensions disabledelayedexpansion :: Define our target key and new full key-value line set "targetKey=""EikonPath"":" set "newKeyValue=""EikonPath"": ""D:\\e5\\desktopframework\\src\\products\\eikon-on-electron""" set "configFile=D:\e4\office-shim\src\eikon-office-shim\config\install-settings.json" :: Create a temporary file to hold modified content (safer than editing in-place) for /f "delims=" %%i in ('type "%configFile%"') do ( set "currentLine=%%i" setlocal enabledelayedexpansion :: Check if the line contains our target key echo(!currentLine! | findstr /i /c:!targetKey! >nul if not errorlevel 1 ( :: Replace the line with our new key-value pair echo(!newKeyValue!>>"%configFile%.tmp" ) else ( :: Keep the original line if it doesn't match echo(!currentLine!>>"%configFile%.tmp" ) endlocal ) :: Replace the original file with the modified temporary file move /y "%configFile%.tmp" "%configFile%" >nul endlocal echo Replacement completed successfully!
Key Changes Explained
- Targeted Line Matching: We use
findstrto identify lines containing"EikonPath":instead of doing a partial substring swap. This ensures we only modify the exact lines we care about. - Full Line Replacement: Instead of replacing just part of the line, we write the complete desired key-value pair for matching lines. No more leftover content!
- Temporary File Handling: Using a temp file avoids potential issues with reading and writing to the same file simultaneously, which can cause data loss or corruption in edge cases.
- Proper Quoting: All strings are properly quoted to handle spaces and special characters in file paths and key-value pairs.
Alternative: Exact Match Replacement
If you want to replace only the exact original key-value pair (not any line containing "EikonPath":), you can adjust the script to search for the full original string. Just update the match variable to the exact line you want to replace:
:: Exact original line to replace (note the doubled %% for escaping) set "originalLine=""EikonPath"": ""%%LOCALAPPDATA%%\\Refinitiv\\Refinitiv Workspace"""
Then modify the findstr check to use this exact match instead of the key substring.
内容的提问来源于stack exchange,提问作者kittu

