Skip to content

Commit cb2d44b

Browse files
author
clay_shooter
committed
Sample disk utils program based on example posted to SourceForge
1 parent bb54b06 commit cb2d44b

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.jacob.samples.system;
2+
3+
import java.text.DecimalFormat;
4+
5+
import com.jacob.activeX.ActiveXComponent;
6+
import com.jacob.com.ComThread;
7+
import com.jacob.com.Dispatch;
8+
import com.jacob.com.Variant;
9+
10+
/**
11+
* Example VB script that grabs hard drive properties.
12+
* <p>
13+
* Source Forge posting http://sourceforge.net/forum/forum.php?thread_id=1785936&forum_id=375946
14+
* <p>
15+
* Enhance by clay_shooter with info from http://msdn2.microsoft.com/en-us/library/d6dw7aeh.aspx
16+
*
17+
* @author qstephenson
18+
*
19+
*/
20+
public class DiskUtils {
21+
22+
/** formatters aren't thread safe but the sample only has one thread */
23+
private static DecimalFormat sizeFormatter = new DecimalFormat("###,###,###,###");
24+
25+
/** a pointer to the scripting file system object */
26+
private ActiveXComponent fileSystemApp = null;
27+
28+
/** the dispatch that points at the drive this DiskUtil operates against */
29+
private Dispatch myDrive = null;
30+
31+
/**
32+
* Standard constructor
33+
*/
34+
public DiskUtils(String drive){
35+
setUp(drive);
36+
}
37+
38+
/**
39+
* open the connection to the scripting object
40+
*/
41+
public void setUp(String drive){
42+
if (fileSystemApp == null){
43+
ComThread.InitSTA();
44+
fileSystemApp = new ActiveXComponent(
45+
"Scripting.FileSystemObject");
46+
myDrive = Dispatch.call(fileSystemApp, "GetDrive", drive)
47+
.toDispatch();
48+
}
49+
}
50+
51+
/**
52+
* Do any needed cleanup
53+
*/
54+
public void tearDown() {
55+
ComThread.Release();
56+
}
57+
58+
/**
59+
* convenience method
60+
* @return driver serial number
61+
*/
62+
public int getSerialNumber() {
63+
return Dispatch.get(myDrive, "SerialNumber").getInt();
64+
}
65+
66+
/**
67+
* Convenience method.
68+
* We go through these formatting hoops so we can make the size string pretty.
69+
* We wouldn't have to do that if we didn't mind long strings with Exxx at the end
70+
* or the fact that the value returned can vary in size based on the size of the disk.
71+
* @return driver total size of the disk
72+
*/
73+
public String getTotalSize() {
74+
Variant returnValue = Dispatch.get(myDrive, "TotalSize");
75+
if (returnValue.getvt() == Variant.VariantDouble){
76+
return sizeFormatter.format(returnValue.getDouble());
77+
} else if (returnValue.getvt() == Variant.VariantInt){
78+
return sizeFormatter.format(returnValue.getInt());
79+
} else {
80+
return "Don't know type: "+returnValue.getvt();
81+
}
82+
}
83+
84+
/**
85+
* Convenience method.
86+
* We wouldn't have to do that if we didn't mind long strings with Exxx at the end
87+
* or the fact that the value returned can vary in size based on the size of the disk.
88+
* @return driver free size of the disk
89+
*/
90+
public String getFreeSpace() {
91+
Variant returnValue = Dispatch.get(myDrive, "FreeSpace");
92+
if (returnValue.getvt() == Variant.VariantDouble){
93+
return sizeFormatter.format(returnValue.getDouble());
94+
} else if (returnValue.getvt() == Variant.VariantInt){
95+
return sizeFormatter.format(returnValue.getInt());
96+
} else {
97+
return "Don't know type: "+returnValue.getvt();
98+
}
99+
}
100+
101+
/**
102+
*
103+
* @return file system on the drive
104+
*/
105+
public String getFileSystemType() {
106+
//figure ot the actual variant type
107+
//Variant returnValue = Dispatch.get(myDrive, "FileSystem");
108+
//System.out.println(returnValue.getvt());
109+
return Dispatch.get(myDrive, "FileSystem").getString();
110+
}
111+
112+
/**
113+
*
114+
* @return volume name
115+
*/
116+
public String getVolumeName() {
117+
return Dispatch.get(myDrive, "VolumeName").getString();
118+
}
119+
/**
120+
* Simple main program that creates a DiskUtils object and queries for the C: drive
121+
*/
122+
public static void main(String[] args) {
123+
//DiskUtils utilConnection = new DiskUtils("F");
124+
DiskUtils utilConnection = new DiskUtils("C");
125+
System.out.println("Disk serial number is: "+ utilConnection.getSerialNumber());
126+
System.out.println("FileSystem is: "+ utilConnection.getFileSystemType());
127+
System.out.println("Volume Name is: "+ utilConnection.getVolumeName());
128+
System.out.println("Disk total size is: "+ utilConnection.getTotalSize());
129+
System.out.println("Disk free space is: "+ utilConnection.getFreeSpace());
130+
utilConnection.tearDown();
131+
}
132+
}

0 commit comments

Comments
 (0)