I'm fairly new to JavaScript.
Given a local machine's folder path (Windows), I was wondering how you can extract the names of all the possible folders in the current path, without the knowledge of how many folders there are or what they are called.
Thank you very much in advance.
Here is a little script to get you started with FileSystemObject in conjuction with JScript:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var shell = new ActiveXObject("WScript.Shell");
var path = "%ProgramFiles%";
var programFiles = fso.GetFolder(shell.ExpandEnvironmentStrings(path));
var subFolders = new Enumerator(programFiles.SubFolders);
while (!subFolders.atEnd())
{
var subFolder = subFolders.item();
WScript.Echo(subFolder.Name);
subFolders.moveNext();
}
Call that with csript.exe on the command line:
cscript subfolders.js
The Windows Script 5.6 Documentation holds all the details you need on this topic (and many others). Download it and have it around, it is really helpful. On Windows systems, a little knowledge of FileSystemObject and it's relatives really can save the day.
You cannot do this via Javascript in a browser as the JS doesn't have that kind of access to the file system from a browser.
Assuming the script will execute in a context where it makes sense to try and access the local hard drives (e.g. in cscript or classic ASP), your best bet is the FileSystemObject.
If you're executing JavaScript in a web browser then you can't, because in this scenario JavaScript has no access to the local file system for security reasons.
Related
I have a classic asp page in VBS and I am trying to create a file on the web server with the following code.
Set fso = CreateObject("Scripting.FileSystemObject")
Set file1 = fso.CreateTextFile("\\localhost\inetpub\wwwroot\cs\batch\123456dirs.bat", true)
This returns the following error:
|666|800a0034|Bad_file_name_or_number
Line 666 is the CreateTextFile line.
According to the Microsoft docs, this means that I'm trying to create a file with an invalid filename. Then it explains the rules for filenames and mine appears to be perfectly valid.
Any suggestions or ideas on how I can further troubleshoot this?
first thing to check to make sure your users have access to the folder. Assuming you're not using windows authentication, make sure IUSR account has write access to the folder.
second, unless inetpub is set up as a share to folder, you're syntax won't work. if the root of your website is located in the CS folder, you can do something like:
Set file1 = fso.CreateTextFile(Server.MapPath( "/cs/batch/123456dirs.bat" ), true)
The createtextfile() function runs on the web server but in the context of the local server itself. Simply put, any path you give it must resolve as if you were logged on to a windows desktop on the server and tried to CD to that path.
The format \localhost... is a UNC path. See this question for a discussion about UNC paths and windows. Unless you know for sure that there is a UNC path mapped for \localhost then that is probably your issue. You may be making the assumption the \localhost will be a reasonable path to use, but as I said unless you know for sure it is available then this is an invalid choice.
Lastly, if you decide to set up a share for \localhost, you will be getting in to some interesting territory around the user context that the web server operates in. You see you will have to set up the share for the IIS user that is configured as the run-as identity for IIS, so you will need to know that and create the required config to give that user the share.
If it were me, I would switch to using a standard windows path, although even then you need to appreciate the run-as user context and security config, etc.
I need to automate a script that is mostly VBScript, but has some JScript in it to test JSON objects as well as a JSON parser that is also written in Jscript. Also, we have a bunch of INCLUDE files (I could do an ExecuteGlobal on them, but there are a bunch of hurdles there too). Normally I would just take all my Classic ASP code, make adjustments for VBS files, save, and set up a task on the server to execute. The issue is the mixed JScript.
Is there a way to execute a classic ASP file automatically that I am not aware of? I could redirect the first visitor of the day or something to a page and then once the page is done, redirect them to the requested page, but that's really a hack. But, maybe a hack is the only way to do this.
Any help would be appreciated, thank you!
Dennis
I figured out the best way to do this is with a XML HTTP get request on the page from a VBS file that I set up as a scheduled task.
Set objXML = CreateObject("MSXML2.ServerXMLHTTP.6.0")
objXML.open "GET", "Full-URL-HERE", false
objXML.send()
sResponse = objXML.responseText
Set objXML = Nothing
Then Full-URL-HERE is the URL to the page that you wanted to automate.
Is multithreading possible using vbscript. i have to form fill a web page in multiple browsers/tabs, so that, i hope can reduce the completion time. Please suggest.
Vbscript does not natively have any methods for launching separate threads although if you try then it's more of "emulation" of multi-threading than anything else
You can check this documentation
To open a url in vbscript you can try something like this:-
set wShell = CreateObject("Shell.Application")
wShell.Open "C:\temp\abc.txt"
I wrote vbscript inside my html file for my site and I can't get it to work. I know it only works in internet explorer as thats the common answer I see people write with this issue. I am able to get basic vbscript working, but when trying to use filesystemobjects to open a text file nothing happens. Code being used is below.
<Script type="text/vbscript">
Dim fsobj, objtxt, thearr
Set fsobj = CreateObject("Scripting.FileSystemObject")
Set objtxt = fsobj.OpenTextFile("./subfolder/foo.txt", 1)
thearr = split(objtxt.readline, ",")
document.write(thearr(0) & " and " & thearr(1))
</script>
I get the code to work when saving with asp extension but not when I save as html, is there a way to get it to work with only using the html extension? If not does someone have an explanation as to why scripting filesystemobject without the asp extension doesn't work? I seem to can't search for the answer I'm looking for.
When you were using the FileSystemObject from an ASP page, then you were manipulating the file-system of the server. This is permitted.
However, when you use the code above, you are executing the code on the client. It is not permitted to access the clients file-system from inside Internet Explorer, as it would have serious security implications. The technical term is "sandboxing".
If you need to interact with the file-system on the client machine, you will need to use a technology such as ActiveX.
When you run the script as client script, it would try to access the file from the client computer, not the server. The file isn't there, and even if it was, your script would not be allowed to access it.
You should consider using HTML Applications by renaming your file with a .hta suffix.
An HTA executes without the constraints of the internet browser security model; in fact, it executes as a "fully trusted" application.
HTML files running inside internet browsers are considered as "untrusted" because the code comes from the internet, and is generally considered as "untrusted" as such the browser enforces a tight security model that prevents those HTML pages gaining access to your computer, which is why the FileSystemObject is unable to open the text file. However, as a HTA it is no longer being run by your browser, but via Microsoft's MSHTA application which grants your script full trust.
For more information see HTML Application - Wikipedia.
In looking at the MVCMailer step by step, I see that you can embed images inline using cid in the HTML.
https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide
I see the code that talks about embedding:
#Html.InlineImage("logo", "Company Logo")
var resources = new Dictionary<string, string>();
resources["logo"] = logoPath;
PopulateBody(mailMessage, "WelcomeMessage", resources);
My question is rather than use a path within the site like:
resources["logo"] = Server.MapPath("~/Content/images/logo.png");
Can I somehow get at images I have in the Azure Cloud or S3 Cloud where I have plenty of space?
Please let me know if there is a way to get the image from some other source than the server the MVC mailer is running on.
Thanks,
Victor
In Server.MapPath(my_path), my_path, specifies the relative or virtual path to map to a physical directory. Server.MapPath is related with local FileSystemObject so in that case it will look for the file (you pass as parameter) in a location within local web server. More info is described here:
http://msdn.microsoft.com/en-us/library/ms524632(v=VS.90).aspx
So if you image is located at http://azure_storage_name.blob.core.windows.net/your_public_container/image_name.jpg, the passing it to Server.MapPath() will not work as this is not a location in local file system. I tried and found that it will not accept and I also try to tweak a few way but no success. IF you can change the code behind and which can access the resources from a URL then it will work otherwise not.