Skip to content

Commit 8bca22d

Browse files
Implement MoveCommand to move files
1 parent b0757de commit 8bca22d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.file.*;
8+
9+
public class MoveCommand implements Command {
10+
@Override
11+
public void execute(String[] args, ShellContext context) throws IOException {
12+
if (args.length < 2) {
13+
System.out.println("Usage: move <source> <destination>");
14+
return;
15+
}
16+
17+
Path source = context.resolvePath(args[0]).toPath();
18+
Path destination = context.resolvePath(args[1]).toPath();
19+
20+
if (!Files.exists(source)) {
21+
System.out.println("The system cannot find the file specified.");
22+
return;
23+
}
24+
25+
try {
26+
Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
27+
System.out.println("Moved: " + source.getFileName() + " -> " + destination);
28+
} catch (IOException e) {
29+
System.out.println("Error moving file: " + e.getMessage());
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)