1

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!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Due to line no 26 and after translate error, have seen, that issue raise for "You must supply a value expression after the '-split' operator" so to resolve this issue, you could follow guideline from https://stackoverflow.com/questions/18582412/you-must-provide-a-value-expression-on-the-right-hand-side-of-the-operator and put "d" after -split – Hasanuzzaman Rana Sep 24 '20 at 14:59

1 Answers1

0

You're enclosing the arguments to powershell.exe in quotes, which makes sense. But this means you are executing a command like:

powershell.exe set-variable l abcdefg; $l -split "d"

Enter that at a Windows command prompt and you'll get the same error. The quotes are stripped off by the command interpreter, so they're not there anymore when Powershell executes the command.

Interactively, I was able to make it work by enclosing the double-quotes in single-quotes:

...>powershell.exe set-variable l abcdefg; $l -split '"d"'
abc
efg
Dave Costa
  • 47,262
  • 8
  • 56
  • 72
  • I am trying but I am in a string so I don't kn ow how to enclose with quotes inside quotes. I tried this: $line -split " + '"' + "d" + '"' + "" and doesn't work – syzygy syzygy Sep 24 '20 at 15:01