Problem
On Windows, you want to list files and folder similar to the du utility found on Linux/Unix systems.
Solution
Use du.js script:
/*
* du.js
* Print and calculate files and folders space usage for a given directory.
* Size printed by default in bytes.
*
* Usage:
* cscript.exe du.js [directory] [/bs:blockSize]
*
* directory -- optional parameter of the directory to use. If omitted then current direcotry is used
* blockSize -- optional parameter. Shows sizes in Kilobytes, Megabytes or Gigabytes.
* Accepts values K, M or G for Kilobytes, Megabytes and Gigabytes respectively.
* If omitted the sizes are show in bytes. This is the default.
*
*
*/
String.prototype.rpad = function(len, char) {
var str = this.toString();
var deltaLen = len - str.length;
for (i=0; i<deltaLen; i++) str = str.concat(char);
return str;
}
var folderSpec;
if (WScript.Arguments.Unnamed.length > 0) {
folderSpec = WScript.Arguments.Unnamed.Item(0);
} else {
var WshShell = WScript.CreateObject("WScript.Shell");
folderSpec = WshShell.CurrentDirectory;
}
var blockSize = 1;
if (WScript.Arguments.Named.length > 0) {
var bsValues = new ActiveXObject("Scripting.Dictionary");
bsValues.add("K", 1024);
bsValues.add("M", 1024*1024);
bsValues.add("G", 1024*1024*1024);
if(bsValues.exists(WScript.Arguments.Named.Item("bs"))) {
blockSize = bsValues.Item(WScript.Arguments.Named.Item("bs"));
} else {
blockSize = 1;
}
}
WScript.echo(new Date() + "\n");
WScript.echo(folderSpec + "\n");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var folder = fso.GetFolder(folderSpec);
var folders = new Enumerator(folder.SubFolders);
var files = new Enumerator(folder.Files);
var resultOutput = "";
var total = calcSizes(folders) + calcSizes(files);
resultOutput = resultOutput.concat("\n" + "Total:\t" + Math.round(total / blockSize) );
WScript.echo(resultOutput);
function calcSizes(objs) {
var size = 0;
for (; !objs.atEnd(); objs.moveNext()) {
try {
WScript.echo( (new String(Math.round(objs.item().Size / blockSize))).rpad(8," ") + "\t" + objs.item().Name + ((objs.item().Type == "File Folder") ? "\\" : "") );
size += objs.item().Size;
} catch(e) {
WScript.echo(e.message + " " + objs.item().Name);
}
}
return size;
}
Usage:
cscript.exe du.js [directory] [/bs:blockSize]
cscript.exe du.js [directory] [/bs:blockSize]
- directory -- optional parameter of the directory to use. If omitted then current direcotry is used
- blockSize -- optional parameter. Shows sizes in Kilobytes, Megabytes or Gigabytes. Accepts values K, M or G for Kilobytes, Megabytes and Gigabytes respectively. If omitted the sizes are show in bytes. This is the default.
Example
C:\scripts>cscript.exe du.js c:\
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Thu Sep 11 12:35:07 UTC+0500 2014
c:\
4543005 $Recycle.Bin
1041265185 app
Permission denied Documents and Settings
0 PerfLogs
1192361151 Program Files
Permission denied ProgramData
148009089 Recovery
2266 scripts
Permission denied System Volume Information
Permission denied Users
Permission denied Windows
24 autoexec.bat
10 config.sys
1073741824 pagefile.sys
Total: 3459922554
C:\scripts>
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Thu Sep 11 12:35:07 UTC+0500 2014
c:\
4543005 $Recycle.Bin
1041265185 app
Permission denied Documents and Settings
0 PerfLogs
1192361151 Program Files
Permission denied ProgramData
148009089 Recovery
2266 scripts
Permission denied System Volume Information
Permission denied Users
Permission denied Windows
24 autoexec.bat
10 config.sys
1073741824 pagefile.sys
Total: 3459922554
C:\scripts>
No comments:
Post a Comment