Windows command-line disk cataloging - save to csv? - windows

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);

Related

How can I delete a Windows Junction using FileSystemObject?

I'm mounting a VHD to a folder (junction) using diskpart.
After unmounting the VHD, I need to delete the folder using FileSystemObject.
var vhdPath = "D:\SomeVhd.vhd";
var fsObj = new ActiveXObject("Scripting.FileSystemObject");
var TypeLib = WScript.CreateObject("Scriptlet.TypeLib");
var vhdmountpoint = fsObj.GetDriveName(vhdPath) + "\\" + TypeLib.Guid;
//Mount with diskpart here, vhdmountpoint is now a junction
//Dismount with diskpart here, vhdmountpoint still a junction
if (fsObj.FolderExists(vhdmountpoint)) { //returns true!
fsObj.DeleteFolder(vhdmountpoint); //Returns path not found
}
Am I missing something?
P.S.
I got around this issue by doing:
var shell = WScript.CreateObject("WScript.Shell");
shell.Run("cmd /c rmdir " + vhdmountpoint);
I suppose that counts as a hack.

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

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);
}

Using JScript for uninstall script

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?

Strange behavioural difference between vbscript and jscript using ADODB.stream in an HTA application

VBscript example:
Function ADO_WriteToFile(FileURL,data)
Dim arrBytes
SET ADObj = CreateObject("ADODB.Stream")
ADObj.Open
ADObj.Charset = "iso-8859-1"
ADObj.Type = adTypeText
ADObj.WriteText data
ADObj.SaveToFile FileURL, adSaveCreateOverwrite
ADObj.Close
ADObj.Open
ADObj.Type = adTypeBinary
ADObj.LoadFromFile FileURL
ADObj.Position = 3
arrBytes = ADObj.Read
ADObj.Position = 0
ADObj.SetEOS
ADObj.Write data
ADObj.SaveToFile FileURL, adSaveCreateOverwrite
ADObj.Close
End Function
JScript example:
function writeTo(fileName,str) {
var ado = new ActiveXObject("ADODB.Stream");
ado.Type = 2;
ado.Open();
ado.Position = 0;
ado.WriteText(str,0);
ado.SaveToFile(fileName,2);
ado.Close();
ado.Open();
ado.Type = 1;
ado.Position = 2;//line 19
var temp = ado.Read();
ado.Position = 0;
ado.SetEOS;
ado.Write(temp);
ado.SaveToFile(fileName,2);
ado.Close();
}
Why does the VBScript example work perfectly except for the fact that it can't accept file paths with space in them?
The JScript example errors out with the message "assignment to the parameter is incorrect." line 19. This doesn't happen if I set Position to 0 however:
ado.Position = 0;
i am using this to write binary files to disk btw
Here are some differences:
In the VBScript version, position is set to 3; in the JScript version, it is set to 2
In the VBScript version, the character set is defined; in the JScript version, it is undefined
In the VBScript version, WriteText and write both reference the argument; in the JScript version, only WriteText references it
References
Use vs Mention in JScript doesn't come for Free
JScript Data Types: Data Type Summary

Resources