批处理文件 - wmic - 空行
batch file - wmic - empty line
查看 google 我正在尝试创建一个批处理文件,输出磁盘列表并释放 space。我找到了一个很好的解决方案,但对于列出的每个驱动器,都有一个 1 space 空行(该行以 space " " 开头,因此 CR LF 用于新的线)。
你有什么建议?
@ECHO OFF
set FILE="testOK.txt"
set IP="127.0.0.1"
set PC="EA"
set TAB=" "
SETLOCAL enableextensions
(for /f "tokens=1-3" %%a in ('
WMIC LOGICALDISK GET FreeSpace^,Name^,Size ^|FINDSTR /I /V "Name"
') do (
echo wsh.echo date ^& %TAB% ^& time ^& %TAB% ^& %ip% ^& %TAB% ^& %PC% ^& %TAB% ^& "%%b" ^& %TAB% ^& FormatNumber^(cdbl^(%%a^)/1024/1024/1024, 2^) ^& " " ^& FormatNumber^(cdbl^(%%c^)/1024/1024/1024, 2^)^& " GB" > "%temp%\tmp.vbs"
if not "%%b"=="" (
echo(
cscript //nologo "%temp%\tmp.vbs"
del "%temp%\tmp.vbs"
)
)
) > %FILE%
示例结果:
01/07/2020 10:20:59 10.0.13.36 ADMINPC C: 4,07 69,90 GB
01/07/2020 10:20:59 10.0.13.36 ADMINPC E: 0,00 0,42 GB
为了使这个答案切合主题,这里有一个 batch-file which uses vbscript and wmi, (not wmic),可以实现您的目标,并且无需创建中间 .vbs
或所有这些变量:
<!-- :
@"%__AppDir__%cscript.exe" //NoLogo "%~f0?.wsf">"testOK.txt"
@Exit /B
-->
<Job><Script Language="VBScript">
Set c=CreateObject("WScript.Network")
Set i=GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
Set o=CreateObject("Scripting.FileSystemObject")
For Each n In i:If n.IPEnabled Then IP=n.IPAddress(0):End If:Next
For Each Drv In o.Drives:If Drv.IsReady=True Then
WScript.Echo Date&VBTab&Time&VBTab&IP&VBTab&c.ComputerName&VBTab&_
Drv.DriveLetter&":"&VBTab&Round(Drv.FreeSpace/1073741824,2)&VBTab&_
Round(Drv.TotalSize/1073741824,2)&" GiB"
End If:Next
</Script></Job>
查看 google 我正在尝试创建一个批处理文件,输出磁盘列表并释放 space。我找到了一个很好的解决方案,但对于列出的每个驱动器,都有一个 1 space 空行(该行以 space " " 开头,因此 CR LF 用于新的线)。 你有什么建议?
@ECHO OFF
set FILE="testOK.txt"
set IP="127.0.0.1"
set PC="EA"
set TAB=" "
SETLOCAL enableextensions
(for /f "tokens=1-3" %%a in ('
WMIC LOGICALDISK GET FreeSpace^,Name^,Size ^|FINDSTR /I /V "Name"
') do (
echo wsh.echo date ^& %TAB% ^& time ^& %TAB% ^& %ip% ^& %TAB% ^& %PC% ^& %TAB% ^& "%%b" ^& %TAB% ^& FormatNumber^(cdbl^(%%a^)/1024/1024/1024, 2^) ^& " " ^& FormatNumber^(cdbl^(%%c^)/1024/1024/1024, 2^)^& " GB" > "%temp%\tmp.vbs"
if not "%%b"=="" (
echo(
cscript //nologo "%temp%\tmp.vbs"
del "%temp%\tmp.vbs"
)
)
) > %FILE%
示例结果:
01/07/2020 10:20:59 10.0.13.36 ADMINPC C: 4,07 69,90 GB
01/07/2020 10:20:59 10.0.13.36 ADMINPC E: 0,00 0,42 GB
为了使这个答案切合主题,这里有一个 batch-file which uses vbscript and wmi, (not wmic),可以实现您的目标,并且无需创建中间 .vbs
或所有这些变量:
<!-- :
@"%__AppDir__%cscript.exe" //NoLogo "%~f0?.wsf">"testOK.txt"
@Exit /B
-->
<Job><Script Language="VBScript">
Set c=CreateObject("WScript.Network")
Set i=GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
Set o=CreateObject("Scripting.FileSystemObject")
For Each n In i:If n.IPEnabled Then IP=n.IPAddress(0):End If:Next
For Each Drv In o.Drives:If Drv.IsReady=True Then
WScript.Echo Date&VBTab&Time&VBTab&IP&VBTab&c.ComputerName&VBTab&_
Drv.DriveLetter&":"&VBTab&Round(Drv.FreeSpace/1073741824,2)&VBTab&_
Round(Drv.TotalSize/1073741824,2)&" GiB"
End If:Next
</Script></Job>