使用批处理脚本将文本文件转换为 json 文件

使用批处理脚本将文本文件转换为 json 文件
@echo off
setlocal EnableDelayedExpansion


echo { >>post.json
FOR /F "tokens=1,2 skip=1  delims==" %%a in (hardware.txt) do (
    set temp=%%a
    set res=%%b
    call :Trim result !res!
    call :Trim tem !temp!
    for /f "delims=0123456789" %%i in ("!result!") do (
        set var=%%i
        echo !var!
        if defined var (
            echo "!tem!" : "!result!", >> post.json
        ) else (
            echo "!tem!" : !result!, >> post.json
        ) 
    )
)
echo } >> post.json

:Trim
SetLocal EnableDelayedExpansion
set Params=%*
for /f "tokens=1*" %%a in ("!Params!") do EndLocal & set %1=%%b
exit /b

我制作了一个 for 循环,它从名为 hardware.txt 的文件中读取以下格式的信息:

os = Microsoft Windows 10 Pro  
osVersion = 10.0.19042  
username = desktop-ujer9ka\arron 
brand = ASUSTeK COMPUTER INC.  
model = X556UR  
ram = 12  
storage = 934 
macAddress = 00:FF:F7:29:ED:06 

我得到以下格式的 json:

{ 
"os" : "Microsoft Windows 10 Pro" ,
"osVersion" : "10.0.19042" ,
"username" : "desktop-ujer9ka\arron" ,
"brand" : "ASUSTeK COMPUTER INC." ,
"model" : "X556UR" ,
"macAddress" : "00:FF:F7:29:ED:06" ,
"processor" : "Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz" ,
}

在第二个 for 循环中,不执行 else 条件。如果有其他简单的方法,请告诉我,或者如果有人可以帮助我,那就太好了。我需要第二个 for 循环,因为我想检查变量是否是数字,因为我需要 json 中的数字。我也不知道如何删除字符串后的最后一个逗号以制作正确的 json。

答案1

@echo off && cd /d "%~dp0" 

setlocal EnableDelayedExpansion 

for /f %%N in ('find /c "=" ^<.\hardware.txt')do set "_n=%%~N" && (
   for /l %%L in (1,1,%%~N)do set /p _line_%%~L=)<.\hardware.txt

%:^(
for /L %%L in (1 1 %_n%)do for %%G in ("     ","    ","   ","  ")do set "_line_%%~L=!_line_%%~L:%%~G=!"
for /L %%L in (1 1 %_n%)do if "!_line_%%~L:~-1!"==" " (set "_line_%%~L=!_line_%%~L:~0,-1!" & goto %:^()

for /L %%L in (1 1 %_n%)do for /f usebackq^tokens^=1-2*delims^=^= %%i in (`
    call echo\!_line_%%~L!`)do set "_line_%%~L="%%~i@%%~j""

>>.\post.json (
    for /L %%L in (2 1 %_n%)do if %%~L equ 2 (
        echo\^{
        echo\    !_line_%%~L: @ =" : "!,
       )else if %%~L neq %_n% (
        echo\    !_line_%%~L: @ =" : "!,
       )else (
        echo\    !_line_%%~L: @ =" : "!
        echo\^}
       )
    ) && endlocal

我正在考虑:

  1. 您需要跳过 hardware.txt 文件的第一行,因为您的For /F循环命令正在使用skip=1,所以循环从不For /L 开始,因此也“跳过”了文件的一行/变量。21

  2. 我假设我使用的脚本(如命令)获取了硬件文件中的大多数字符串wmic并将其重定向到文件,因此,这就是在行/字符串末尾添加行末尾的额外空格的地方。

  3. 我不明白(我的英语水平有限),你需要在你想要/需要识别数字的地方做什么,任何额外的解释都可能会调整答案以满足你此时的需求。

  4. 尽管我已经假设你的命令是通过使用获得的wmic,也不知道在第一行跳转到什么内容或其来源,但我添加了另一个获取类似于你的 hardware.txt 文件的信息的 bat,但是已经将输出重定向到 .json 文件格式

  5. 物品4.肯定没有回答你的问题的那一部分如果有其他简单的方法,请告诉我,请原谅,有些事情我宁愿重新做,以便于将来的编辑/改编处理

  6. find "="在第一个循环中使用时For /F,我会计算带有“=”的行数,并考虑/使用与您在问题中发布的 hardware.txt 相同的文件,因此我建议您使用问题中发布的相同行和相应数量的行进行测试,如果有差异,请告诉我...

  7. 我认为在项目中指出的用途6.可能会使其skip=1 过时/无效,因此我在下面添加了完整的代码以及建议代码以获取行尾没有额外空格的字符串......


  • 没有skip=1inFor循环:
:: for /L %%L in (2 1 %_n%)do if %%~L equ 2 ()else if()else()...
::                ↓                       ↓
:: for /L %%L in (1 1 %_n%)do if %%~L equ 1 ()else if()else()...

@echo off && cd /d "%~dp0"

if not defined _fjson (call setlocal EnableDelayedExpansion
     set "_fjson=post.json" & call "%~f0" >"%~dp0\!_fjson!"
     timeout /t 05 | type "!_fjson!" & endlocal & goto :eof
    );= else <con call set "_spc="     ","    ","   ","  ""
     
for /f %%N in ('find /c "=" ^<.\hardware.txt')do set "_n=%%~N" & (
     for /l %%L in (1,1,%%~N)do set /p _line_%%~L=)<.\hardware.txt
 
%:^(
for /L %%L in (1,1,%_n%)do for %%G in (;%_spc%)do (set "_line_%%~L=!_line_%%~L:%%~G=!"
     if "!_line_%%~L:~-1!"==" " call set "_line_%%~L=!_line_%%~L:~0,-1!" && goto %:^()
 
for /L %%L in (1,1,%_n%)do for /f usebackq^tokens^=1-2*delims^=^= %%i in (
    `call echo\!_line_%%~L!`)do call set "_line_%%~L=    "%%~i@%%~j"" && (
         if %%~L equ 1 (echo\^{&& <con: call echo\!_line_%%~L: @ =" : "!^
            )else if not %%~L equ %_n% (call echo\!_line_%%~L: @ =" : "!^,
            )else call echo\!_line_%%~L: @ =" : "! && echo\^});=

  • 获取.json格式正确的字符串,且行末没有多余的空格
@echo off && setlocal EnableDelayedExpansion 

:: var line_1 ::  os = Microsoft Windows 10 Pro
title <nul & title set variable line_1
for /f usebackq^tokens^=4delims^=^<^> %%i in (`
     wmic os get caption /format:xml ^| find "VALUE"
   `)do set "_line_1="os" : "%%~i""

:: var line_2 :: osVersion = 10.0.19042
title <nul & title set variable line_2
for /f usebackq^tokens^=4delims^=^<^> %%i in (`
     wmic os get version /format:xml ^| find "VALUE"
   `)do set "_line_2="osVersion" : "%%~i""

:: var line_3 :: username = desktop-ujer9ka\arron 
title <nul & title set variable line_3
for /f usebackq^tokens^=4delims^=^<^> %%i in (`
     wmic /node:. computersystem get username /format:xml ^| find "VALUE"
   `)do set "_line_3="username" : "%%i""

:: var line_4 :: brand = ASUSTeK COMPUTER INC.
title <nul & title set variable line_4
for /f usebackq^tokens^=4delims^=^>^< %%i in (`
     wmic csproduct get vendor /format:xml ^| find "VALUE"
   `)do set "_line_4="brand" : "%%~i""

:: var line_5 :: model = X556UR
title <nul & title set variable line_5
for /f usebackq^tokens^=4delims^=^>^< %%i in (`
     wmic csproduct get name /format:xml ^| find "VALUE"
   `)do set "_line_5="model" : "%%~i""

:: var line_6 :: ram = 12
title <nul & title set variable line_6
for /f usebackq^tokens^=4delims^=^>^< %%i in (`
     wmic computersystem get totalphysicalmemory /format:xml ^|find "VALUE"`)do for /f %%G in ('
     mshta "vbscript:Execute("createobject(""scripting.filesystemobject""^).GetStandardStream(1^).write(Round(%%i/1073741824^)^):close")"^|more
   ')do set "_line_6="ram" : "%%~G""

:: var line_7 :: storage = 934
title <nul & title set variable line_7
for /f usebackq^tokens^=4delims^=^>^< %%i in (`
     wmic logicaldisk where Caption^="C:" get Size /format:xml^|find "VALUE"`)do for /f %%G in ('
     mshta "vbscript:Execute("createobject(""scripting.filesystemobject""^).GetStandardStream(1^).write(Round(%%i/1073741824^)^):close")"^|more
   ')do set "_line_7="storage" : "%%G""

:: var line_8 macAddress = 00:FF:F7:29:ED:06
title <nul & title set variable line_8
for /f usebackq^tokens^=4delims^=^>^< %%i in (`
     wmic path Win32_NetworkAdapter where "PNPDeviceID like '%%%%PCI%%%%' and NetConnectionStatus='2' and AdapterTypeID='0'" get MacAddress /format:xml ^| find "VALUE"
   `)do set "_line_8="macAddress" : "%%~i""

:: var line_9 :: processor = Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
title <nul & title set variable line_9
for /f usebackq^tokens^=4delims^=^>^< %%i in (`
      wmic CPU get NAME /format:xml ^| find "VALUE"
    `)do set "_line_9="processor" : "%%~i""

:: var lines 1-9 :: Writing lines from 1 to 9 in post.json file
title <nul & title write line 1-9 to file: .\post.json
>.\post.json (
    for /L %%L in (1 1 9)do for /f usebackq^delims^= %%i in (`
            "echo\    !_line_%%~L!"`)do if %%~L equ 1 (
             echo\^{ && echo\%%~i,)else if %%~L neq 9 (
             echo\%%~i,)else echo\%%~i&& echo\^}
            )

:: Checking lines_1-9 :: Checking file contents/lines on screen
title <nul & title Checking file contents/lines on screen
timeout /t 10 | type .\post.json & endlocal

  • 在我的电脑上我得到输出:
{
    "os" : "Microsoft Windows 10 Home Single Language",
    "osVersion" : "10.0.19043",
    "username" : "DESKTOP-JRRL1K6\DP",
    "brand" : "LENOVO",
    "model" : "80UH",
    "ram" : "20",
    "storage" : "447",
    "macAddress" : "5C:C9:D3:7E:B1:1C",
    "processor" : "Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz"
}

在此处输入图片描述

答案2

正如您所做的那样set var=%%,变量已被定义 - 它只是空的。

检查空变量:

IF [%1] == [] ...
IF "%~1" == "" ...

更多信息请参阅帖子 在批处理文件中测试参数是否为空的正确方法是什么?

相关内容