使用批次腳本將文字檔案轉換為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 循環從2not開始1,因此也「跳過」檔案的一行/變數。

  2. 硬體檔案中的大部分字串都是由腳本獲取的,我假設我像命令一樣使用wmic並重定向到文件,因此,這就是在行末尾添加額外空格的地方/細繩。

  3. 我不明白(我的英語能力有限),您需要在想要/需要識別數字的地方做什麼,任何額外的解釋也可能會調整答案以滿足您的需要。

  4. 儘管我已經假設您的命令是通過使用獲得的wmic,也不知道要在第一行或其來源上跳轉什麼內容,但我添加了另一個蝙蝠,它獲取與您的hardware.txt 文件類似的信息,但是已經重定向.json 檔案格式的輸出

  5. 物品4.絕對沒有回答你問題的部分如果還有其他簡單的方法請告訴我,請原諒,我更喜歡重做一些事情,以方便將來編輯/改編時的處理

  6. find "="在第一個循環中使用時For /F,我正在計算具有“=”的行,並考慮/使用等於問題中發布的 hardware.txt 的文件,因此我建議您使用相同的行和相應的數量進行測試問題中發布的行,如果有差異,請告訴我......

  7. 我想 item 中指出的用途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" == "" ...

欲了解更多信息,請參閱帖子 測試批次檔中的參數是否為空的正確方法是什麼?

相關內容