Question

mplement as LastnameDir (30 points). Displays the names ofthe non-hidden files and directories in the current directory, or another directory, along with creation time, and size of file. In case of subdirectories, it displays the word cDIR> instead of file size. At the end, the command should display the total number of files in the directory, the total file size, and the number of sub-directories. COMP 3063, operating Systems, Spring 2017, Assignment 1, Due Feb 21.2017. (c) ALodgher Page 1 of 2 LodgherDir (displays info of all files and directories in current directory) LodgherDir c ILFromDir (displays info of all files and directories in clLFrombir)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Program

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
public class StackBasedIteration
{
static void Main(string[] args)
{
//Provide your directory name and path here. My directory was "C:/x/"
TraverseTree("C:/LastnameDir/");// You can provide "LastnameDir"

Console.WriteLine("Press any key");
Console.ReadKey();
}

public static void TraverseTree(string root)
{
// Data structure to hold names of subfolders to be
// examined for files.
Stack<string> dirs = new Stack<string>(20);
int count=0;
int dirno = 0;
long sum = 0;

if (!System.IO.Directory.Exists(root))
{
throw new ArgumentException();
}
dirs.Push(root);
while (dirs.Count > 0)
{
string currentDir = dirs.Pop();
string[] subDirs;
try
{
subDirs = System.IO.Directory.GetDirectories(currentDir);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
continue;
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}

string[] files = null;
try
{
files = System.IO.Directory.GetFiles(currentDir);
}

catch (UnauthorizedAccessException e)
{

Console.WriteLine(e.Message);
continue;
}

catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}
// Perform the required action on each file here.
// Modify this block to perform your required task.
Console.WriteLine("_______________________________________________________________________________\n");
Console.WriteLine("<DIR> : {0}\n", currentDir);
Console.WriteLine("\nFile Creation Time\t:\tFile Length\t:\tFile Name\n");
Console.WriteLine("_______________________________________________________________________________\n\n");
foreach (string file in files)
{
try
{
// Perform whatever action is required in your scenario.
System.IO.FileInfo fi = new System.IO.FileInfo(file);
Console.WriteLine("{2}\t:\t{1}\t\t:\t{0}", fi.Name, fi.Length, fi.CreationTime);
sum = sum + fi.Length;
count++;
}
catch (System.IO.FileNotFoundException e)
{
// If file was deleted by a separate application
// or thread since the call to TraverseTree()
// then just continue.
Console.WriteLine(e.Message);
continue;
}
}
// Push the subdirectories onto the stack for traversal.
// This could also be done before handing the files.
foreach (string str in subDirs)
{
dirs.Push(str);
dirno++;
}
  
}
Console.WriteLine("\n...............................................................................\n");
Console.WriteLine("Total number of files : {0}\nTotal file size : {1}\nTotal number of Sub-Directories : {2}\n", count, sum, dirno);
Console.WriteLine("_______________________________________________________________________________\n\n");
  
}
}
}

OUTPUT:

ile CreationTime File Length Pile Hame 2-01-2017 12:00:20 1-01-2017 20:19:19 2-01-2017 11:47:00 2-01-2017 11:45:47 0-10-2016

1-01-2017 14:14:04 2-02-2017 12:36:05 2-02-2017 12:35:50 6-02-2017 21:35:44 6-02-2017 21:03:53 6-02-2017 21:03:43 7-10-2016 1

1-02-2017 08:4256 1-02-2017 08:42:56 4535 5212 Calculator-java CalculatorWitMethods.jav 1-02-2017 08:4256 1-02-2017 08:4256 1

Add a comment
Know the answer?
Add Answer to:
Displays the names of the non-hidden files and directories in the current directory, or another directory,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a) How would you use the ls command to display all of the files/directories in the...

    a) How would you use the ls command to display all of the files/directories in the directory ‘/sbin’ that start with ‘bl’? b) How would you use the ls command to display all of the files/directories in your current directory that contain the word ‘grade’ somewhere in the name and end with ‘.pdf’? c) How would you use the ls command to display all of the files/directories in your current directory that are any two characters followed by the file...

  • Write a Java program that lists all the files in a directory and their subdirectories, that...

    Write a Java program that lists all the files in a directory and their subdirectories, that mimics the Unix ls command or the Windows dir command. Note that when a directory is encountered we do not immediately print its contents recursively. Rather, as we scan each directory, place any subdirectory in a List. After the directory entries are printed then process each subdirectory recursively. For each file that is listed, include the modification time, the file size, and if it...

  • Select your answer from here. absolute pathname to the file called xyz changes the directory to...

    Select your answer from here. absolute pathname to the file called xyz changes the directory to the parent of the current directory. lists current directory files including the invisible files sends xyz file to the line printer deletes the directory called xyz displays the content of the file called xyz displays the current directory pathname cancels the printing job on the 1p 1 printer confirms the deletion of the xyz file before deleting it lists the current directory in long...

  • What program do you use to list the files in a directory? Which option do you...

    What program do you use to list the files in a directory? Which option do you use to display: All files, including dotfiles (hidden files) Names in reverse alphabetical order Entire tree, including all subdirectories Flags after each name (/ = directory) Size of each file, in human readable units Long listings (permissions, owner, and so on) Information about a directory itself, not its contents ​When you look at a directory listing, what do the entries . and .. mean?

  • do numbers 4-8 4. Given any directory, use the Is command to display: • all files...

    do numbers 4-8 4. Given any directory, use the Is command to display: • all files and sub-directories starting with the letter "D" (note do not list anything in any sub-directory) • its immediate sub-directories (sub-directories only, and no other ordinary files) its immediate hidden sub-directories only - take a screenshot (#3-3) that clearly shows the command and the result. 5. Assume that the following files are in the working directory: $ ls intro notesb ref2 section 1 section3 section4b...

  • Problem 4: Write a Bash script that removes all zero length ordinary files in the directory...

    Problem 4: Write a Bash script that removes all zero length ordinary files in the directory (including those in the sub-directories at all levels) passed as an optional argument. If you do not specify the directory argument, the script uses the current working directory as the default argument. This problem is for practicing bash programming skills. Though there is an easier way to achieve the goal with the find command, the find command is not allowed to appear in your...

  • What is a single command that concatenate all files in the current directory that end in...

    What is a single command that concatenate all files in the current directory that end in .c into a single file all.c in the backup directory? Assume the backup directory is a child of the current directory.

  • A filename con comprise multiple embedded dots (e.g., a.b.c.d.e). True False A device file is not...

    A filename con comprise multiple embedded dots (e.g., a.b.c.d.e). True False A device file is not really a stream of characters and doesn't contain anything at all. True False Relative pathnames begin with the root directory. True False Running the ls -a command uniquely identifies directories and binary executables. True False What is the result of running mkdir -p share/man/cat1 from the command line? a. It creates the directory share. b. It creates the directory share/man. c. It creates the...

  • Open Command Prompt typing CMD command in your Search window. Change the directory from current to...

    Open Command Prompt typing CMD command in your Search window. Change the directory from current to root (C:). Make a new directory called CIS165_Your Last Name (example: CIS165_Nossa). Within this directory, make subdirectories called Can1 and Can2. Verify the created directory and subdirectories using particular command. Under Can1 subdirectory creates three files called Jam.txt, Milk.txt and Juice.txt. Verify that you created these files using particular command. Move Jam.txt file from the Can1 subdirectory to the Can2 folder. Compare using special...

  • 20. Write the DOS command to erase a file named FILEBILL which is stored in the...

    20. Write the DOS command to erase a file named FILEBILL which is stored in the sub-directory named MYFILES located at the root of drive C. 21. Write the DOS command to return you to the root of drive C if your current directory is C:\TEACHING OPSYS\SUMM 22. Write the DOS command to erase all the files on drive A. 23. Write the DOS command to delete a subdirectory named DIRFRED which is located at the root of drive C....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT