I am using Java.
I am trying to execute the dir command in powershell.exe and for each line try to do a -split to remove all the 'd' of each line.
Well, It doesn't work, this is the error:
Directorio: C:\Users\2dam\Desktop\JavaApplication9
En l�nea: 1 Car�cter: 98
+ ... Directorio: C:\Users\2dam\Desktop\JavaApplication9; $line -split d
+ ~
Debe proporcionar una expresi�n de valor despu�s del operador '-split'.
En l�nea: 1 Car�cter: 100
+ ... Directorio: C:\Users\2dam\Desktop\JavaApplication9; $line -split d
+ ~
Token 'd' inesperado en la expresi�n o la instrucci�n.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression
It's in Spanish but in sort is saying that it doesn't expect the 'd' and that it needs an expression after -split, here is my code.
package javaapplication9;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaApplication9 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
//String command = "powershell.exe your command";
//Getting the version
String command = "powershell.exe dir";
// Executing the command
Process powerShellProcess = Runtime.getRuntime().exec(command);
// Getting the results
powerShellProcess.getOutputStream().close();
String line;
System.out.println("Standard Output:");
BufferedReader stdout = new BufferedReader(new InputStreamReader(
powerShellProcess.getInputStream()));
while ((line = stdout.readLine()) != null) {
String[] commands = {"powershell.exe", "Set-Variable", "-Name \"line\" -Value \""+line+"\";", " $line -split \"d\""};
//Getting the version
System.out.println(line);
// Executing the command
Process powerShellProcess2 = Runtime.getRuntime().exec(commands);
// Getting the results
powerShellProcess2.getOutputStream().close();
String line2;
BufferedReader stdout2 = new BufferedReader(new InputStreamReader(
powerShellProcess2.getInputStream()));
while ((line2 = stdout2.readLine()) != null) {
System.out.println(line2);
}
BufferedReader stderr2 = new BufferedReader(new InputStreamReader(
powerShellProcess2.getErrorStream()));
while ((line2 = stderr2.readLine()) != null) {
System.out.println(line2);
}
stderr2.close();
}
}
}
Any Ideas on what I am doing wrong?
Thanks!