在批处理文件中访问未知数量的命令(参数) [英] Accessing unknown number of commands (parameters) in batch file

查看:98
本文介绍了在批处理文件中访问未知数量的命令(参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个有点难以解释,但我会尽力而为.

this one's a bit difficult to explain, but I'll do my best.

我正在通过字符串数组将目录列表传递到批处理文件中,该字符串数组是用Java创建的,然后使用Runtime.getRuntime().exec(commands)传递给.bat.我遇到的麻烦是关于访问命令数组的问题,命令数组的大小可能因执行而异.例如,在一次运行中,命令"可能包含以下内容:

I'm passing a list of directories into a batch file via a string array, which is created in Java and then passed into the .bat using Runtime.getRuntime().exec(commands). The trouble I am having is in regards to accessing the commands array, the size of which may vary from execution to execution. For example, during one run, "Commands" may contain the following:

{"cmd.exe","/C",开始","program.bat","stringA","stringB","stringC"}

{"cmd.exe", "/C", "Start", "program.bat", "stringA", "stringB", "stringC"}

前四个元素用于调用批处理文件,因此只有字符串A,B和C作为参数传递到批处理文件(program.bat)中.但是,在下一次运行时,命令"可能看起来像这样:

The first four elements are used to call the batch file, so only strings A, B, and C are passed into the batch file (program.bat) as parameters. However, on the next run, "commands" may look like this:

{"cmd.exe","/C",开始","program.bat","stringA","stringB","stringC",stringD,stringE}

{"cmd.exe", "/C", "Start", "program.bat", "stringA", "stringB", "stringC", stringD, stringE}

如您所见,参数列表中又添加了两个字符串.我的问题是:在我的批处理文件中,我有这个:

As you can see, there are two more strings added to the parameters list. My question is this: In my batch file I have this:

::Get stringA (param 1)
set stringA=%1
::Get stringB (param 2)
set stringB=%2
::Get stringC (param 3)
set stringC=%3

这将使用三个字符串参数(来自第一个"commands"数组),并将局部变量设置为传递给相应参数的任何值.我想知道是否有一种方法可以从批处理文件中确定参数的数量(例如,从第二个命令"数组),并相应地设置/创建适当数量的局部变量.我主要关注Java,因此批处理文件对我来说还是一个新手.任何建议将不胜感激,因为我一直试图独自解决这一问题,但没有成功.

This takes the three string parameters (from the first "commands" array) and sets local variables to whatever values are passed in to the corresponding parameters. I am wondering if there is a way to determine the number of parameters (from the second "commands" array, for example) from within the batch file, and set/create the proper number of local variables accordingly. I focus primarily on Java, so batch files are still fairly new to me. Any suggestions will be very much appreciated, as I've been trying to figure this one out for a while on my own with no success.

推荐答案

如此处所述:

https://stackoverflow.com/a/14298769/955143

您可以从%1到%9进行读取,并使用SHIFT键从数组中删除第一个元素,然后将它们重新编号为新数字.

You can read by %1 to %9, and use SHIFT to delete the first element from the array, an then renummerate they to new numbers.

例如,%2变为%1,第10个元素变为第9个元素,您可以通过%9进行访问.

For instance, the %2 becomes %1, and the 10th element becomes the 9th, and you can acess it by %9.

由于读取的值变为空字符串,因此您已经到达数组的末尾

Since the read value become a empty string you have reached the end of the array

您可以编写一个循环(使用goto或for)来读取%1值,然后使用SHIFT将第二个元素旋转到第一个位置,并检查它是否不为空,然后重复该过程.

You can write a loop (with goto or for) to reade the %1 value, and use SHIFT to rotate the 2nd element to the first position, and check if it isn't empty, them repeat the process.

这篇关于在批处理文件中访问未知数量的命令(参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆