Wrap your code in a VBS script and point your task to this file
VBscript's run method can execute batch commands in a hidden window, if you set "intWindowStyle" to 0.
object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
Put this code into your HiddenTask.vbs file.
Set wShell = CreateObject ("Wscript.Shell")
wShell.Run "cmd /c copy /Y nul D:\keepalive.txt", 0
I successfully tested your example. It creates a new empty file just like it should. Of course without displaying any window.
Read this SO question regarding your issue that it's not working when used as scheduled task. Grant your job administrator rights and set the "start in" folder. Also, check the event log for this task.
Additionally I made some screenshots. Maybe you are able to find the difference to your setup.
![enter image description here](../../I/static/images/ae1048242db4af4ee4ce42acbf408ae0cf709a2425ff289885dcbe291a2c1512.png)
IMPORTANT
If you're facing an issue after following the instructions here, please read the comments below, the issue may be the file name or path.
1
Wrap your code in a VBS script and point your task to this file. VBS scripts can run CMD commands in a hidden window. Read this answer to see how it works. Remember: In your case, you can put the CMD command
– nixda – 2013-09-06T09:42:25.730copy /Y nul B:\keepalive.txt
directly into that VBS, and no additional CMD file is needed like I explained over there. Currently at work I can't write a full answer, I hope you get the idea.@nixda, why is your excellent comment not posted as an answer? – Dave – 2013-09-06T11:07:07.640
@nixda, this doesn't work as advertised :) there as two issues I faced trying to make this work. First one, which isn't a big deal, when I used the command
copy /Y nul B:\keepalive.txt
in the VBS file it returns an error, something like the script can't file the specified file, so I put the entirecmd /C copy /Y nul B:\keepalive.txt
command. The second issue, the scheduled task cannot run VBS files and always displays "How do you want to open this file?" dialog. I'm on Windows 8, as previously said, and double-clicking the file works; i.e. the file is executed. – TheBlueSky – 2013-09-06T11:43:58.153