Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/mycmd/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private static void registerCommands(Map<String, Command> commands) {
commands.put("ipconfig", new IpConfig());
commands.put("alias", new AliasCommand());
commands.put("unalias", new UnaliasCommand());
commands.put("rename", new RenameCommand());
commands.put("tasklist", new TasklistCommand());
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/mycmd/commands/RenameCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.mycmd.commands;

import com.mycmd.Command;
import com.mycmd.ShellContext;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
* Implements the "rename" (or "ren") command for MyCMD.
* Usage: rename <oldName> <newName>
*/
public class RenameCommand implements Command {

@Override
public void execute(String[] args, ShellContext context) throws IOException {
if (args.length != 2) {
System.out.println("Usage: " + usage());
return;
}

File oldFile = context.resolvePath(args[0]);
File newFile = context.resolvePath(args[1]);

if (!oldFile.exists()) {
System.out.println("The system cannot find the file specified: " + args[0]);
return;
}

try {
Files.move(oldFile.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
System.out.println("File renamed from " + oldFile.getName() + " to " + newFile.getName());
} catch (IOException e) {
throw new IOException("Failed to rename file: " + e.getMessage(), e);
}
}

@Override
public String description() {
return "Renames a file or directory.";
}

@Override
public String usage() {
return "rename <oldName> <newName>";
}
}