So I found this old web application (asp classic) in my workplace and been asked to modified it. What i'm trying to do is, I want to display all files from this particular folder. Instead of hardcoding each of the file name with its link, I tried as below:
<%
var fs = new ActiveXObject("Scripting.FileSystemObject");
var fo = new ActiveXObject("Scripting.FileSystemObject");
var f = new ActiveXObject("Scripting.FileSystemObject");
var theFile = new ActiveXObject("Scripting.FileSystemObject");
fo=fs.GetFolder("C:\\inetpub\\wwwroot\\edocument\\MyFiles");
f = fo.Files;
For Each theFile in f
Response.write(theFile.Name+"<br>");
Next
%>
seems like For Each loop won't work/not recognized in asp javascript but found some working example using vbscript. I also tried to access the collection directly:
Response.write(f[0].Name);
but it says ...is null or not an object
Any idea how can I access this Files Collection?
In javascript you need an Enumerator() to walk the collection, and either use for next, or while like in the example below
<% #LANGUAGE="JScript" %>
<%
var fso = Server.CreateObject("Scripting.FileSystemObject");
var fo = fso.GetFolder("C:\\inetpub\\wwwroot\\edocument\\MyFiles");
var f = new Enumerator(fo.Files);
f.moveFirst()
while (!f.atEnd()) {
Response.Write(f.item().name + "<BR>");
f.moveNext();
}
fo = null;
f = null;
fso = null;
%>
Related
I'm new in code with Javascript and I have a problem with an array I need to create.
var tabLine = new Array();
var tabCol = new Array();
var tabComplet = new Array();
var fso, f1, ts, s, cl, ln;
var ForReading = 1;
var i = 0;
function ReadFiles() {
fso = new ActiveXObject("Scripting.FileSystemObject");
// Read the contents of the file.
ts = fso.OpenTextFile("PathToAFile\TextFile.txt", ForReading);
s = ts.ReadAll();
tabLine = s.split('\n');
cl = tabLine.length;
ts.Close();
for (i = 0; i <tabLine.length; i++) {
var tabCol = tabLine[i].split("\t");
for (j=0;j<tabCol.length;j++) {
tabComplet[i,j] = tabCol[j];
alert(tabComplet[i,j]);
}
}
alert(tabComplet[10,5]);
alert(tabComplet[3,5]);
}
ReadFiles();
The file I need to read is a text file; It have many line and each line have information separate by tabulation.
This function read the text file and convert it into an array in two dimensions.
I had some alert to check the contain of my array.
The first alert give me the result I want (it display the content of the array witch is different for each element in the array)but the two other give me the same result. I check with another alert and, in the for boucle, these two element are different.
I make more test and all happen like if my array only have the same line copy/paste.
Thanks in advance for all information that can help me.
Here is an example of file I use :
http://www.k-upload.fr/afficher-fichier-2017-05-18-1b0cfa685testimport2.txt.html
Usually there are bigger than this one but for test it's OK.
I have a webpage where I am fetching the name of files in a Folder into an array using VBScript, then I am passing that array to JavaScript variable, so that I can display the names on the screen.
VBScript Code:
Function allFiles()
Dim arr, arr2, oTargetFolder
arr = array()
set oFSO = CreateObject("Scripting.FileSystemObject")
oTargetFolder = "C:\Users\msiddiq1\Documents\WSDLs"
set objFolder = oFSO.GetFolder(oTargetFolder)
set oFiles = objFolder.Files
For Each files in oFiles
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = files.Name
Next
allFiles = arr
End Function
JS:
var folderFiles = allFiles();
alert(folderFiles.length); // alerts `undefined`
I can pass hardcoded values from vbscript to javascript, but not this array.
Please suggest.
You have to wrap the resulting array in a VBArray object and call toArray:
var folderFiles = new VBArray(allFiles());
var ff = folderFiles.toArray();
alert(ff.length);
or in one line:
var folderFiles = (new VBArray(allFiles())).toArray();
Note that VBScript is deprecated in IE11 edge mode, so it will be disappearing at some point.
I wish to add wishlist functionality to a client site using a combination of Javascript and cookies. To achieve this, each item on the site will have a button that the user can click to add the item to their wishlist. The button code is as follows:
<a data-id="5054" data-node="3286" data-type="package" class="wishList">Add to Wishlist</a>
When the button is clicked the following JavaScript is executed:
$('.wishList').click(function () {
var nodeId = $(this).data("node");
var id = $(this).data("id");
var type = $(this).data("type");
var package = new Object();
package.id = id;
package.nodeId = nodeId;
var jsonObject = new Object();
jsonObject.package = package;
var jsonString = JSON.stringify(jsonObject);
console.log(jsonString);
var newObject = new Object();
newObject.id = 8888;
newObject.nodeId = 7777;
var obj = JSON.parse(jsonString);
obj['package'].push(newObject);
console.log(JSON.stringify(obj));
});
This code is derived from this Stack Overflow question and my own knowledge:
Adding a new array element to a JSON object
The aim of the code is to simulate both creating a new JSON notation using Javascript objects as well as parsing the notation back into a Javascript object, adding a new item to the array and then using stringify to convert it back into a string again. My problem is, the push method does not seem to work. When I run this code, I get the following error:
TypeError: obj.package.push is not a function
As this part of the code was taken from the other question, I am at a loss to understand why it is not working in this context as I do not see any comments suggesting that this does not work on the other post.
My aim is to build up the following JSON notation:
{"package":[{"id":"5054","nodeId":"3286"},{"id":"8888","nodeId":"7777"}]}
This will eventually lead to a more complex notation of:
{"package":[{"id":"5054","nodeId":"3286"},{"id":"8888","nodeId":"7777"}], "hotel":[{"id":"3421","nodeId":"1234"},{"id":"8748","nodeId":"2435"}],}
This notation will then be stored as a cookie that will be added to and manipulated using JavaScript and read on the server side with each request using .Net.
My question is, what am I doing wrong here? Why in this case is the .push funcionality not working?
I've created a Fiddle here: http://jsfiddle.net/jezzipin/kdVVK/
As your package property will be a simple list of objects, it needs to be an array.
$('.wishList').click(function () {
var nodeId = $(this).data("node");
var id = $(this).data("id");
var type = $(this).data("type");
var package = [];
package.push({
id: id,
nodeId: nodeId
});
var jsonObject = {};
jsonObject.package = package;
var jsonString = JSON.stringify(jsonObject);
console.log(jsonString);
var newObject = {
id: 8888,
nodeId: 7777
};
var obj = JSON.parse(jsonString);
obj['package'].push(newObject);
console.log(JSON.stringify(obj));
});
updated fiddle
Can anyone fill in the blanks here.
I have been trying to get a script I could run to query all available users in the Global Catalog for active directory and finally managed it in VBS -looking for any particular username as below:
Const ADS_SECURE_AUTHENTICATION = 1
Set oGC = GetObject("GC:")
For Each child In oGC
Set oEntrprise = child
Exit For
Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("AD.txt", True)
' Setup ADO.
Set oConn = CreateObject("ADODB.Connection")
Set oComm = CreateObject("ADODB.Command")
oConn.Provider = "ADsDSOObject"
oConn.Properties("ADSI Flag") = ADS_SECURE_AUTHENTICATION
oConn.Open
oComm.ActiveConnection = oConn
' Set the search command and filter.
objFile.WriteLine(oEntrprise.ADsPath)
oComm.CommandText = "<" & oEntrprise.ADsPath & ">;(&(objectCategory=person)(objectClass=user)(givenName=aaron*));cn,distinguishedName;subTree"
' Execute the query.
Set oRS = oComm.Execute
' Print the results.
oRS.MoveFirst
While Not oRS.EOF
For Each field In oRS.Fields
objFile.WriteLine(field)
Next
objFile.WriteLine("")
oRS.MoveNext
Wend
WScript.Echo "Finished"
Im now trying to convert it to JS but I cannot replicate it.
I cannot find the golden answer for looping through GetObject("GC:"). For each doesnt seem to work like for like in this case. Is anyone aware of how to do this?
So in effect i need the JS equivelant of oEntrprise in the above script.
var oConn = WScript.CreateObject("ADODB.Connection");
var oComm = WScript.CreateObject("ADODB.Command");
var keyname = "samaccountname";
var keyvalue = "aaron";
oConn.Provider = "ADsDSOObject";
oConn.Properties("ADSI Flag") = 1;
oConn.Open;
oComm.ActiveConnection = oConn;
var objRootDSE = GetObject("GC:");
for (var i = 0; i < objRootDSE.length; i++) {
WriteToFile("Moahhh");
var oEntrprise = objRootDSE[i];
oComm.CommandText = "<" + oEntrprise.ADsPath + ">;(&(objectCategory=person)(objectClass=user)(givenName=a*));cn,distinguishedName;subTree";
var oRS = oComm.Execute;
}
function WriteToFile(sText){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FileObject = fso.OpenTextFile("C:\\builds\\LogFile.txt", 8, true,0); // 8=append, true=create if not exist, 0 = ASCII
FileObject.write(sText)
FileObject.close()
}
In JScript you need to use an Enumerator to step over the elements of a collection
var objRootDSE = GetObject('GC:');
for (var childs = new Enumerator(objRootDSE) ; !childs.atEnd(); childs.moveNext()){
var child = childs.item();
WScript.Echo( child.Name );
};
Thanks for the suggested answer - answers the impossible loop I could not solve but I have now found a way to query the Global Catalogue for users directly without a need for the loop:
var aoi = WScript.CreateObject("ADSystemInfo");
var gcBase = aoi.ForestDNSName;
var ado = WScript.CreateObject("ADODB.Connection");
ado.Provider = "ADSDSOObject";
ado.Open;
WriteToFile(aoi.ForestDNSName);
var objectList = ado.Execute("<GC://" + gcBase + ">;(&(objectCategory=person)(objectClass=user)("+keyname+"="+keyvalue+"*));cn,distinguishedName;subTree");
if(!objectList.EOF)
{
WriteToFile(objectList("distinguishedName").value);
}
function WriteToFile(sText){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FileObject = fso.OpenTextFile("C:\\LogFile.txt", 8, true,0); // 8=append, true=create if not exist, 0 = ASCII
FileObject.write(sText)
FileObject.close()
}
Never again.
Whoever finds this I will help Google get here - Global Catalog JavaScript query for Active Directory!
I want to add names of files of a specific folder to an JS's array, but nothing happens:
var pics = new Array();
var x;
var fs = new ActiveXObject("Scripting.FileSystemObject");
alert('x');
var fo = fs.GetFolder(Server.MapPath("C:\wamp\www\newsite\ErfanGhiasiPanel\Slider Images"));
for (x in fo.files){
pics.push(x.Name);
}
For instance, whet I insert an
alert('something')
after var fs = new ActiveXObject... or next lines, it won't appear.
What you guys think?
Thank you
Assuming JScript + Classic ASP due to the MapPath (which you dont need in your case) you need to escape the path string;
var pics = [];
var fs = new ActiveXObject("Scripting.FileSystemObject");
var fo = new Enumerator(fs.GetFolder("C:\\wamp\\www\\newsite\\ErfanGhiasiPanel\\Slider Images").Files);
for (; !fo.atEnd(); fo.moveNext()) {
pics.push(fo.item(0).Name)
}