以下代码检查系统中可用的所有驱动器的空间。如何使其仅打印有关G:驱动器的信息,该驱动器也是网络驱动器?并以千兆字节而不是字节打印?谢谢:))
%let xmldata = %sysfunc(pathname(work))\wmic_output.xml;
%let xmlautomap = %sysfunc(pathname(work))\wmic_output-automap.xml;
%let xmlmap = %sysfunc(pathname(work))\wmic_output-map.xml;
filename wmic "&xmldata" encoding="utf-16";
filename wmicmap "&xmlmap";
filename gather pipe "wmic logicaldisk get name,size,freespace /format:rawxml > ""&xmldata""";
data _null_;
infile gather;
input;
put _infile_;
rc = sleep(.1,1);
run;
libname wmic xmlv2 automap=replace xmlmap=wmicmap;
proc copy in=wmic out=work;
run;
proc transpose data=work.property out=properties(drop=_name_) suffix=_text;
by instance_ordinal;
id property_name;
var value;
run;
filename gather;
filename wmic;
filename wmicmap;
title1 "Available space in G: drive, if there is less than 1GB available, please contact the team";
proc print data=work.property noobs;
run;
title; footnote;
title1 "There is less than 1GB available, please contact the BA team";
proc print data=work.property;
var PROPERTY_NAME value;
WHERE instance_ordinal=(4);
/*where property_name='FreeSpace';*/
run;
title; footnote;
答案 0 :(得分:2)
TAZZ:
关于/format:rawxml
的Pro是您不需要编写输入语句。 Con是你必须处理XML并重塑它。
假设您删除了/ format:rawxml。可以使用FIND
过滤wmic输出 - 但是您必须应对输入不一致的数据。考虑wmic在我的系统上提供的无格式输出。
> wmic logicaldisk get name,size,freespace
FreeSpace Name Size
356353708032 C: 536394588160
348834766848 D: 500104687616
E:
68748967936 G: 128278560768
1173479329792 H: 3000579911680
E:是没有磁盘的蓝光驱动器。我无法想到一个简单的单行输入语句,可以处理E:行而不会出错。这就是我在回答你的另一个问题时使用rawxml的原因。自动化执行了所有标准化输入,转置重塑了数据。
但是,如果过滤到您知道将在所有列中包含数据的驱动器,则可以使用remove / format并将FIND添加到管道。然后,单个DATA Step可以读取结果,缩放字节并生成状态消息:
filename gspot pipe "wmic logicaldisk get name,size,freespace | find ""G:""";
%let status = G: *NOT FOUND*;
data _null_;
infile gspot;
attrib freespace length=8 name length=$2;
input freespace name;
freespaceGB = round(freespace / 1000**3,0.1);
* robust if, in case pipe delivered more than expected;
if name = "G:" then call symput ('status', "G: has " || cat(freespaceGB) || "GB free");
run;
%put NOTE: &=status;
如果您坚持使用XML输入法,您可以使用DATA跳过转置数据来生成状态消息。在这个例子中,没有来自wmic库的Proc COPY工作 - 转置直接从wmic库中读取数据:
* ... libname prep ... *;
libname wmic xmlv2 automap=replace xmlmap=wmicmap;
proc transpose data=wmic.property out=work.properties(drop=_name_) suffix=_text;
by instance_ordinal;
id property_name;
var value;
run;
您可以直接在转置输出数据集选项中进行过滤,例如out=work.properties(drop=_name_ where=(name_text="G:"))
生成您的状态消息
%let status = G: *NOT FOUND*;
data _null_;
set work.properties;
freespace = input (freespace_text, best12.);
freespaceGB = round(freespace / 1000**3,0.1);
if name_text = "G:" then call symput ('status', "G: has " || cat(freespaceGB) || "GB free");
run;
%put NOTE: &=status;
答案 1 :(得分:2)
wmic
似乎有点矫枉过正 - 如果你想要的只是自由空间,那么普通的dir
怎么样?
filename cmd pipe "dir G:\ | findstr /c:""bytes free""";
data _null_;
infile cmd;
input;
free_space_gb = input(scan(_infile_,3,' '), comma20.) * 2**-30;
put "There is currently " free_space_gb 8.2 "GB of free space on the G drive";
call symput('free_space_gb',free_space_gb); /*Create macro variable*/
run;
%macro print_alert_html;
%if &free_space_gb < 1 %then %do;
ods listing close;
ods html file = "%sysfunc(pathname(work))\report.html";
ods html text = "Alert: only &free_space_gb GB of space left on the G drive!";
ods html close;
ods listing;
%end;
%mend;
然后你需要运行宏,如果它触发你应该能够打开它生成的html报告:
%print_alert_html;
x explorer "%sysfunc(pathname(work))\report.html";
答案 2 :(得分:2)
不确定为什么简单输出会使列的顺序与您要求的顺序不同。但如果它始终如一,那么它应该不难。要处理缺少的FREESPACE值,您可以尝试预读前两个字符。
data disks ;
infile 'wmic logicaldisk get name,size,freespace' pipe firstobs=2 truncover ;
length name $2 size freespace 8 ;
input name $2. @1 @ ;
if name = ' ' then input name ;
else input freespace name size ;
if name ne ' ';
format size freespace comma32. ;
run;
所以测试你的示例输出。
data disks ;
infile cards firstobs=2 truncover ;
length name $2 size freespace 8 ;
input name $2. @1 @ ;
if name = ' ' then input name ;
else input freespace name size ;
if name ne ' ';
format size freespace comma32. ;
cards;
FreeSpace Name Size
356353708032 C: 536394588160
348834766848 D: 500104687616
E:
68748967936 G: 128278560768
1173479329792 H: 3000579911680
;
产生这个结果。