0

I have a batch file which, if I run manually, produces a text file with a single number. However, if I schedule it with Task Scheduler, it produces an "error". Let me know if there is anything obvious that I am doing wrong.

Batch file:

rem Call mysql_start. bat which starts mysql and runs get_rain.bat and sends output to raw_totals.txt
call mysql_start.bat

rem Open raw_totals and remove 'raincounter' header
type C:\CumulusMX\cocorahs_reports\raw_totals.txt | findstr /v raincounter >     C:\CumulusMX\cocorahs_reports\raw_totals_no_header.txt

rem Calculate 7a-7a rain and send output to 7-7_rain.txt
setlocal enabledelayedexpansion
set cnt=1
for /f "usebackq" %%i in ("C:\CumulusMX\cocorahs_reports\raw_totals_no_header.txt") do (
   set var!cnt!=%%i
   set /a cnt+=1
)
(powershell %var1% - %var2%) > C:\CumulusMX\cocorahs_reports\output.txt
powershell exit


rem Delete raw_totals and raw_totals_no_header
del /f C:\CumulusMX\cocorahs_reports\raw_totals.txt
del /f C:\CumulusMX\cocorahs_reports\raw_totals_no_header.txt

"Error" output:

'-' was specified as the argument to -Command but standard input has not been redirected for this process.

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
[-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]
[-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
[-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
[-ConfigurationName <string>]
[-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
[-Command { - | <script-block> [-args <arg-array>]
              | <string> [<CommandParameters>] } ]

PowerShell[.exe] -Help | -? | /?

(Cutting off for brevity)......
sbagnato
  • 603
  • 3
  • 11
  • 35
  • 2
    How is the action of your scheduled task defined? – stackprotector Jul 19 '20 at 17:25
  • 6
    Please read [What must be taken into account on executing a batch file as scheduled task?](https://stackoverflow.com/a/41821620/3074564) The second command line fails already if __Start in__ is not defined with the directory containing `mysql_start.bat`. `call "%~dp0mysql_start.bat"` would be most likely a solution for this command line, but all other command lines referencing files and folders with paths relative to path of current directory fail, too. A solution could be as second command line `cd /D "%~dp0"` to make the directory of the batch file the current directory. – Mofi Jul 19 '20 at 17:34
  • Your error message is exactly what you'd get if you ran `(powershell -) > C:\CumulusMX\cocorahs_reports\output.txt`. This means that the variables `%var1%` and `%var2%` are not defined at the time you use them in your `powershell` command. I assume that `%var1%` and `%var2%`, are supposed to be the first 'token', _(number)_ from the second and third lines respectively of `raw_totals.txt`. Could you please run your script again, but this time remove the `del /f C:\CumulusMX\cocorahs_reports\raw_totals.txt` line. You can then inspect the files content, or post it in your question for assistance. – Compo Jul 19 '20 at 18:55
  • @Mofi, good call! Totally glossed over that first batch call not including the full directory. That was the problem. Thanks! – sbagnato Jul 20 '20 at 00:38
  • 3
    @sbagnato - have you thot about doing this in pure powershell? the calls to other apps/utils/bat/cmd files can usually be made from inside powershell. plus, it looks like you have a CSV file that you are stripping the header from ... and PoSh can handle CSV files directly. [*grin*] – Lee_Dailey Jul 20 '20 at 02:12
  • @lee_dailey, not against it. However, no csv file, only txt. It's a "master" batch that runs a mysql script and then manipulates it. The ultimate goal is to take the final numeric value and enter it into a web form. I'm playing with Selenium to get that last part done, but first things first – sbagnato Jul 20 '20 at 12:53
  • @sbagnato - ah! well ... do what you are most comfy with. [*grin*] it may be worth your time to switch from BAT to PoSh just for the simplicity of only dealing with one data representation ... – Lee_Dailey Jul 20 '20 at 14:29
  • Also, what was wrong with using the answer provided for you in [your previous question](https://stackoverflow.com/q/62600998), where the `SELECT` performed the subtraction for you. – Compo Jul 22 '20 at 12:03

0 Answers0