将参数从一个批处理文件传递到另一个 [英] Passing an argument from one batch file to another

查看:223
本文介绍了将参数从一个批处理文件传递到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个批处理文件.

第一个需要输入参数,它是父文件夹的路径.它会读取父文件夹内所有子文件夹的名称,并为每个子文件夹执行第二个批处理文件.

打包文件1

@echo off
for  /d %%Y in (%1/*) do (  
set SubfolderNameAndPath=%%Y
call batch2.bat %SubfolderNameAndPath% 
)

第二个批处理文件使用SubfolderNameAndPath作为输入参数,并对每个子文件夹中存在的所有文件执行Evaluate.exe.它将Evaluate.exe的结果保存到带有扩展名的文本文件结果"中,该扩展名带有每次被访问的子文件夹的名称. (results_%~n1.txt).

打包文件2

@echo off
for  %%X in (%1/*) do (  
echo Evaluate.exe  %%X AnotherArgumentHere -o results_%~n1.txt   
)

当我运行batch1(batch1.bat ParentFolderPath)时,即使它似乎调用了batch2.bat,也不执行batch2.bat.我认为我为batch2.bat定义输入参数的方式出了问题,但我无法弄清楚它是什么.

%SubfolderNameAndPath%不包含任何空格.文件夹的路径都没有. 非常感谢您的帮助.

解决方案

首先,当放在方括号对中时,您不能设置变量,然后使用%来访问它.您必须首先(在for循环之前)setlocal enabledelayedexpansion,然后使用!而不是%来访问变量.

但是由于您仍然不对%SubfolderNameAndPath%做任何事情,因此应该消除它.

第二,您需要补偿包含空格的路径,因此希望下面的代码对您有用.为此,批处理对传递到的参数使用特殊的表示法,以删除它们周围的双引号.因此,如果%1 = "This string",则%~1 = This string.同样,如果%1 = This string,则%~1仍然= This string.因此,除非您要保留任何双引号,否则使用%~1表示法没有任何缺点.

因此,在整数batch1.bat中,我们删除了%1周围的所有潜在引号,然后在%~1/*周围加上了双引号,并在内容中留有双引号%%Y. >

batch1.bat

@echo off
for  /d %%Y in ("%~1/*") do call batch2.bat "%%~Y"

然后我们对batch2.bat做相同的事情,但更多.

batch2.bat

@echo off
for  %%X in ("%~1/*") do (
  echo Evaluate.exe  "%%~X" AnotherArgumentHere -o "results_%~n1.txt"
)


2012/09/05 15:21

这抓你好吗?而不是调用单独的批处理:

for /d %%x in (%1\*) do (
  for /f "tokens=*" %%y in ( 'dir /b /a:-d "%%~dpnxx"' ) do (
    echo Evaluate.exe %%~nxy AnotherArgumentHere -o "results_%%~nxx.txt" 
  )
)

(以上内容可以全部放在一行上,只需除去括号即可.)

我假设输出为:
Evaluate.exe <file_name> AnotherArgumentHere -o "results_<directory_name>.txt"

I have 2 batch files.

The first one needs an input argument which is a path of a parent folder. It reads the names of all subfolders inside the parent folder and executes the second batch file for each subfolder.

BATCH FILE 1

@echo off
for  /d %%Y in (%1/*) do (  
set SubfolderNameAndPath=%%Y
call batch2.bat %SubfolderNameAndPath% 
)

The second batch file uses as input argument the SubfolderNameAndPath and executes an Evaluate.exe for all the files that exist in each subfolder. It saves the results of the Evaluate.exe to a text file "results" with an extension that carries the name of the subfolder that each time has been accessed. (results_%~n1.txt).

BATCH FILE 2

@echo off
for  %%X in (%1/*) do (  
echo Evaluate.exe  %%X AnotherArgumentHere -o results_%~n1.txt   
)

When I run the batch1 (batch1.bat ParentFolderPath) even if it seems to call the batch2.bat, the batch2.bat is not executed. I believe that something goes wrong with the way that I define the input arguments for the batch2.bat but I cannot figure out what it is.

The %SubfolderNameAndPath% does not contain any spaces. Neither the path to the folder does. I would really appreciate your help on that.

解决方案

Well, first, when inside a bracketed pair you can't set a variable then access it with the %'s. You must first (before the for loop) setlocal enabledelayedexpansion, then access your variables with the !'s rather than the %'s.

But since you do not do anything with %SubfolderNameAndPath% anyway, you should just eliminate it.

Second, you need to compensate for paths containing spaces, so hopefully my code below will work out for you. To do this, batch uses a special notation for arguments passed to to remove double quotes around them. So, if %1 = "This string", then %~1 = This string. Also, if %1 = This string, then %~1 still = This string. So there is no drawback to using the %~1 notation, unless you want to retain any double quotes.

So, int batch1.bat, we remove any potential quotes surrounding %1, and then place double-quotes around %~1/*, and also double-quote %%Y just in case there are spaces in it.

batch1.bat

@echo off
for  /d %%Y in ("%~1/*") do call batch2.bat "%%~Y"

Then we do the same thing for batch2.bat, but more of it.

batch2.bat

@echo off
for  %%X in ("%~1/*") do (
  echo Evaluate.exe  "%%~X" AnotherArgumentHere -o "results_%~n1.txt"
)


EDIT: 2012/09/05 15:21

How's this grab you? Instead of calling a seprate batch:

for /d %%x in (%1\*) do (
  for /f "tokens=*" %%y in ( 'dir /b /a:-d "%%~dpnxx"' ) do (
    echo Evaluate.exe %%~nxy AnotherArgumentHere -o "results_%%~nxx.txt" 
  )
)

(The above can be put all on one line, just remove the brackets.)

I'm assuming the output is:
Evaluate.exe <file_name> AnotherArgumentHere -o "results_<directory_name>.txt"

这篇关于将参数从一个批处理文件传递到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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