is there a way to execute a cmd command like "move FolderA FolderB" without creating a .bat file and start it? It would be nice if it would work without creating files on HDD.
Asked
Active
Viewed 114 times
0
-
Why is Java Tagged in this question ? – Sharp Edge Feb 18 '14 at 14:38
-
@Sharpedge I assume they want to do it from a Java program; though I only inferred it from the tag, so I could be wrong. – slim Feb 18 '14 at 14:52
-
possible duplicate of [Run cmd commands through java](http://stackoverflow.com/questions/15464111/run-cmd-commands-through-java) – Kevin Panko Feb 18 '14 at 18:08
2 Answers
1
Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","move","dirA/a.txt","dirB"});

PeterMmm
- 24,152
- 13
- 73
- 111
1
Process process = new ProcessBuilder("cmd.exe",
"/c","move","dirA/a.txt","dirB").start();
ProcessBuilder
is preferred to Runtime.exec()
since Java 1.5, according to JavaDoc.
Be sure to read the Process
Javadoc to understand how to read from and write to processes.
Shelling out for commands like move
is bad practice, because it's neither portable nor secure. Work with File
classes instead. But sometimes you have to shell out to interact with more esoteric external programs.

slim
- 40,215
- 13
- 94
- 127
-
I sadly have to shell out and make the function "windows-only", because java let windows do the file-moving-process. only single cmd commands move files in a sorted order. – Feb 18 '14 at 18:35
-
There is definitely nothing you can do with 'move' that you can't do with pure Java. – slim Feb 18 '14 at 18:41
-
That is not right. When java or apache commons do the moving, the process begins e.g. at "e.txt" instead of "a.txt", if you 26 files, a.txt....z.txt – Feb 19 '14 at 06:46
-
I suggest you put your java move code into a new question and ask why it's skipping a-d. There will be a fault in your code. – slim Feb 19 '14 at 08:09
-
Hi, I think I found the problem. After moving a file, java needs to wait 0.1 seconds. Than the moving is in a sorted order. – Feb 19 '14 at 13:11