4

The problem: When I double click the .bat file it executes as expected. When I schedule it in Windows Task Scheduler it executes except the line that has cscript.

Content of .bat file:

@echo off
cls

cscript CSV_To_Excel.vbs c:\tableaudata\test.csv c:\tableaudata\test.xlsx
echo.file converted >>log.txt

What is throwing me off is the fact that log.txt gets created indicating that the .bat file is being executed. But .xlsx is not created. However, on manually double clicking .bat both log.txt and test.xlsx is created.

What could be the problem?

Rian Smith
  • 363
  • 2
  • 4
  • 12
  • You do not specify paths to your script (only script name). I've seen this causing problems with Task Scheduler (even when the script is in the supposed/set working directory) – wmz Sep 27 '12 at 19:49
  • Used path for everything and still the same result: `C:\Windows\System32\cscript.exe c:\tableaudata\CSV_To_Excel.vbs c:\tableaudata\test.csv c:\tableaudata\test.xlsx` – Rian Smith Sep 27 '12 at 20:26
  • Are you 100% sure it executes? Do you see something similar to: `Result: The task completed with an exit code of (0)` in log? Have you deleted/renamed the log prior to testing? – wmz Sep 27 '12 at 20:59
  • Yes, 100% sure it executes. I delete log.txt before each run and it gets recreated with the message "file converted". I also tried dumping the cscript line to output.txt. That also gets created. Here is the content in output.txt: `Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation. All rights reserved.` – Rian Smith Sep 27 '12 at 22:10
  • Maybe this would help: http://stackoverflow.com/questions/8925908/how-to-start-a-vbs-script-in-windows-7-task-scheduler-with-messagebox. You could also add win version and if you run it under your account or other one so hopefully you'll get better answers. Sorry I couldn't be of more help... – wmz Sep 27 '12 at 22:38
  • Tried all 4 ways from that post and the same result. Hey, THANKS for helping!! – Rian Smith Sep 27 '12 at 22:55

3 Answers3

4

Resolved!! In the windows task scheduler I had to click "change user or group" button and add "Administrators" group.

Rian Smith
  • 363
  • 2
  • 4
  • 12
  • Exactly the same problem. Solved by running as a user with admin privileges AND selecting a checkbox called "Run with highest privileges" before it would work. – Simon Hutchison Sep 07 '16 at 05:43
2

To help debug the situation, add the following to the end of your cscript command line:

>>c:\MyCScriptOutput.txt 2>&1

Then, check to see if the c:\MyCScriptOutput.txt file has any error message(s) in it. If it does, please add this information (both the command line and the output) to your question.

I'm speculating, but the problem might be that cscript is trying and failing to run interactively, so you could try replacing "cscript" in your command line with "cscript //Nologo //B", to see if that fixes it.

Mattias Andersson
  • 2,331
  • 1
  • 17
  • 27
  • MyCScriptOutput.txt has the following text in it: `Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation. All rights reserved.` When I run with //NoLogo //B the file has no content. Again, no xlsx created. – Rian Smith Sep 28 '12 at 17:15
  • If it helps, when I doubleclick the .bat file manually, the command window appears, stays for a bit (I'm guessing for the duration of the conversion proess), then disappears. When the task scheduler executes, the command window doesn't appear. I just see log.txt created. – Rian Smith Sep 28 '12 at 17:19
  • It's hard to debug CSV_To_Excel.vbs without seeing it, but does it import any other units or make any file references that might depend on the working directory? What happens if you make all of the cscript line file references absolute paths and add the line [START OF LINE] cd /d "%~dp0" [END OF LINE] before your cscript line? – Mattias Andersson Sep 28 '12 at 17:53
  • Here are the files I'm using: [http://dropcanvas.com/5ckz9](http://dropcanvas.com/5ckz9) Adding that line to .bat didn't yield the expected result (I'm already using all absolute paths in .bat file) – Rian Smith Sep 28 '12 at 18:09
0

The main problem is you don't specify full path to your CSV_To_Excel.vbs Scheduler execute script from c:\windows\system32 (where schtasks.exe located) So, your batch call to cscript should be

cscript %~dp0\CSV_To_Excel.vbs c:\tableaudata\test.csv c:\tableaudata\test.xlsx
echo.file converted >> %~dp0\log.txt

Sergey Enns
  • 141
  • 1
  • 5