简体   繁体   English

如何获取计划任务调用批处理文件的名称,并将其传递给Java程序?

[英]How can I get name of scheduled task calling batch file, and pass it to Java program?

Let's say I have multiple scheduled tasks running at different times, but all of which are running a common Windows batch file. 假设我有多个计划任务在不同时间运行,但所有这些任务都运行一个常见的Windows批处理文件。 This batch file in turn executes a Java program. 该批处理文件又执行Java程序。

I would like my batch file to get the name of the scheduled task that's calling it, and pass its name to the program that my batch is executing. 我希望我的批处理文件获取调用它的计划任务的名称,并将其名称传递给我的批处理正在执行的程序。 How can I do this? 我怎样才能做到这一点?

You could pass the name of the scheduled task as an argument to your batch file. 您可以将计划任务的名称作为参数传递给批处理文件。 You cannot figure it out from inside the batch without help from outside. 没有外界的帮助,你无法从批量内部弄明白。

Like Joey said there is no way to do it without getting help from outside. 像乔伊说的那样,如果没有外界的帮助,就没有办法做到这一点。

You could create a separate instance of the batch for each task with an argument in each one specifying which task is assigned to run it. 您可以为每个任务创建一个单独的批处理实例,每个任务中的参数指定分配哪个任务来运行它。 You could also create smaller batches like this one: 您还可以创建像这样的小批量:

CALL mybatch.bat 1st_task

and that would pass the name of your first task into the batch as the %1 variable. 这会将您的第一个任务的名称作为%1变量传递给批处理。

You could also have your batch figure it out based on the time that it was run using the %time% variable, but this would need some parsing I'm sure as you can't always guarantee it runs at the same time down to the second. 您也可以根据使用%time%变量运行的时间让您的批量计算出来,但这需要一些解析我确定,因为您不能总是保证它同时运行到第二。

it might look something like this: 它可能看起来像这样:

if '%time:~0,5%'=='10:30' set var=1st_task
if '%time:~0,5%'=='12:00' set var=2nd_task

and so on 等等

(That last one assumes that your tasks only run at specified times during the day... and if for some reason they execute at a different time this won't work) (最后一个假设您的任务仅在白天的指定时间运行...如果由于某种原因它们在不同的时间执行,这将无效)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM