Using JScript for uninstall script - installation

I am building an installer in VS2010 and want a script to run after uninstall (to remove license files). I have found JScript as a scripting language for Windows 7 and have implemented a simple script to delete a directory (which works fine):
var wshShell = WScript.CreateObject("WScript.Shell");
var result = wshShell.Popup("Remove license?", 0, "Remove license?", 4);
if (result == 6) {
var license_dir = wshShell.ExpandEnvironmentStrings("%ProgramData%");
license_dir += "\\<my product>";
var fso;
fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FolderExists(license_dir)) {
fso.DeleteFolder(license_dir, true);
} else {
WScript.Echo(license_dir + " didn't exist. Nothing removed.");
}
}
My question is, is this a valid and (more importantly) portable way of doing this?

Related

How to set default file browse location with firefox addon sdk

Im new Firefox addon programming.
I want set default file browse location with firefox addon sdk.
Thank you so much.
open scratchpad copy and paste this:
const nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["#mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
var startDir = FileUtils.File('C:\\');
fp.displayDirectory = startDir;
fp.init(window, "Dialog Title", nsIFilePicker.modeOpen);
fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);
var rv = fp.show();
if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
var file = fp.file;
// Get the path as string. Note that you usually won't
// need to work with the string paths.
var path = fp.file.path;
// work with returned nsILocalFile...
}
if thats what you want let me know, then ill put it in a default location

print pdf directly to printer windows

I have a windows app that prints pdfs directly to a printer. Everything is working but for some reason for each pdf to print I see the pdf reader Nitro Pro pop up in the background then close.
Is there a way to keep the window from poping up. It does not seem to effect the over application but just kind of annoying.
private void PrintDocument(string printer, string fileName)
{
ProcessStartInfo info = new ProcessStartInfo
{
Arguments = "\"" + printer + "\"",
Verb = "PrintTo",
FileName = fileName,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true
};
Process p = new Process { StartInfo = info };
p.Start();
p.WaitForExit(5000);
if (p.HasExited == false)
{
p.Kill();
}
}
This is not possible.
Windows can't print a file directly, it relies on a program to do so. It will use whatever application has configured itself to handle the PrintTo verb for the particular file extension. In your case it appears the application is Nitro Pro.
It's possible that you can find and install an application that can print the file without opening a window to do so, but that's beyond the scope of StackOverflow.

Script Works on Win 7, Not on Server 2003

I have a script that is rather simple, it boots up WinSCP and checks the directory for a file that starts with "TSA". If the file exists, it exits, if it does not exist, it transfers over a new file.
Its up and running on my Windows 7 machine, that is where i created it - but when i transfer it over to my server [windows server 2003] it never finds the file.
My script:
var FILEPATH = "../zfinance/TSA";
// Session to connect to
var SESSION = "someplace#somewhere.com";
// Path to winscp.com
var WINSCP = "c:\\program files\\winscp\\winscp.com";
var filesys = WScript.CreateObject("Scripting.FileSystemObject");
var shell = WScript.CreateObject("WScript.Shell");
var logfilepath = filesys.GetSpecialFolder(2) + "\\" + filesys.GetTempName() + ".xml";
var p = FILEPATH.lastIndexOf('/');
var path = FILEPATH.substring(0, p);
var filename = FILEPATH.substring(p + 1);
var exec;
// run winscp to check for file existence
exec = shell.Exec("\"" + WINSCP + "\" /log=\"" + logfilepath + "\"");
exec.StdIn.Write(
"option batch abort\n" +
"open \"" + SESSION + "\"\n" +
"ls \"" + path + "\"\n" +
"exit\n");
// wait until the script finishes
while (exec.Status == 0)
{
WScript.Sleep(100);
WScript.Echo(exec.StdOut.ReadAll());
}
if (exec.ExitCode != 0)
{
WScript.Echo("Error checking for file existence");
WScript.Quit(1);
}
// look for log file
var logfile = filesys.GetFile(logfilepath);
if (logfile == null)
{
WScript.Echo("Cannot find log file");
WScript.Quit(1);
}
// parse XML log file
var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.async = false;
doc.load(logfilepath);
doc.setProperty("SelectionNamespaces",
"xmlns:w='http://winscp.net/schema/session/1.0'");
doc.setProperty("SelectionLanguage", "XPath");
var nodes = doc.selectNodes("//w:file/w:filename[starts-with(#value, '" + filename + "')]");
if (nodes.length > 0)
{
WScript.Echo("File found");
WScript.Quit(0);
}
else
{
WScript.Echo("File not found");
WScript.Quit(1);
}
After much investigation, i think i've found the piece of code that does not function properly:
// parse XML log file
var doc = new ActiveXObject("MSXML2.DOMDocument.6.0");
doc.async = false;
doc.load(logfilepath);
doc.setProperty("SelectionNamespaces",
"xmlns:w='http://winscp.net/schema/session/1.0'");
The only problem is, i have no idea why. The log file at this point should be written over with the xml code, but this does not happen.
Thanks in advance for any help.
And the answer is........... WinSCP on Windows Server 2003 was WAY out of date. So out of date that the log was completely different from one version to the next. Updated and VIOLA! Problem solved. Thanks for your help.
Maybe you need to install MSXML2.DOMDocument.6.0
http://msdn.microsoft.com/en-us/library/windows/desktop/cc507436%28v=vs.85%29.aspx
If you open up regedit and look for "MSXML2.DOMDocument.6.0" does it find it? If so maybe security settings for the script need to be set in order to be able to create an activeX object.
What can you see when you put some stuff in try catch?
try{
//stuff
}catch(e){
WScript.Echo(e.message);
}

Visual Studio Console Application Installer - Schedule Windows Task

Does anyone know how to create an installation project using Visual Studio 2010 that creates a Windows Scheduler task? I'm building an installer for a Console Application that needs to run every X minutes, and it would be nice for the customer not to have to schedule it manually.
Thanks!
in Wix (.wixproj) you can do it in a CustomAction, written in Jscript, that invokes Schtasks.exe .
I don't know about VS2010's support of WIX, I think it is built-in.
The custom action module (the .js file) should have a function to run a schtasks command, something like this:
function RunSchtasksCmd(command, deleteOutput) {
deleteOutput = deleteOutput || false;
var shell = new ActiveXObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var tmpdir = fso.GetSpecialFolder(SpecialFolders.TemporaryFolder);
var tmpFileName = fso.BuildPath(tmpdir, fso.GetTempName());
var windir = fso.GetSpecialFolder(SpecialFolders.WindowsFolder);
var schtasks = fso.BuildPath(windir,"system32\\schtasks.exe") + " " + command;
// use cmd.exe to redirect the output
var rc = shell.Run("%comspec% /c " + schtasks + "> " + tmpFileName, WindowStyle.Hidden, true);
if (deleteOutput) {
fso.DeleteFile(tmpFileName);
}
return {
rc : rc,
outputfile : (deleteOutput) ? null : tmpFileName
};
}
And then, use that from within the custom action function itself, something like this
var r = RunSchtasksCmd("/Create Foo bar baz");
if (r.rc !== 0) {
// 0x80004005 == E_FAIL
throw new Error("exec schtasks.exe returned nonzero rc ("+r.rc+")");
}
var fso = new ActiveXObject("Scripting.FileSystemObject");
var textStream = fso.OpenTextFile(r.outputfile, OpenMode.ForReading);
// Read from the file and parse the results.
while (!textStream.AtEndOfStream) {
var oneLine = textStream.ReadLine();
var line = ParseOneLine(oneLine); // look for errors? success?
}
textStream.Close();
fso.DeleteFile(r.outputfile);
Some people say writing CA's in script is the wrong thing to do, because they are hard to maintain, hard to debug, or it's hard to do them right. I think those are bad reasons. CA's implemented correctly in script, work well.
WIX has its own custom action for creating windows task and scheduling them.
<CustomAction Id="CreateScheduledTask" Return="check" Directory="Application" ExeCommand=""[SystemFolder]SCHTASKS.EXE" /CREATE /SC DAILY /TN "My Task" /ST 12:00:00 /TR "[INSTALLLOCATION]Apps\File.exe" /RU [%USERDOMAIN]\[LogonUser]" />
Above command will create a task with name 'My Task' which will execute everyday at 12 AM.
SCHTASKS command is used to create and schedule a windows task.

Windows command-line disk cataloging - save to csv?

I'm working on a Windows batch script that creates a directory/file listing of a complete hard disk for archival/cataloging purposes, using only command line-tools (and open-source/free tools). For each of the entries in the listing I wanted to list the filename, directory where it resides in, the filesize, date a,nd time of the file, and the md5 sum. I have been able to create somewhat a working starting point, but I'm hitting a wall since I'm not sure if it is even possible using the command-line tools in Windows. The command "dir /s /a:-d /o:-d /t:c" gives me a nice overview, but I would like this overview displayed (or saved to) a comma-delimited format. So my questions are:
Can I create a csv file with all the fields I mentioned above, with the standard command-line tools (and a m5 freeware tool for the md5 sums)
Do you know of a better way, or is there a dead simple disk cataloging command-line tool I missed?
Thanks in advance for any tips!
You can use dir /s /a:-d /o:-d /t:c > slam.txt
Then the content of this slam.txt, can be processed by WScript in windows, making a CSV file ...
If you need a WScript ex, I can provide one ?
I know this not an CSV example - but it should be complex enough for pattern inspiration :)
and remember this fil is saved as .js
var what2lookfor = '<rect ';
var forReading = 1, forWriting = 2, forAppending = 8, jx = 0, ix = 0;
var triStateUseDefault = -2, triStateTrue = -1, triStateFalse = 0;
var thisRecord="", validFileTypes="js,xml,txt,php,xsl,css,htm,html" , akkum = "";
var fileArray = [];
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var objFiles = FSO.GetFolder("F:\\xps1710\\jscript\\");
var objFileControl = new Enumerator(objFiles.files);
for (; !objFileControl.atEnd(); objFileControl.moveNext()) {
objFile = FSO.GetFile(objFileControl.item());
var ext = objFile.Name.split(".");
if (validFileTypes.indexOf(ext[1]) > 1) {
fileArray[ix] = "F:\\xps1710\\jscript\\" + objFile.Name;
ix++;
}
}
for (zx = 0 ; zx < ix ; zx++ ) {
var file2Traverse = FSO.OpenTextFile(fileArray[zx], forReading, triStateUseDefault );
while (!file2Traverse.AtEndOfStream) {
thisRecord = file2Traverse.ReadLine();
if (thisRecord.indexOf(what2lookfor) > 1 ) {
akkum = akkum + fileArray[zx] + '::' + thisRecord + '\n';
break;
}
}
}
WScript.Echo(akkum);

Resources