@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。