Create a JScript script (w3_firstname_lastname.js) that takes two parameters to
ID: 3712859 • Letter: C
Question
Create a JScript script (w3_firstname_lastname.js) that takes two parameters to do the following:
1) List all files names, size, date created in the given folder
2) Parameter1 = Root Folder name
Parameter2= File size
The script should check and validate the folder name, and the size
The script should loop until find all files greater than the specified size
3) Optionally, you can save the list into a file “Results.txt” using the redirection operator or by creating the file in the script.
4) Make sure that you include comment block in all your codes
This needs to be saved as js. Also I am new at this and need to know where to out in the folder I want to search
Explanation / Answer
The file will be run in web browser.
It will be run in Internet Explorer (as Activex object run only on Internet Explorer).
The localhost should be added to trusted hosts list in "Internet Explorer Optioins", as system usually prevents reading and writing to the file system.
---------------------------
There will be two buttons in the httml page. If you press "List Files" -- Files and sizes, DateCreated will be displayed.
If you press "Save the List to File" button, the list will be saved to file.
Missing: File size validation is missed in the code.
===========================================================================================
Program:
-----------------------------------------
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Show Local Folder File List</title>
<script type="text/javascript">
var fso = new ActiveXObject("Scripting.FileSystemObject");
// Function to display the file size as human readable format.
function humanFileSize(size) {
var i = Math.floor( Math.log(size) / Math.log(1024) );
return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
};
// Function for path validation
function isPathValid(path) {
if((path.charAt(0) != "\" && path.charAt(1) != "\") && (path.charAt(0) != "/" && path.charAt(1) != "/"))
{
// First Char Should be drive letter
if(!path.charAt(0).match(/^[a-zA-z]/))
{
alert("Enter Valid location");
return false;
}
//Second char should be :
if(path.charAt(1) == "" ||!path.charAt(1).match(/^[:]/) || !path.charAt(2).match(/^[/\]/))
{
alert("Enter Valid location");
return false;
}
// else {
// alert("Entered location " + path + " is correct");
// }
}
return true;
}
// Validate the given file size.
function isSizeValid(filesize) {
// pattern for filesize.
// It should start with numbers. It can have B,kb,mb,gb,tb (case insensitive)
var pattern =/^d+$/g;
// It return true if matches else false.
var testsize = pattern.test(filesize);
return testsize;
}
// Returns the files list obejct
function ShowFolderFileList(foldername, minsize){
if(!isPathValid(foldername)) {
alert("Not a Valid Folder Path");
return "";
}
var fileslist = "";
//fso - file system object.
var files = fso.GetFolder(foldername);
// recurse subfolders
var subfolders = new Enumerator(files.SubFolders);
for(; !subfolders.atEnd(); subfolders.moveNext())
{
fileslist+=ShowFolderFileList((subfolders.item()).path);
}
// display all file path names.
var fc = new Enumerator(files.files);
//alert(fc.item().Size);
for (; !fc.atEnd(); fc.moveNext())
{
// If the file size is greater than min size specified then only print
if(fc.item().Size >= minsize) {
fileslist += fc.item() + " " + humanFileSize(fc.item().Size) + " " + new Date(fc.item().DateCreated).toDateString() + " ";
}
}
return fileslist;
}
//Show all the files in web Browser.
function listFiles(){
var filesList = ShowFolderFileList('C:\xampp\htdocs\chegg', 1024);
// Replace the tag with <br> to show line by line.
document.getElementById('files').innerHTML = filesList.replace(/ /gi, "<br/>");
}
// Save files to file
function saveListFiles() {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FileObject = fso.OpenTextFile('C:\xampp\htdocs\chegg\Results.txt', 2, true,0); // 2=overwrite, true=create if not exist, 0 = ASCII
FileObject.write(ShowFolderFileList('C:\xampp\htdocs\chegg', 1024)) // write the contents
FileObject.close() // close file
}
</script>
</head>
<body>
<input type='button' value='List Files'/>
<br />
<br />
<input type='button' value='Save the List to File'/>
<div id="files"/>
</html>
=========================================================
Output: (Same output Goes to file)
C:
mpphtdocscheggFilesAndFolder.html 4.25 kB Sat Apr 21 2018
C:
mpphtdocscheggheader-image.jpg 6.19 kB Sat Apr 14 2018
C:
mpphtdocscheggindex.html 5.57 kB Sat Apr 14 2018
C:
mpphtdocscheggmedium.jpg 4.13 kB Sat Apr 14 2018