在.CMD中格式化结果

时间:2018-07-20 01:43:48

标签: batch-file cmd

我试图创建一个运行并检查基本系统信息的基本脚本,但我希望对输出进行格式化,以便结果在同一行且易于阅读。

@Echo Off

systeminfo | findstr /B /C:"OS Name" /C:"OS Version"

wmic cpu get name, status

systeminfo | findstr /C:"Total Physical Memory"

@echo off & setlocal ENABLEDELAYEDEXPANSION
SET "volume=C:"
FOR /f "tokens=1*delims=:" %%i IN ('fsutil volume diskfree %volume%') DO (
    SET "diskfree=!disktotal!"
    SET "disktotal=!diskavail!"
    SET "diskavail=%%j"
)
FOR /f "tokens=1,2" %%i IN ("%disktotal% %diskavail%") DO SET "disktotal=%%i"& SET "diskavail=%%j"
ECHO(Total Space:            %disktotal:~0,-9% GB
ECHO(Available Space:        %diskavail:~0,-9% GB

systeminfo | find "System Boot Time:"

systeminfo | find "System Type:"

Echo Antivirus: & wmic /node:localhost /namespace:\\root\SecurityCenter2 path AntiVirusProduct Get DisplayName | findstr /V /B /C:displayName || echo No Antivirus installed

Current output

主要示例是wmic命令,将结果放在下一行而不是同一行。

任何更好的脚本编写方式都可以得到我的帮助。

2 个答案:

答案 0 :(得分:1)

@echo off
setlocal

for /f "tokens=1,* delims=:" %%A in ('systeminfo') do (
    if "%%~A" == "OS Name" (
        call :print "%%~A" %%B
    ) else if "%%~A" == "OS Version" (
        call :print "%%~A" %%B
        call :cpu
    ) else if "%%~A" == "Total Physical Memory" (
        call :print "%%~A" %%B
        call :fsutil C:
    ) else if "%%~A" == "System Boot Time" (
        call :print "%%~A" %%B
    ) else if "%%~A" == "System Type" (
        call :print "%%~A" %%B
    )
)

:next
call :antivirus

pause
exit /b

:print
setlocal enabledelayedexpansion
set "name=%~1"
if not defined name exit /b
set "name=%~1                                "
set "full=%*"
set "full=!full:,=!"
set "data="
set "skip1="

for %%A in (!full!) do (
    if not defined skip1 (
        set "skip1=defined"
    ) else if not defined data (
        set "data=%%~A"
    ) else (
        set "data=!data! %%~A"
    )
)

echo !name:~,30!: !data!
exit /b

:antivirus
setlocal enabledelayedexpansion
set "antivirus="

for /f "tokens=*" %%A in ('
 2^>nul wmic /node:localhost
 /namespace:\\root\SecurityCenter2 path AntiVirusProduct
 Get DisplayName /value
 ^| findstr /i /b /c:"DisplayName" ^|^| echo No Antivirus installed
') do set "antivirus=%%~A"

if /i "!antivirus:~,12!" == "DisplayName=" set "antivirus=!antivirus:~12!"

call :print Antivirus "!antivirus!"
exit /b

:cpu
for /f "tokens=1,* delims==" %%A in ('wmic cpu get name^, status /value') do (
    call :print "%%~A" %%B
)
exit /b

:fsutil
setlocal enabledelayedexpansion
2>nul >nul net session || exit /b

for /f "tokens=1,* delims=:" %%A in ('2^>nul fsutil volume diskfree %1') do (
    set "name=%%~A"
    set "value=%%~B"
    call :print "!name:bytes=GBs!" !value:~,-9!
)
exit /b

fsutil命令的输出略有不同。 如果可以,我选择了一个简单的选项。

由于同时需要以下两个结果,因此无法确定防病毒输出 安装或不确定。

它仅运行systeminfo一次即可节省时间。

查看set /?,以获取有关替换的信息, 使用。

net session命令用于测试脚本是否以管理员身份运行。 如果不是Admin,则fsutil将被跳过,因为它需要Admin。

查看for /?,了解脚本中使用的命令的用法。

使用enabledelayedexpansion是为了防止出现特殊情况 暴露的字符可能会导致错误。 并在代码块中用于根据需要延迟扩展。

从用空格替换的值字符串中删除逗号。 逗号通常用作千位分隔符,数字看起来很奇怪 用空格代替。发生这种情况是因为使用了简单的 for循环将逗号解释为参数分隔符。其他 chararters也可以这样做,尽管也许空间更好 比他们的情况要好。

答案 1 :(得分:0)

WMIC的输出是unicode!

可以通过将值传递到另一个FOR / F循环中来删除结尾的<CR>。这也消除了幻影的“空白”行(实际上是<CR>

@Echo Off
Call :GET_CPU name CPU_Name

Set "WMIC_Antivirus=wmic /node:localhost /namespace:\\root\SecurityCenter2 path AntiVirusProduct Get DisplayName ^| findstr /V /B /C:displayName"
@For /F "delims=" %%I in ('%WMIC_Antivirus%') do ( 
    for /f "delims=" %%A IN ("%%I") DO SET "Antivirus=%%A"
)

echo CPU       :   %CPU_Name%
echo Antivirus :   %Antivirus%
pause & exit
::----------------------------------------------------
:GET_CPU 
Set "WMIC_CPU=wmic cpu get name /Value"
FOR /F "tokens=2 delims==" %%I IN (
  '%WMIC_CPU% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::----------------------------------------------------

您可以尝试将输出保存到文本文件中:

@Echo Off
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
REM  --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >nul 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO                      **************************************
ECHO                       Running Admin shell... Please wait...
ECHO                      **************************************

    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
SetLocal EnableDelayedExpansion
set "Log=%~dp0Log.txt"
If exist "%Log%"  Del "%Log%" 
Call :GET_CPU name CPU_Name
Set "WMIC_Antivirus=wmic /node:localhost /namespace:\\root\SecurityCenter2 path AntiVirusProduct Get DisplayName ^| findstr /V /B /C:displayName"
@For /F "delims=" %%I in ('%WMIC_Antivirus%') do ( 
    for /f "delims=" %%A IN ("%%I") DO SET "Antivirus=%%A"
)

(
    Echo.
    Echo ***************************** General infos ***********************************
    Echo.
    Echo Running under: %username% on profile: %userprofile%
    Echo Computer name: %computername%
    Echo.
    systeminfo
    Echo Operating System:
    Echo PROCESSOR ARCHITECTURE : %PROCESSOR_ARCHITECTURE%
    echo NUMBER_OF_PROCESSORS : %NUMBER_OF_PROCESSORS%
    echo PROCESSOR_IDENTIFIER : %PROCESSOR_IDENTIFIER%
    echo PROCESSOR_LEVEL : %PROCESSOR_LEVEL%
    echo PROCESSOR_REVISION : %PROCESSOR_REVISION%
    echo OS TYPE : %OS%
    echo(
    echo Program files path : %Programfiles%
    echo Program files(86^) path  : %Programfiles(86^)%
    echo ProgramW6432 path : %ProgramW6432%
    echo PSModulePath : %PSModulePath%
    echo SystemRoot : %SystemRoot%
    echo Temp Folder : %Temp%
    echo CPU       :   !CPU_Name!
    echo Antivirus :   !Antivirus!
    Echo.
    Echo **************************** Drives infos *************************************
    Echo.
    Echo Listing currently attached drives:
    wmic logicaldisk get caption,description,volumename | find /v ""
    Echo.
    Echo Physical drives information:
    for /F "tokens=1-3" %%A in ('fltmc volumes^|find ":"') do echo %%A %%B %%C
)>>"%Log%"
Start "" "%Log%" & exit
::----------------------------------------------------
:GET_CPU 
Set "WMIC_CPU=wmic cpu get name /Value"
FOR /F "tokens=2 delims==" %%I IN (
  '%WMIC_CPU% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::----------------------------------------------------