Tuesday, April 17, 2007

Automate "save as" and file listings generation


Haven't you ever faced with a need to convert set of your documentation (say Visio diagrams) so that it supports previous version of the software if compared to the one you used to create it? It's often the case that we as a software company run a newer version of the software if compared to our customers.

One of the possible solution is to avoid the problem and mandate that a project team uses Visio 2002 (or Word 2000). Another option is to go file by file and open-save as previous version. I liked the third option the most - have a small script to automate it:

//*************************************************************
// WSH Script to automate Visio 2003 Save As Visio 2002 Process
//**************************************************************


var sPath = WScript.Arguments(0);

var sCurrentNesting = "\\";

var oVisio = WScript.CreateObject("Visio.Application");
var oFOS = WScript.CreateObject("Scripting.FileSystemObject");


var oFolder = oFOS.GetFolder(sPath);

iterate(oFolder);


function iterate(oFolder) {
var oFiles = new Enumerator(oFolder.Files);
for (; !oFiles.atEnd(); oFiles.moveNext()) {
var oFile = oFiles.item();
if (oFOS.GetExtensionName(oFile.Path) == "vsd") {
// WScript.Echo(oFile.Path);

oVisio.Visible = false;
oVisio.Documents.Open(oFile.Path)

// reset version to 2002
oVisio.ActiveDocument.Version = 393216;

oVisio.AlertResponse = 6; //Yes
oVisio.ActiveDocument.SaveAs(oFile.Path);
oVisio.AlertResponse = 0; // Default

oVisio.ActiveDocument.Close();
}

}

var sPreviousContext = sCurrentNesting;

var oSubFolders = new Enumerator(oFolder.SubFolders);
for (; !oSubFolders.atEnd(); oSubFolders.moveNext()) {
var oSubFolder = oSubFolders.item();
sCurrentNesting += oSubFolder.Name + "\\";

iterate(oSubFolder);

sCurrentNesting = sPreviousContext;
}
}

You can modify it to do the same thing for your Word or Excel files. Or may be do something else with them.

Yesterday I needed a list of files recursively from the current folder populated into Excel spreadsheet. Just have a bunch of documents to go through so decided to have a tracking sheet to make sure I don't miss anything and someone else after me has a categorized list with filtered out duplicates, etc. I remembered the small script I created a few years ago (the one presented above) and thought I would update it a little to generate a list for me. I ended up with a much simpler solution:

dir /a /b /s > filelisting.txt

:) you can make "Generate File Listing" a command in your explorer context menu. If you interested how - read here

Cheers

No comments: