Create a JScript script (w3_firstname_lastname.js) that takes two parameters to
ID: 3710537 • 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
Explanation / Answer
<html>
<head>
<title>File list and size checker</title>
<script type="text/javascript">
function ShowFileList(rootFolder,file_size)
{
var fso = new ActiveXObject("Scripting.FileSystemObject"); //define file system object
var file,filesize,date_created;
writefile = fso.CreateTextFile("filelist.txt", true); //define object for writing file list in file
var line = "";
var fileList="";
var folder_content = fso.GetFolder(rootFolder); //get the content of folder
var fileArray = new Enumerator(folder_content.files); //get all files in given folder
for (; !fileArray.atEnd(); fileArray.moveNext()) //for each filel in given folder
{
file = fso.GetFile(fileArray.item()); //get the file object of the current file
filesize = file.Size; //get the size of current file
if(filesize < file_size) //if current file is smaller than reference file size then skip it
continue;
date_created = file.DateCreated; //get the creation date of the current file
line = fileArray.item() + " " + date_created + " " + filesize; //add the file to the output
writefile.WriteLine(line); //write the line to the file
fileList += line + "<br>"; //add the line to the HTML output
}
writefile.Close(); //close the file
document.getElementById('files').innerHTML = fileList; //write the file list to the page.
}
</script>
</head>
<body>
List of files...
<div id="files"></div>
<input type='button' value='List Files'/>
</body>
</html>