0

No doubt I have done something very simple wrong but I cant figure it out. I'm no expert, I'm writing my dissertation and am using .bat files with a series of PowerShell instructions to convert and move files around. An existing file that works executes a parser .exe in all subfolders:

powershell.exe -command "& {$start_path = 'C:\folder'; Get-ChildItem $start_path -Recurse -Directory | ForEach-Object {Set-Location $_.FullName | ".\OpenWeather6to6.exe"};}

c:\folder is a placeholder, OpenWeather6to6 is just the name I gave it.

Now the problem! I have another parser that generates SQL scripts that I want to run with the same PowerShell line. Both parsers work just fine if manually started either through explorer or CMD. But this second one won't run through .bat?? It's the exact identical script but with OpenWeatherSQL.exe.

.\OpenWeatherSQL.exe : The term '.\OpenWeatherSQL.exe' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At line:1 char:156
+ ...  | ForEach-Object {Set-Location $_.FullName | .\OpenWeatherSQL.exe};}
+                                                   ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\OpenWeatherSQL.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Could it have to do with the type of .exe? They're both from the same source file, I wrote them in CodeBlocks in C++, and they do pretty much the exact same thing. I'm baffled.

P.S. I know it's better to run them through a .ps1 script. However, in other places, I combined PS work with other functions that are easier to run through .bat and it doesn't really matter if its good code, it just has to do these few things and nobody will ever look at it or use it again; just need to get past this.

spikey_richie
  • 8,367
  • 2
  • 25
  • 42
Liron Chen
  • 15
  • 2
  • Please show us both commands exactly as they appear. it would probably be helpful to see the whole bat. also, are both executables in the same directory? – Frank Thomas Jul 13 '21 at 14:00
  • You are indeed making it very hard on yourself combining batch code and powershell code like this. Every time the command is executed, a new instance of powershell is started, so you loose things like working folder etc, which is most likely why this command fails. It probably starts that particular powershell instance in another folder. – LPChip Jul 13 '21 at 14:05
  • Please confirm in your adjusted script you have encapsulated the name in quotes `".\OpenWeatherSQL.exe"` as the error seems to imply that its not in quotes and hence trying to execute OpenWeatherSQL.exe from the current directory. – CraftyB Jul 13 '21 at 16:13
  • CraftyB I can confirm, it is the exact same script in each character except for the executables name. I checked over and over again. – Liron Chen Jul 13 '21 at 16:33
  • @LironChen - As per my original comment I think you were missing the quotes around the executable, this is identifiable by the error please see my example [here](https://imgur.com/NdkVuYN). as you can see from my example - 1, does not show quotes around the executable name and provides the exact same error as you were receiving. 2, shows the quotes as they were included in the original command, also provides a different error as it is not seeing the command as an executable instruction. – CraftyB Jul 14 '21 at 08:12
  • @CraftyB I appreciate the help, however I can verify with certainty that I did not omit the quotation marks in the code that threw up an error. However, its sorted now; I dont know why but Im happy to move on. – Liron Chen Jul 14 '21 at 19:27

1 Answers1

0

I have no idea why but it works now. The command is the same as far as I can tell?? I added the complete path, I cant understand why that would be any better than a placeholder but whatever, and the name of the .exe that didnt work.

The only thing that changed is that I edited the code of the parser and recompiled it. It must be that PowerShell cant run an executable with specific code inside it somehow; although all I changed were a few output formatting details.

powershell.exe -command "& {$start_path = 'C:\Users\lmche\Desktop\OpenWeather\6to6data'; Get-ChildItem $start_path -Recurse -Directory | ForEach-Object {Set-Location $_.FullName | ".\OpenWeatherSQL.exe"};}
Liron Chen
  • 15
  • 2