0
votes

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?

1
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
@Squashman Answers go in the answer field, not the comment field. Comments are for asking for clarification and such.VertigoRay
@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
@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
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

1 Answers

0
votes

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.