2

I'm trying to add a watcher for a folder, once file is created, start a program

$script= "C:\test.py"
$watcher.Path ="e:\my folder\test folder\dropbox"    

Start-Process cmd -Argument "/k python $script $path" 

Problem is that, this will always get me error saying e:\my no such file

I know this is an issue with space, but I tried the following, still get same error

Start-Process cmd -Argument "/k python $script '$path'" 

does anyone know how to fix this?

many thanks

0

2 Answers 2

1

You need to use escaped embedded double quotes with Start-Process:

$script= "C:\test.py"
$path = "e:\my folder\test folder\dropbox"    

Start-Process cmd -Argument "/k python $script `"$path`"" 

Without that, the spaces in the $path value break the path into multiple arguments when cmd receives the arguments.

`" embeds a verbatim " inside a "..." string.

As an aside:

  • If your intent is to run a command asynchronously, in a new window (on Windows), as your command suggests, Start-Process is indeed the right tool.

  • By contrast, do not use Start-Process if you want to run other console applications (such as cmd) synchronously, in the same window; in that event, just invoke the console application directly - see this answer for more.

Sign up to request clarification or add additional context in comments.

Comments

0

I was trying to run a powershell cmd to parse the input, specifically to get the version of chrome running on the server.

     String version = null;
     String command = "powershell.exe (Get-Item \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\").VersionInfo";
     Process process = Runtime.getRuntime().exec(command);
     process.getOutputStream().close();
     BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));       
     String line = reader.readLine();
     while(line!=null){
         if(line.matches("[0-9]{2,3}\\.[0-9]{1,2}\\..*")){
             version = line.substring(0, line.indexOf("."));
         }             
         line = reader.readLine();                
     }       

This failed with the error indicating that the command was split at the spaces in the path.

I replaced the escaped double quotes with single quotes and it worked:

String command = "powershell.exe (Get-Item 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe').VersionInfo";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.