批处理文件参数仅通过第一个字符

时间:2019-04-27 10:28:43

标签: python batch-file

我正在尝试将参数传递给批处理文件

我的文件仅包含以下代码:

set branch=%1
echo %branch%

使用参数Foo调用文件

输出

F

我似乎找不到错误的语法。

希望得到一些帮助


根据评论的要求:

完整,准确的脚本:

ECHO "Starting unit tests in get_unittests_results.bat"
@rem SET sainity_test_dir="C:\code\EPMD\%1\Templates\Testing\Main"
@rem SET testing_dir="C:\code\EPMD\%1\Templates\Testing"
@rem SET report_outcome_dir="%CD%\report_outcome"
@rem SET sainity_file_name="sanity.py"
@rem SET dev_epd_python_path="C:\DEV_EPD_Prerequisite\Anaconda2\python.exe"
@rem cd %sainity_test_dir%
@rem %dev_epd_python_path% %sainity_file_name%


ECHO %1
set branch=%1
ECHO %branch%
ECHO "Starting unit tests in get_unittests_results.bat"
SET dev_epd_python_path="C:\DEV_EPD_Prerequisite\Anaconda2\python.exe"
SET sainity_test_file_path="..\..\EPMD\%1\Templates\Testing\Main\sanity.py"
ECHO %dev_epd_python_path% %sainity_test_file_path%
%dev_epd_python_path% %sainity_test_file_path%

由python脚本调用:

def run_batch(path, params):
    p = Popen(path.format(*params), cwd='.')
    stdout, stderr = p.communicate()
    if stderr is not None:
        print stderr

def create_unity_build(args):
    branch_name = args.branch
    run_batch(build_unity_script_path, [branch_name])

args.branch包含一个长度超过1(已测试)的字符串。

该批次仅接收该字符串的第一个字母(当我更改第一个字母时会更改)。

2 个答案:

答案 0 :(得分:1)

在Python代码中,如果path是批处理文件路径的 string ,而params是参数的 list ,则可能会使用:

p = Popen([path] + params)

将一个列表与另一个列表连接(扩展)。

至于

p = Popen(path.format(*params), cwd='.')

路径必须是要格式化的模式,我不知道它的值以及如何创建。似乎很奇怪,因为路径是test.cmd {}之类的模式。可以省略cmd='.',因为当前目录为默认目录。

答案 1 :(得分:0)

由于没有足够的空间在评论中发布此内容,因此建议您首先调整以便更好地使用双引号:

Rem Echo "Starting unit tests in get_unittests_results.bat"
Rem Set "sainity_test_dir=C:\code\EPMD\%~1\Templates\Testing\Main"
Rem Set "testing_dir=C:\code\EPMD\%~1\Templates\Testing"
Rem Set "report_outcome_dir=%CD%\report_outcome"
Rem Set "sainity_file_name=sanity.py"
Rem Set "dev_epd_python_path=C:\DEV_EPD_Prerequisite\Anaconda2\python.exe"
Rem CD /D "%sainity_test_dir%"
Rem "%dev_epd_python_path%" "%sainity_file_name%"

Echo %1
Set "branch=%~1"
Echo %branch%
Echo "Starting unit tests in get_unittests_results.bat"
Set "dev_epd_python_path=C:\DEV_EPD_Prerequisite\Anaconda2\python.exe"
Set "sainity_test_file_path=..\..\EPMD\%~1\Templates\Testing\Main\sanity.py"
Echo "%dev_epd_python_path%" "%sainity_test_file_path%"
"%dev_epd_python_path%" "%sainity_test_file_path%"