批处理文件参数只传递第一个字符

问题描述 投票:2回答:2

我试图将参数传递给批处理文件

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

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的字符串(已测试)。

批处理只接收该字符串的第一个字母(当我更改第一个字母时更改)。

python batch-file
2个回答
1
投票

在Python代码中,如果path是批处理文件路径的字符串而params是参数列表,那么可能会使用:

p = Popen([path] + params)

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

至于

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

path需要是一个要格式化的模式,我不知道它的价值和创建方式。似乎很奇怪,因为路径将是像test.cmd {}这样的模式。可以省略cmd='.',因为当前目录是默认的。


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%"
© www.soinside.com 2019 - 2024. All rights reserved.