Ejemplo n.º 1
0
        protected override void Action(string[] args)
        {
            if (args[0] == "")
            {
                Log.PrintLineError("No file specified!");
                return;
            }

            var pathToFile = WorkingDirectory.GetCombinedWith(args[0]);

            if (!File.Exists(pathToFile))
            {
                Log.PrintLineError("File is not exists!");
                return;
            }

            try
            {
                File.Delete(pathToFile);
            }
            catch (Exception e)
            {
                Log.PrintLineError("Can`t remove the file!");
                throw;
            }
        }
Ejemplo n.º 2
0
        protected override void Action(string[] args)
        {
            if (args[0] == "")
            {
                Log.PrintLineError("No folder specified!");
                return;
            }

            var pathToDir = WorkingDirectory.GetCombinedWith(args[0]);

            if (!Directory.Exists(pathToDir))
            {
                Log.PrintLineError("Folder is not exists!");
                return;
            }

            try
            {
                Directory.Delete(pathToDir, true);
            }
            catch (Exception e)
            {
                Log.PrintLineError("Can`t remove the folder!");
                throw;
            }
        }
Ejemplo n.º 3
0
        protected override void Action(string[] args)
        {
            // We can ls current dir or dir which user specified
            var lookUpDir = WorkingDirectory.Directory;

            if (args[0] != null)
            {
                lookUpDir = new DirectoryInfo(WorkingDirectory.GetCombinedWith(args[0]));
            }

            // dir which is looks up should be exist
            if (!lookUpDir.Exists)
            {
                Log.PrintLineError("Directory is not exists!");
                return;
            }

            var dirs  = lookUpDir.GetDirectories();
            var files = lookUpDir.GetFiles();

            // Print all dirs in look up dir
            int i = 1;

            foreach (DirectoryInfo d in dirs)
            {
                Log.PrintGray($"{i}) ");
                Log.PrintLineContrast(d.Name);
                i++;
            }

            // Print all files
            foreach (FileInfo f in files)
            {
                Log.PrintGray($"{i}) ");
                Log.PrintLine(f.Name);
                i++;
            }

            // If dir to look up is empty => print hint
            if (dirs.Length + files.Length == 0)
            {
                Log.PrintLineGray("The directory is empty.");
            }
        }
Ejemplo n.º 4
0
        protected override void Action(string[] args)
        {
            // Minimum number of args 2
            if (args[0] == "" || args[1] == "")
            {
                Log.PrintLineError("You should specify source and destination!");
                return;
            }

            // Get full paths
            var sourceFilePath        = WorkingDirectory.GetCombinedWith(args[0]);
            var destinationFolderPath = WorkingDirectory.GetCombinedWith(args[1]);
            var destinationFilePath   = Path.Combine(destinationFolderPath, args[0]);

            // Check source file and output dir existence and output file non-existence
            if (!File.Exists(sourceFilePath))
            {
                Log.PrintLineError("File to move is not exists!");
                return;
            }
            if (!Directory.Exists((destinationFolderPath)))
            {
                Log.PrintLineError("Destination folder is not exists!");
                return;
            }
            if (File.Exists(destinationFilePath))
            {
                Log.PrintLineError("There is already same file in directory you trying to move!");
                return;
            }

            try
            {
                File.Move(sourceFilePath, destinationFilePath);
            }
            catch (Exception e)
            {
                Log.PrintLineError("Can`t move a file!");
                throw;
            }
        }
Ejemplo n.º 5
0
        protected override void Action(string[] args)
        {
            // Minimum number of args is 2
            if (args[0] == "" || args[1] == "")
            {
                Log.PrintLineError("You should specify to files!");
                return;
            }

            // Get full paths of "from" and "to" files
            var sourceFilePath      = WorkingDirectory.GetCombinedWith(args[0]);
            var destinationFilePath = WorkingDirectory.GetCombinedWith(args[1]);

            // Source should be and output shouldn`t exist
            if (!File.Exists(sourceFilePath))
            {
                Log.PrintLineError("File you specified is not exists!");
                return;
            }

            if (File.Exists((destinationFilePath)))
            {
                Log.PrintLineError("Destination file shouldn`t be exists!");
                return;
            }

            // Actual copying with error handling
            try
            {
                File.Copy(sourceFilePath, destinationFilePath);
            }
            catch (Exception e)
            {
                Log.PrintLineError("Can`t copy a file!");
                throw;
            }
        }
Ejemplo n.º 6
0
        protected override void Action(string[] args)
        {
            // Minimum number of args is 3
            if (args[0] == "" || args[1] == "" || args[2] == "")
            {
                Log.PrintLineError("To few arguments. You should specify 3 or move arguments");
                return;
            }

            // Get full absolute paths of files user specified
            List <string> filePaths = new List <string>();
            int           i         = 0;

            while (i < 10 && args[i] != "")
            {
                var filePath = WorkingDirectory.GetCombinedWith(args[i]);
                filePaths.Add(filePath);
                i++;
                Log.PrintLine(i.ToString());
            }

            // Read all data from these files
            var lines = new List <string>();

            for (int j = 0; j < filePaths.Count - 1; j++)
            {
                if (!File.Exists(filePaths[j]))
                {
                    Log.PrintLineError("Some of specified files is not exists!");
                    return;
                }

                try
                {
                    using (StreamReader sr = new StreamReader(filePaths[j]))
                    {
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            lines.Add(line);
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.PrintLineError("Can`t read file!");
                    throw;
                }
            }

            // Write all strings into output (last argument) file
            if (File.Exists(filePaths.Last()))
            {
                Log.PrintLineError("File you want to combine is already exists!");
                return;
            }
            using (StreamWriter sr = new StreamWriter(filePaths.Last()))
            {
                foreach (string l in lines)
                {
                    sr.WriteLine(l);
                }
            }
        }