forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivex-scripting-tests.ts
326 lines (275 loc) · 11.3 KB
/
activex-scripting-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/// <reference types="windows-script-host" />
// Note -- running these tests under cscript requires some ES5 polyfills
const collectionToArray = <T>(col: { Item(key: any): T }): T[] => {
const results: T[] = [];
const enumerator = new Enumerator<T>(col);
enumerator.moveFirst();
while (!enumerator.atEnd()) {
results.push(enumerator.item());
enumerator.moveNext();
}
return results;
};
const fso = new ActiveXObject('Scripting.FileSystemObject');
// https://msdn.microsoft.com/en-us/library/ebkhfaaz(v=vs.84).aspx
{
/** Generates a string describing the drive type of a given Drive object. */
const driveTypeString = (drive: Scripting.Drive) => {
switch (drive.DriveType) {
case Scripting.DriveTypeConst.Removable:
return 'Removeable';
case Scripting.DriveTypeConst.Fixed:
return 'Fixecd';
case Scripting.DriveTypeConst.Remote:
return 'Network';
case Scripting.DriveTypeConst.CDRom:
return 'CD-ROM';
case Scripting.DriveTypeConst.RamDisk:
return 'RAM Disk';
default:
return 'Unknown';
}
};
/** Generates a string describing the attributes of a file or folder. */
const attributesString = (f: Scripting.File | Scripting.Folder) => {
const attr = f.Attributes;
if (attr === 0) {
return 'Normal';
}
const attributeStrings: string[] = [];
if (attr & Scripting.FileAttribute.Directory) { attributeStrings.push('Directory'); }
if (attr & Scripting.FileAttribute.ReadOnly) { attributeStrings.push('Read-only'); }
if (attr & Scripting.FileAttribute.Hidden) { attributeStrings.push('Hidden'); }
if (attr & Scripting.FileAttribute.System) { attributeStrings.push('System'); }
if (attr & Scripting.FileAttribute.Volume) { attributeStrings.push('Volume'); }
if (attr & Scripting.FileAttribute.Archive) { attributeStrings.push('Archive'); }
if (attr & Scripting.FileAttribute.Alias) { attributeStrings.push('Alias'); }
if (attr & Scripting.FileAttribute.Compressed) { attributeStrings.push('Compressed'); }
return attributeStrings.join(',');
};
const drivesInfoReport = () => {
const driveLine = (d: Scripting.Drive) => {
let parts: Array<string | number> = [
d.DriveLetter,
d.Path,
driveTypeString(d),
d.IsReady ? 'true' : 'false'
];
if (d.IsReady) {
parts = parts.concat([
d.DriveType === Scripting.DriveTypeConst.Remote ? d.ShareName : d.VolumeName,
d.FileSystem,
d.TotalSize,
d.FreeSpace,
d.AvailableSpace,
d.SerialNumber.toString(16)
]);
}
return parts.join(' ');
};
const ret = `
Number of drives: ${fso.Drives.Count}
Drive File Total Free Available Serial
Letter Path Type Ready? Name System Space Space Space Number
${new Array(106).join('-')}
${collectionToArray(fso.Drives).map(driveLine).join('\n')}`
.trim().replace(' ', '\t');
};
type fiileFolderKey = keyof Scripting.File & keyof Scripting.Folder;
type keys = fiileFolderKey | 'Attribs' | 'Created' | 'Accessed' | 'Modified';
const detailBuilder = (f: Scripting.File | Scripting.Folder, x: fiileFolderKey | 'Attribs' | 'Created' | 'Accessed' | 'Modified') => {
if (x === 'Attribs') { return attributesString(f); }
const label = x;
switch (x) {
case 'Created':
x = 'DateCreated';
break;
case 'Accessed':
x = 'DateLastAccessed';
break;
case 'Modified':
x = 'DateLastModified';
break;
}
return `${label}\t${f[x]}\n`;
};
const fileDetailsString = (f: Scripting.File) => {
const keys: keys[] = ['Path', 'Name', 'Type', 'Attribs', 'Created', 'Accessed', 'Modified', 'Size'];
return keys.map(x => detailBuilder(f, x)).join('');
};
const folderDetailsString = (f: Scripting.Folder) => {
const keys: keys[] = ['Path', 'Name', 'Attribs', 'Created', 'Accessed', 'Modified', 'Size'];
return keys.map(x => detailBuilder(f, x)).join('');
};
const folderContentsString = (f: Scripting.Folder): string => {
const files = collectionToArray(f.Files);
const subfolders = collectionToArray(f.SubFolders);
return `
Folder: ${f.Path}
There ${files.length === 1 ? 'is 1 file' : `are ${files.length} files`}
${files.map(fileDetailsString).join('')}
There ${subfolders.length === 1 ? 'is 1 subfolder' : `are ${subfolders.length} subfolders`}
${subfolders.map(folderDetailsString).join('')}
${subfolders.map(folderContentsString).join('')}`
.trim().replace(' ', '\t');
};
const testDrive = 'C:\\';
const testPath = 'C:\\test';
const testfolderInfo = () => {
if (!fso.DriveExists(testDrive) || !fso.FolderExists(testPath)) { return ''; }
return folderContentsString(fso.GetFolder(testPath));
};
const deleteTestFolder = () => {
// two ways to delete a file:
const filepathToDelete = `${testPath}\\LoremIpsum\\Paragraph1.txt`;
fso.DeleteFile(filepathToDelete);
// fso.GetFile(filepathToDelete).Delete();
// two ways to delete a folder:
fso.DeleteFolder(`${testPath}\\LoremIpsum`);
fso.GetFolder(testPath).Delete();
};
const createLyrics = (folder: Scripting.Folder) => {
let stream = folder.CreateTextFile('Paragraph1.txt');
stream.Write('Lorem Ipsum - Paragraph 1');
stream.WriteBlankLines(1);
stream.WriteLine('Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sem.');
stream.WriteLine('Donec ante. Nulla facilisi. Phasellus interdum nulla a nunc. Morbi laoreet nunc.');
stream.WriteBlankLines(2);
stream.Close();
stream = folder.CreateTextFile('Paragraph2.txt');
stream.WriteLine('Lorem Ipsum - Paragraph 2');
stream.WriteBlankLines(1);
stream.WriteLine('Nullam nulla quam, sollicitudin ut, lobortis ornare, tristique a, augue.');
stream.WriteLine('Vestibulum sem felis, fermentum eu, volutpat eu, vulputate eget, elit. ');
stream.WriteBlankLines(2);
stream.Close();
};
const getLyrics = () => {
const folderPath = `${testPath}\\LoremIpsum`;
let lyrics = '';
// One way to read from a file
let stream = fso.OpenTextFile(fso.BuildPath(folderPath, 'Paragraph1.txt'));
lyrics = stream.ReadAll() + '\n\n';
stream.Close();
// Another way to read from a file
stream = fso.GetFile(fso.BuildPath(folderPath, 'Paragraph2.txt')).OpenAsTextStream();
while (!stream.AtEndOfStream) {
lyrics += stream.ReadLine() + '\n';
}
stream.Close();
return lyrics;
};
const buildTestFolder = () => {
// Bail out if the drive doesn't exists ...
if (!fso.DriveExists(testDrive)) { return false; }
// or the directory already exists
if (fso.FolderExists(testPath)) { return false; }
const testFolder = fso.CreateFolder(testPath);
const stream = fso.CreateTextFile(fso.BuildPath(testPath, 'readme.txt'));
stream.WriteLine('My sample text collection');
stream.Close();
const subfolder = testFolder.SubFolders.Add('LoremIpsum');
createLyrics(subfolder);
return true;
};
const echoLines = (linecount = 1) => new Array(linecount).forEach(() => WScript.Echo(''));
if (!buildTestFolder()) {
WScript.Echo('Test directory already exists or cannot be created. Cannot continue.');
} else {
drivesInfoReport();
echoLines(2);
WScript.Echo(testfolderInfo());
echoLines(2);
WScript.Echo(getLyrics());
echoLines(2);
deleteTestFolder();
}
}
// source --https://msdn.microsoft.com/en-us/library/ts2t8ybh(v=vs.84).aspx
{
const showFreeSpace = (drvPath: string) => {
const d = fso.GetDrive(fso.GetDriveName(drvPath));
let s = `Drive ${drvPath} - `;
s += d.VolumeName + '<br>';
s += `Free Space: ${d.FreeSpace / 1024} Kbytes`;
return (s);
};
}
// source -- https://msdn.microsoft.com/en-us/library/kaf6yaft(v=vs.84).aspx
{
const getALine = (filespec: string) => {
const file = fso.OpenTextFile(filespec, Scripting.IOMode.ForReading, false);
let s = '';
while (!file.AtEndOfLine) {
s += file.Read(1);
}
file.Close();
return (s);
};
}
// https://msdn.microsoft.com/en-us/library/ch28h2s7(v=vs.84).aspx
{
const showDriveInfo = (path: string) => {
const bytesPerGB = 1024 * 1024 * 1024;
const drv = fso.GetDrive(fso.GetDriveName(path));
const ret =
drv.IsReady ?
`${drv.Path} - ${drv.FreeSpace / bytesPerGB} GB free of ${drv.TotalSize / bytesPerGB} GB` :
'Not ready';
WScript.Echo(ret);
};
const showFolderInfo = () => {
const fldr = fso.GetFolder("c:\\");
let ret = `
Folder: ${fldr.Path}
Drive: ${fldr.Drive}
${fldr.IsRootFolder ? 'Is root folder' : `Parent folder: ${fldr.ParentFolder}`}
`.trim();
// Create and delete a folder.
const newFolderName = 'C:\\TempFolder1';
fso.CreateFolder(newFolderName);
ret += `Base Name of Added Folder: ${fso.GetBaseName(newFolderName)}`;
fso.DeleteFolder(newFolderName);
WScript.Echo(ret);
};
}
// https://msdn.microsoft.com/en-us/library/czxefwt8(v=vs.84).aspx
{
const readFiles = () => {
const file = fso.CreateTextFile("c:\\testfile.txt", true);
// Write a line.
WScript.Echo('Writing file');
file.WriteLine("Hello World");
file.WriteBlankLines(1);
file.Close();
// Read the contents of the file.
WScript.Echo('Reading file');
const textStream = fso.OpenTextFile("c:\\testfile.txt", Scripting.IOMode.ForReading);
WScript.Echo(`File contents = "${textStream.ReadLine()}"`);
textStream.Close();
};
const manipulateFiles = () => {
const file = fso.CreateTextFile("c:\\testfile.txt", true);
WScript.Echo('Writing file');
// Write a line.
file.Write("This is a test.");
// Close the file to writing.
file.Close();
WScript.Echo('Moving file to c:\\tmp');
// Get a handle to the file in root of C:\.
let file2 = fso.GetFile("c:\\testfile.txt");
// Move the file to \tmp directory.
file2.Move("c:\\tmp\\testfile.txt");
WScript.Echo('Copying file to c:\\temp <br>');
// Copy the file to \temp.
file2.Copy('c:\\temp\\testfile.txt');
WScript.Echo('Deleting files <br>');
// Get handles to files' current location.
file2 = fso.GetFile('c:\\tmp\\testfile.txt');
const file3 = fso.GetFile('c:\\temp\\testfile.txt');
// Delete the files.
file2.Delete();
file3.Delete();
WScript.Echo('All done!');
};
}