如何以循环作为计划任务运行批处理文件?(How can I run a batch file with a loop as a scheduled task?)

编程入门 行业动态 更新时间:2024-10-28 20:20:39
如何以循环作为计划任务运行批处理文件?(How can I run a batch file with a loop as a scheduled task?)

我在批处理文件中有以下代码:

for /F "tokens=1,2" %%a in (%CD%\clients.txt) do ( start "My Task" /d "%CD%" /b php -f "%CD%\task.php" "%%a" "/%%b" )

如果我从命令提示符手动运行批处理文件,它可以正常工作。 但是,如果我将其作为计划任务运行,则会失败并显示上次运行结果(0x2)。 请注意,如果我从代码中删除循环并对要运行的进程中的变量进行硬编码,则任务运行正常。

我还注意到,当计划任务执行批处理文件时,任何类型的简单文件操作(例如echo test> log.txt)似乎都不会运行,但是可以手动运行。 我尝试将计划任务设置为“以最高权限运行”,但不修复它。 我还将计划任务操作的“开始”设置设置为PHP和文本文件所在的目录,但也不起作用。 这里的想法用完了,有什么想法吗?

I have the following code in a batch file:

for /F "tokens=1,2" %%a in (%CD%\clients.txt) do ( start "My Task" /d "%CD%" /b php -f "%CD%\task.php" "%%a" "/%%b" )

If I run the batch file manually from the command prompt, it works fine. However, if I run it as a scheduled task it fails and Last Run Result displays (0x2). Note that if I remove the loop from the code and hardcode values for the variables in the process to be run, the task runs fine.

I've also noticed that any kind of simple file operation, such as echo test > log.txt doesn't seem to run when the batch file is executed by the scheduled task, but works manually. I tried setting the scheduled task to "Run with highest privileges", doesn't fix it. I also set the "Start in" setting for the scheduled task action to the directory where the PHP and text files are, that doesn't work either. Am running out of ideas here, any thoughts?

最满意答案

我想到了。 正如iCodez在评论中指出的那样,%CD%并不是我所期望的。 文件是在我的简单测试中创建的,但是在错误的文件夹中。 问题是由于我已将HKEY_CURRENT_USER \ Software \ Microsoft \ Command Processor \ AutoRun更新为指向其他文件夹。 看起来这比我在计划任务中指定的Start In目录优先。 我删除了我的AutoRun键,事情开始工作了。 只是去表明编辑其中一些注册表项是有风险的!

I figured it out. As iCodez pointed out in the comments, %CD% was not what I expected it to be. Files were getting created in my simple tests, but in the wrong folder. The problem was caused by the fact that I had updated HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun to point to a different folder. Looks like this was taking precedence over the Start In directory I was specifying in the scheduled task. I removed my AutoRun key and things started working property. Just goes to show it's risky to edit some of those registry keys!

更多推荐

本文发布于:2023-07-21 09:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1208947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:批处理文件   计划   run   batch   task

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!