Skip to content

Instantly share code, notes, and snippets.

@teramako
Last active August 29, 2015 13:56
Show Gist options
  • Save teramako/9306751 to your computer and use it in GitHub Desktop.
Save teramako/9306751 to your computer and use it in GitHub Desktop.
Windows でディスク使用率等の一覧を表示する
@if(0)==(0) ECHO OFF
CScript //NoLogo //E:JScript "%~f0" %*
GOTO :EOF
@end
var COLUMN_INFOs = {
k: {
drive: { width: 5, align: "left" },
type: { width: 4, align: "left" },
total: { width: 14, align: "right" },
used: { width: 14, align: "right" },
free: { width: 14, align: "right" },
pct: { width: 4, align: "right" },
byteUnit: "K"
},
m: {
drive: { width: 5, align: "left" },
type: { width: 4, align: "left" },
total: { width: 14, align: "right" },
used: { width: 14, align: "right" },
free: { width: 14, align: "right" },
pct: { width: 4, align: "right" },
byteUnit: "M"
},
g: {
drive: { width: 5, align: "left" },
type: { width: 4, align: "left" },
total: { width: 9, align: "right" },
used: { width: 9, align: "right" },
free: { width: 9, align: "right" },
pct: { width: 4, align: "right" },
byteUnit: "G"
},
t: {
drive: { width: 5, align: "left" },
type: { width: 4, align: "left" },
total: { width: 9, align: "right" },
used: { width: 9, align: "right" },
free: { width: 9, align: "right" },
pct: { width: 4, align: "right" },
byteUnit: "T"
}
};
function main(args) {
var unit = args(0) || "g";
var columnInfo = COLUMN_INFOs[unit.toLowerCase()];
if (!columnInfo) {
WScript.Echo("argument must be 'k', 'm', 'g', 't' or empty.");
WScript.Quit(1);
}
showDrives(columnInfo);
}
main(WScript.Arguments);
function showHeader(aCI) {
//WScript.Echo("Drive Type Total Used Free % ValumeName");
//WScript.Echo("----- ---- ------- ------- ------- ---- ----------");
var unit = "(" + aCI.byteUnit + "B)";
WScript.Echo([
align("Drive", aCI.drive.width, aCI.drive.align),
align("Type", aCI.type.width, aCI.type.align),
align("Total" + unit, aCI.total.width, aCI.total.align),
align("Used" + unit, aCI.used.width, aCI.used.align),
align("Free" + unit, aCI.free.width, aCI.free.align),
align("%", aCI.pct.width, aCI.pct.align),
"ValumeName"
].join(" "));
WScript.Echo([
StringRepeat("-", aCI.drive.width),
StringRepeat("-", aCI.type.width),
StringRepeat("-", aCI.total.width),
StringRepeat("-", aCI.used.width),
StringRepeat("-", aCI.free.width),
StringRepeat("-", aCI.pct.width),
StringRepeat("-", 10)
].join(" "));
}
function showDrives(aColumnInfo) {
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var e = new Enumerator(FSO.Drives);
showHeader(aColumnInfo);
for (; !e.atEnd(); e.moveNext()) {
showDriveInfo(e.item(), aColumnInfo);
}
}
// Drive Type Size Used Aviable Percent
// ----- ----
function showDriveInfo (aDrive, aCI) {
var msg;
if (aDrive.IsReady) {
var usedSize = aDrive.TotalSize - aDrive.FreeSpace;
msg = [
align(aDrive.DriveLetter + ":", aCI.drive.width, aCI.drive.align),
align(getDriveType(aDrive), aCI.type.width, aCI.type.align),
align(toByte(aDrive.TotalSize, aCI.byteUnit, 2), aCI.total.width, aCI.total.align),
align(toByte(usedSize, aCI.byteUnit, 2), aCI.used.width, aCI.used.align),
align(toByte(aDrive.FreeSpace, aCI.byteUnit, 2), aCI.free.width, aCI.free.align),
align((usedSize / aDrive.TotalSize * 100).toFixed(0)+"%", aCI.pct.width, aCI.pct.align),
aDrive.VolumeName
].join(" ");
} else {
msg = [
align(aDrive.DriveLetter + ":", aCI.drive.width, aCI.drive.align),
align(getDriveType(aDrive), aCI.type.width, aCI.type.align),
align("-", aCI.total.width, aCI.total.align),
align("-", aCI.used.width, aCI.used.align),
align("-", aCI.free.width, aCI.free.align),
align("-", aCI.pct.width, aCI.pct.align)
].join(" ");
}
WScript.Echo(msg);
}
function toByte (aNum, aByteType, aFixSize, aShowUnit) {
var byteSize = 1;
var postfix = aByteType.toUpperCase();
switch (postfix) {
case "K":
byteSize = 1024;
break;
case "M":
byteSize = 1024 * 1024;
break;
case "G":
byteSize = 1024 * 1024 * 1024;
break;
case "T":
byteSize = 1024 * 1024 * 1024 * 1024;
break;
default:
throw new TypeError("Unknown ByteType: " + aByteType);
}
return (aNum / byteSize).toFixed(aFixSize) + (aShowUnit ? postfix :"");
}
function align (aStr, aSize, aType) {
var spaces = StringRepeat(" ", aSize);
switch (aType) {
case undefined:
case 0:
case "left":
return (aStr + spaces).slice(0, aSize);
case 1:
case "right":
return (spaces + aStr).slice(-aSize);
default:
throw new TypeError("Unknown AlignmentType");
}
}
function getDriveType (aDrive) {
switch (aDrive.DriveType) {
case 1: // Removable
return "RDD";
case 2: // HardDisk
return "HDD";
case 3: // NetworkDirve
return "Net";
case 4: // CD
return "CDD";
case 5: // RAM Disk
return "RAM";
case 0:
default: // Unknown
return "???";
}
}
function StringRepeat (aStr, aCount) {
var result = "";
for(;;) {
if (aCount & 1) {
result += aStr;
}
aCount >>= 1;
if (aCount) {
aStr += aStr;
} else {
break;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment