0

I'm trying to get my Python apps executed using only one .bat file.

The code I have is:

powershell -c "& { $Args | % { pythonw $_ } } 'C:\Users\pc2\Dropbox\app1.py' 'C:\Users\pc2\Dropbox\app2.py' 'C:\Users\pc2\Dropbox\app3.py' 'C:\Users\pc2\Dropbox\app4.py' 'C:\Users\pc2\Dropbox\app5.py'"

pause

This should open in PowerShell all scripts, but when executed the bat file, the cmd window is opened and I get:

C:\Users\USER\Dropbox\Aplicaciones>powershell -c "& { $Args | \Users\pc2\Dropbox\Aplicaciones\OnlyBot\only.py' 'C:\Users\pc2\Dropbox\Aplicaciones\Linker2Bot\linker.py' 'C:\Users\pc2\Dropbox\Aplicaciones\Esmuybarato\barato.py' 'C:\Users\pc2\Dropbox\Aplicaciones\to2\to2.py' 'C:\Users\pc2\Dropbox\Aplicaciones\TrackerBOT\boxxon.py'"

Falta la cadena en el terminador: '. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

I notice the pythonw $_ is missing when the batch is executed.

How can I solve this?

Uniextra
  • 133
  • 3
  • 17
  • You need to double the percent symbol. The percent symbol is a special character in batch for referencing the value of a variable. So you need to escape the percent symbol by doubling it. – Squashman Mar 16 '18 at 16:51
  • @Squashman Answers go in the answer field, not the comment field. Comments are for asking for clarification and such. – VertigoRay Mar 16 '18 at 17:12
  • @VertigoRay, go nuts and post it as an answer. I really don't care. Majority of questions asked on StackOverFlow are duplicates. As is this one. I have probably answered this question a half dozen times myself over the years. – Squashman Mar 16 '18 at 17:15
  • @Squashman It's to help the rest of us know from the main menu that a possible answer has been posted. Allowing us to move on to questions without any answers. – VertigoRay Mar 16 '18 at 17:20
  • @VertigoRay I personally do not know powershell that well either. If there was other things wrong with his powershell my answer still would have been wrong. Do you just take it for granted that a question with an answer is correct and just skip reading the question? – Squashman Mar 16 '18 at 17:35
  • 2
    It is interesting to see that there are users who write a batch script interpreted by Windows command interpreter `cmd.exe` which executes a PowerShell command interpreted by `powershell.exe` to run several Python scripts interpreted by `pythonw.exe`. Why not writing a Python script which does all at once? Or why is a batch file used at all and not just a PowerShell script with file extension *.ps1? – Mofi Mar 16 '18 at 17:37
  • Powershell and Python are both first-class script hosts on Windows. Why the batch file wrapper? – jwdonahue Mar 16 '18 at 21:39
  • [This](https://stackoverflow.com/questions/45760457/how-to-run-a-powershell-script-with-white-spaces-in-path-from-command-line/45762288#45762288) should answer your question – vrdse Mar 17 '18 at 15:32
  • @Squashman I prefer to work on ones without answers presented. I don't assume all answers are correct. A suggested answer should be presented as an answer not a comment; even if it might not be correct/complete. I initially just felt bad cause you gave the answer and I felt like I would be taking your credit by posting it as the answer ... knowing you were correct. Anyway ... how I choose to work through the volunteer queue is my prerogative. How you choose to answer/comment is yours. Thanks for helping on SO. :) – VertigoRay Mar 18 '18 at 04:11

1 Answers1

0

As Squashman pointed out, you likely just need to double up the %:

powershell -c "& { $Args | %%{ pythonw $_ } } 'C:\Users\pc2\Dropbox\app1.py' 'C:\Users\pc2\Dropbox\app2.py' 'C:\Users\pc2\Dropbox\app3.py' 'C:\Users\pc2\Dropbox\app4.py' 'C:\Users\pc2\Dropbox\app5.py'"

The percent symbol is a special character in batch for referencing the value of a variable. So you need to escape the percent symbol by doubling it.

VertigoRay
  • 5,935
  • 6
  • 39
  • 48
  • This solved the problem, but anyway, the code is not working i run and the cmd shows correctly the excecution, but no script is runn – Uniextra Mar 17 '18 at 23:57