From 4c6758f764adf9962b4508dd0091bf67134b6918 Mon Sep 17 00:00:00 2001 From: Anshuman Jadiya Date: Thu, 9 Oct 2025 17:59:09 +0530 Subject: [PATCH 1/3] Implement PingCommand to execute ping on hostname This class executes a ping command to a specified hostname and prints the output. --- .../java/com/mycmd/commands/PingCommand.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/com/mycmd/commands/PingCommand.java diff --git a/src/main/java/com/mycmd/commands/PingCommand.java b/src/main/java/com/mycmd/commands/PingCommand.java new file mode 100644 index 0000000..6b87d85 --- /dev/null +++ b/src/main/java/com/mycmd/commands/PingCommand.java @@ -0,0 +1,36 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public class PingCommand { + public static void main(String[] args) { + if (args.length < 1) { + System.out.println("Usage: java PingCommand "); + return; + } + + String host = args[0]; + + try { + // Run the ping command + Process process = Runtime.getRuntime().exec("cmd.exe /c ping " + host); + + // Read the output of the ping command + BufferedReader reader = new BufferedReader( + new InputStreamReader(process.getInputStream()) + ); + + String line; + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + + // Wait for the process to finish + int exitCode = process.waitFor(); + System.out.println("Ping command exited with code: " + exitCode); + + } catch (Exception e) { + System.out.println("Error executing ping command."); + e.printStackTrace(); + } + } +} From b0757ded6614e27799c5703cdbbb0a1bedc12645 Mon Sep 17 00:00:00 2001 From: Anshuman Jadiya Date: Sat, 11 Oct 2025 14:58:06 +0530 Subject: [PATCH 2/3] Implement LsCommand to list directory contents --- .../java/com/mycmd/commands/LsCommand.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/com/mycmd/commands/LsCommand.java diff --git a/src/main/java/com/mycmd/commands/LsCommand.java b/src/main/java/com/mycmd/commands/LsCommand.java new file mode 100644 index 0000000..e07a290 --- /dev/null +++ b/src/main/java/com/mycmd/commands/LsCommand.java @@ -0,0 +1,34 @@ +import java.io.File; + +public class LsCommand { + + public static void main(String[] args) { + // Determine the directory to list + String directoryPath = "."; // current directory by default + if (args.length > 0) { + directoryPath = args[0]; // if user provides a path + } + + File directory = new File(directoryPath); + + if (!directory.exists()) { + System.out.println("Directory does not exist: " + directoryPath); + return; + } + + if (!directory.isDirectory()) { + System.out.println(directoryPath + " is not a directory."); + return; + } + + // List files and directories + String[] contents = directory.list(); + if (contents != null && contents.length > 0) { + for (String item : contents) { + System.out.println(item); + } + } else { + System.out.println("Directory is empty."); + } + } +} From 8bca22d76cf1ac711800a8c2bd1ae5c57b604af1 Mon Sep 17 00:00:00 2001 From: Anshuman Jadiya Date: Sat, 11 Oct 2025 14:59:23 +0530 Subject: [PATCH 3/3] Implement MoveCommand to move files --- .../java/com/mycmd/commands/MoveCommand.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/com/mycmd/commands/MoveCommand.java diff --git a/src/main/java/com/mycmd/commands/MoveCommand.java b/src/main/java/com/mycmd/commands/MoveCommand.java new file mode 100644 index 0000000..3498abd --- /dev/null +++ b/src/main/java/com/mycmd/commands/MoveCommand.java @@ -0,0 +1,32 @@ +package com.mycmd.commands; + +import com.mycmd.Command; +import com.mycmd.ShellContext; +import java.io.File; +import java.io.IOException; +import java.nio.file.*; + +public class MoveCommand implements Command { + @Override + public void execute(String[] args, ShellContext context) throws IOException { + if (args.length < 2) { + System.out.println("Usage: move "); + return; + } + + Path source = context.resolvePath(args[0]).toPath(); + Path destination = context.resolvePath(args[1]).toPath(); + + if (!Files.exists(source)) { + System.out.println("The system cannot find the file specified."); + return; + } + + try { + Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING); + System.out.println("Moved: " + source.getFileName() + " -> " + destination); + } catch (IOException e) { + System.out.println("Error moving file: " + e.getMessage()); + } + } +}