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

批处理文件:通过CMD从TXT文件提取指定字段内容

Got it, let's figure out how to extract those code2 and code3 values using CMD. Your file is in JSON format, which can be tricky with pure CMD since it doesn't natively handle structured data—but we've got two solid options here:

Option 1: Pure CMD (For Simple, Fixed-Format JSON)

This works if your JSON is in a single line (like your example) with consistent spacing and formatting. It uses string parsing with findstr and for loops to pull out the values.

To output with keys (code2: jwfuj4r code3: dsfhy3)

@echo off
setlocal enabledelayedexpansion
for /f "tokens=2,4 delims=:,{} " %%a in ('findstr /r "\"code2\":\"" "\"code3\":\"" yourfile.txt') do (
    :: Clean up code2 value (remove quotes)
    set "code2_val=%%a"
    set "code2_val=!code2_val:~1,-1!"
    :: Clean up code3 value (remove quotes)
    set "code3_val=%%b"
    set "code3_val=!code3_val:~1,-1!"
    :: Print the result
    echo code2: !code2_val! code3: !code3_val!
)
endlocal

To output only values (jwfuj4r dsfhy3)

Modify the loop to just print the cleaned values:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=2,4 delims=:,{} " %%a in ('findstr /r "\"code2\":\"" "\"code3\":\"" yourfile.txt') do (
    set "code2_val=%%a"
    set "code2_val=!code2_val:~1,-1!"
    set "code3_val=%%b"
    set "code3_val=!code3_val:~1,-1!"
    echo !code2_val! !code3_val!
)
endlocal

How this works:

  • findstr grabs the line containing both code2 and code3 (since your example is all in one line)
  • The for loop splits the line using :, ,, {, } and spaces as delimiters
  • We use delayed expansion to strip the surrounding quotes from each value

Option 2: CMD + PowerShell (More Reliable for Any Valid JSON)

If your JSON might have line breaks, extra spaces, or more complex structure, this method is way more robust. PowerShell natively parses JSON, so it won't break if the formatting changes a bit.

To output with keys

powershell -Command "$jsonContent = Get-Content 'yourfile.txt' | ConvertFrom-Json; Write-Host 'code2:' $jsonContent.code2 'code3:' $jsonContent.code3"

To output only values

powershell -Command "$jsonContent = Get-Content 'yourfile.txt' | ConvertFrom-Json; Write-Host $jsonContent.code2 $jsonContent.code3"

Why this is better:

  • PowerShell properly parses the JSON structure instead of just string-slicing, so it handles any valid JSON format
  • No need to worry about spacing or line breaks in the original file

Common issues with "failed previous attempts"

Most pure CMD solutions rely on strict formatting—if your JSON had line breaks, extra spaces, or different delimiter placement, those methods would fail. The PowerShell approach avoids that entirely by treating the content as structured data.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:29:11