使用FOR循环将批处理文件中的变量传递到另一个批处理文件,以处理文件夹列表 [英] Passing a variable in batch file to another batch file with FOR loop to process list of folders

查看:44
本文介绍了使用FOR循环将批处理文件中的变量传递到另一个批处理文件,以处理文件夹列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DVD上有一个文件夹列表,我希望一次处理一个文件夹.我正在尝试使用FOR命令来传递信息以将其设置为第二个批处理文件中的变量.

I Have a list of folders on DVD that I would like to process one at a time. I am trying to use a FOR command to pass through information to set as the variable in a 2nd batch file.

我让用户输入两个变量,然后我想从我的文件夹列表中输入第三个变量.然后为DVD上的每个文件夹运行第二个批处理文件:

I am having the user input two variables, then I'd like a 3rd variable to come from my list of folders. Then run a 2nd batch file for each folder on the DVD:

echo enter Drive letter
set /p Drive=

echo enter diskname
set /p diskname=

FOR /F %%i IN ('dir /ad /b e:\') DO set book=%%i


call Load.bat %drive% %diskname% %book%

然后在我的load.bat文件中:

Then in my load.bat I have:

Set drive=%1
Set diskname=%2
Set book=%3

Copy %drive%\%book% \\server\%book%

该脚本将在DVD上的一个文件夹中正常运行.我怎样才能让它遍历每个文件夹?

The script will run fine on ONE folder on the DVD. How can I get this to loop through each folder?

谢谢!

推荐答案

FOR /F %%i IN ('dir /ad /b e:\') DO (
 set book=%%i
 call Load.bat %drive% %diskname% %%i
)

请注意,如果任何参数可能包含空格之类的分隔符,则该参数需要加引号"(必须用双引号引起来),然后应使用%〜n (其中n = 1..3)以访问子批次中的参数.

Note that if any of the paramaters may contains separators like spaces, the parameter needs to be "quoted" (must be double quotes) and you should then use %~n (where n=1..3 in your case) to acess the parameter in the subsidiary batch.

(必须与 do 关键字在同一物理行上.其他数据可以在单独的行上,也可以全部在一行上,但在这种情况下,它们需要用& 分隔,例如

the ( must be on the same physical line as the do keyword. The other data may be on seperate lines, or may be all on one line if you wish, but in that case they need to be separated by &, eg

FOR...DO set book=%%i&call Load.bat "%drive%" "%diskname%" "%%i"

请注意,如果数据全部在一行上,则不需要括号.为了简洁起见,我省略了 for 循环的主体,并为每个参数插入了引号,因此子批处理将需要%〜1 .. %〜3 删除这些引号.

Note that if the data is all on one line, the parentheses are not required. I've omitted the body of the for loop for brevity's sake and inserted the quotes for each parameter, so the subsidiary batch would require %~1..%~3 to strip off those quotes.

实际上,您不需要将数据作为参数发送.子批处理可以访问(和更改)环境变量,因此在执行子批处理时,这三个参数已经存在于变量中.

Actually, you don't need to send the data as parameters. The subsidiary batch can access (and change) the environment variables, so those three parameters are already present in the variables when the subsidiary batch is executed.

这篇关于使用FOR循环将批处理文件中的变量传递到另一个批处理文件,以处理文件夹列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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