我正在使用sysinternals中的PsInfo以csv的形式返回驱动器信息。
当我运行此命令时。
psinfo -c -d volume
我得到以下输出。
PCName,,C:,Fixed,NTFS,,930.97 GB,705.81 GB,75.8%,D:,CD-ROM,,,,,0.0%,G:,Fixed,NTFS,My Book,1862.98 GB,889.71 GB,47.8%
我的目标是解析输出以实现这种格式:
PCNAME,,
,,C:,Fixed,NTFS,,930.97 GB,705.81 GB,75.8%
,,G:,Fixed,NTFS,My Book,1862.98 GB,889.71 GB,47.8%
因此,当电子表格查看器读取输出时,它看起来是可读和有组织的。
我尝试使用正则表达式来匹配驱动器号位,我不知道如何正确捕获输出。
编辑:(忘了实际发布我的代码......) 这是我到目前为止所做的。
ECHO OFF
For /f "tokens=1*" %%x in ('net view ^| find "\\"') do (psinfo %%x filter -d -c >> out.csv & echo. >> out.csv)
::used to remove blanmk lines from output
@jrepl "^[ \t]*(.*?)[ \t]*$" "$1?$1:false" /jmatch /f out.csv /o -
pause
答案 0 :(得分:0)
这对我有用。在这个例子中,您应该可以使用所需输出的格式。
@echo off
FOR /F "tokens=1* delims=," %%G IN ('psinfo -c -d volume') DO (
SET "pcname=%%G"
SET "driveinfo=%%H"
)
set driveinfo="%driveinfo:,=","%"
:LOOP
FOR /F "tokens=1-7* delims=," %%G IN ("%driveinfo%") DO (
echo "%pcname%",%%G,%%H,%%I,%%J,%%K,%%L,%%M
SET "driveinfo=%%N"
)
IF DEFINED driveinfo GOTO LOOP
输出
"COCO","C:","Fixed","NTFS","","930.97 GB","705.81 GB","75.8%"
"COCO","G:","Fixed","NTFS","My Book","1862.98 GB","889.71 GB","47.8%"
以下代码已更改为复制您确切需要的输出。
@echo off
FOR /F "tokens=1* delims=," %%G IN ('psinfo -c -d volume') DO (
SET "pcname=%%G"
SET "driveinfo=%%H"
)
set driveinfo="%driveinfo:,=","%"
echo %pcname%,,
:LOOP
FOR /F "tokens=1-7* delims=," %%G IN ("%driveinfo%") DO (
echo ,,%%~G,%%~H,%%~I,%%~J,%%~K,%%~L,%%~M
SET "driveinfo=%%N"
)
IF DEFINED driveinfo GOTO LOOP
这是在少数几行中完成的。
@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=1* delims=," %%G IN ('psinfo -c -d volume') DO (
echo %%G,,
SET "driveinfo=%%H"
)
set "drive2=echo ,,!driveinfo:%%=%%&echo,,!"
%drive2:~0,-1%
答案 1 :(得分:0)
@ECHO OFF
SETLOCAL enabledelayedexpansion
FOR /f "delims=" %%a IN ('psinfo -c -d volume') DO SET "var=%%a"
SET "var2=!var:%%=\!"
SET "retained="
:again
REM FOR /f "tokens=delims==:" %%a IN () DO
FOR /f "tokens=1*delims=:" %%a IN ("%var2%") DO (
CALL :FORMAT "%%a"
SET "var2=%%b"
IF NOT "%%b"=="" GOTO again
)
GOTO :EOF
:FORMAT
SET "data=%~1"
IF "%data:~-1%"=="\" (SET "data=%retained%:%data%") ELSE (SET "data=%retained%:%data:~,-2%")
IF DEFINED retained (
ECHO ,,!data:\=%%!
) ELSE (
ECHO %data:~1%,
)
SET "retained=%~1"
SET retained=%retained:~-1%
GOTO :eof
将\
替换为%
以便于处理。
答案 2 :(得分:0)
您可以使用Powershell获取相同的信息,例如使用Get-Volume命令。这会返回除了比例之外的所有内容,例如:
DriveLetter FileSystemLabel FileSystem DriveType HealthStatus OperationalStatus SizeRemaining Size ----------- --------------- ---------- --------- ------------ ----------------- ------------- ---- D CD-ROM Healthy Unknown 0 B 0 B C Win7 NTFS Fixed Healthy OK 43.59 GB 238.03 GB H Projects NTFS Fixed Healthy OK 96.73 GB 375.24 GB
Get-Volume
返回卷信息对象。 shell只显示最常见的。
您只能选择所需的属性,并使用select命令添加更多列:
Get-Volume | where {$_.DriveType -ne 'CD-ROM'}
| select DriveLetter, FileSystemLabel,FileSystem,SizeRemaining, Size,
@{Name="Ratio";Expression={"{0:P}" -f ($_.SizeRemaining/$_.Size)}}
|Format-Table
DriveLetter FileSystemLabel FileSystem SizeRemaining Size Ratio ----------- --------------- ---------- ------------- ---- ----- C Win7 NTFS 46793633792 255584587776 18,31% H Projects NTFS 103853056000 402911129600 25,78%
我在这里作弊有点过滤掉where
的CD ROM如果结果太长而无法放入窗口,Powershell会将它们显示为名称/值列表。最终的Format-Table用于强制Powershell将结果格式化为表格
我可以将表存储到具有正常重定向的文件中:
Get-Volume | where {$_.DriveType -ne 'CD-ROM'}
| select DriveLetter, FileSystemLabel,FileSystem,SizeRemaining, Size,
@{Name="Rate";Expression={"{0:P}" -f ($_.SizeRemaining/$_.Size)}}
|Format-Table > somefile.txt
或者我可以将结果导出为Export-CSV
的真实CSV Get-Volume | where {$_.DriveType -ne 'CD-ROM'}
| select DriveLetter, FileSystemLabel,FileSystem,SizeRemaining, Size,
@{Name="Rate";Expression={"{0:P}" -f ($_.SizeRemaining/$_.Size)}}
| Export-CSV somefile.csv
答案 3 :(得分:0)
以下代码段根据需要重新格式化psinfo -c -d volume
的输出。由于您似乎希望根据您的示例数据按驱动器类型(Fixed
)进行过滤,因此我实现了这样的功能;要不过滤任何内容,只需将第set "FILTER=Fixed"
行更改为set "FILTER="
:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "FILTER=Fixed"
(set LF=^
%= empty line =%
)
for /F "tokens=1,* delims=, eol=," %%K in ('psinfo -c -d volume') do (
echo(%%K,,
set "LINE=%%L"
setlocal EnableDelayedExpansion
set ^"LINE=!LINE:%%,=%%^%LF%%LF%!^"
for /F "delims= eol=," %%E in ("!LINE!") do (
endlocal
if defined FILTER (
for /F "tokens=2 delims=, eol=," %%F in ("%%E") do (
if "%%F"=="%FILTER%" (
echo(,,%%E
)
)
) else (
echo(,,%%E
)
setlocal EnableDelayedExpansion
)
endlocal
)
endlocal
exit /B