-
Notifications
You must be signed in to change notification settings - Fork 5
/
bbg2xlsx.js
140 lines (102 loc) · 3.42 KB
/
bbg2xlsx.js
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
// Converts (Bloomberg) xlx (2003) files, in argument DIRPATH and its subfolders, to xlsx files
// Written by Antonio FASANO
// University of Rome LUISS and Salerno
// Last revision Oct 2015
// Usage:
// cscript bbg2xlsx.js dirpath
//Get Args
var argsUnnamed = WScript.Arguments.Unnamed,
nArgs=WScript.Arguments.length,
fso=fso = new ActiveXObject("Scripting.FileSystemObject");
//No arg at all
if (nArgs==0) ShowUsage ();
//Get and check rootDir
var rootDir=argsUnnamed(0);
if (!IsFolder(rootDir)) WScript.Quit();
rootDir= fso.GetFolder(rootDir)
//User warning
WScript.Echo("I might overwrite xlsx in the tree: " + rootDir.Path + '\nProceed (y/N)');
var ans = WScript.StdIn.ReadLine();
if(ans.toUpperCase() != 'Y') WScript.Quit();
//Get xls recursively
var files=listFiles(rootDir.Path, '\\.xls$');
// Loop and convert them
for (k in files) {
//Get file parts
var xls = fso.GetFile(files[k]),
xlsPath=fso.GetFile(xls),
xlsName= fso.GetFileName(xls),
xlsBase= fso.GetBaseName(xls),
xlsFolder=fso.GetParentFolderName(xls),
xlsxPath=xlsFolder + '\\' + xlsBase + '.xlsx';
//Delete conflicting xlsx
if(fso.FileExists(xlsxPath)) fso.DeleteFile(xlsxPath);
//Open xls file
ExcelApp = new ActiveXObject("Excel.Application");
ExcelBook = ExcelApp.Workbooks.Open (xlsPath);
//Save as xlsx
WScript.Echo('Converting ' + xlsPath);
var xlWorkbookDefault=51
ExcelBook.SaveAs(Filename=xlsxPath, FileFormat=xlWorkbookDefault);
ExcelBook.Close(SaveChanges = false);
ExcelApp.Quit();
}
//==========================================================
//Service Functions
function GetFullPath(file){
var WshShell = WScript.CreateObject ("WScript.Shell");
//No ':' no full path. Then attach File name to curr dir
if (file.match(/:/)==null)
file=WshShell.CurrentDirectory + "\\" + file;
return file;
}
function IsFile (file){
var fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FileExists(file))
return true;
else{
WScript.Echo("Cant find file '" + file + "'!")
return false;
}
}
function IsFolder (folder){
var fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FolderExists(folder))
return true;
else{
WScript.Echo("Cant find folder '" + folder + "'!")
return false;
}
}
//Return array with fullpath to files in dirpath and dirpath subirs matching re string
//E.g listFiles(".", '\\.xls$');
function listFiles(dirpath, re){
var fso = new ActiveXObject("Scripting.FileSystemObject"),
fileList =new Array();
recurseFiles(fso.GetFolder(dirpath), re, fileList);
return(fileList);
}
//listFiles service function
function recurseFiles(folder, re, fileList){
var ef = new Enumerator(folder.files);
while (! ef.atEnd()) {
var file = ef.item();
if (file.name.match(re)){
//WScript.Echo(file.name);
fileList.push(file.Path)
}
ef.moveNext();
}
var ed = new Enumerator(folder.SubFolders);
while (! ed.atEnd()) {
var subfolder = ed.item();
recurseFiles(subfolder, re, fileList);
ed.moveNext();
}
}
function ShowUsage (){
WScript.Echo("USAGE:");
WScript.Echo("cscript bbg2xlsx.js dirpath");
WScript.Echo("All xls files in dirpath and its subdirs will be converted to xlsx");
WScript.Quit();
}