SystemInfo - Get computer System Model via CMD - Extra spaces bug

后端 未结 3 1465
抹茶落季
抹茶落季 2021-01-29 01:17

I\'m trying to get a Computer System Model type via Batch file. for this i\'ve created this script:

systeminfo | find \"System Model\" > %temp%\\TEMPSYSINFO.t         


        
3条回答
  •  忘了有多久
    2021-01-29 01:42

    You could use

    for /F "tokens=2* delims=: " %%a in (%temp%\TEMPSYSINFO.txt) do set SYSMODEL=%%b
    

    meaning use delimiters COLON or SPACE, making the text token3 BUT as the text might? include colon or space, the * means 'the rest of the line following the delimiters after token 2 the first-mentioned token (2) is the one that gets assigned to%%a, the next highest (*) to%%b`

    Now you could also code

    for /F "tokens=2* delims=: " %%a in (
     'systeminfo 2^>nul ^| find "System Model" '
     ) do set SYSMODEL=%%b
    

    which means you don't need the TEMPSYSINFO.txt file. This executes the single-quoted command line - the special characters > and | need a caret (^) to 'escape' them (turn off their special meaning) as far as the FOR is concerned (they belong to the quoted command, not the FOR.)

    The 2>nul will suppress the SYSTEMINFO progress text.

    And it's quite legitimate to break this line - just have to be careful precisely where you do it. Makes the code more readable.

提交回复
热议问题