Shell 将多行字符串传递到批处理文件

Shell 将多行字符串传递到批处理文件,shell,batch-file,vbscript,Shell,Batch File,Vbscript,我正在从vbscript向批处理文件传递多行字符串。但是,当我试图从批处理文件获取整个字符串时,它只接收第一行。如何让批处理文件知道如何读取整个字符串,而不是停止在换行符处 输入: C:\组件A C:\组件B C:\ComponentC VBScript: multstring = "C:\Component_A" + Chr(13) + Chr(10) + "C:\ComponentB" + Chr(13) + Chr(10) + "C:\ComponentC" + Chr(13

我正在从vbscript向批处理文件传递多行字符串。但是,当我试图从批处理文件获取整个字符串时,它只接收第一行。如何让批处理文件知道如何读取整个字符串,而不是停止在换行符处

输入:
C:\组件A

C:\组件B

C:\ComponentC

VBScript:

multstring = "C:\Component_A" + Chr(13) + Chr(10) + "C:\ComponentB" +
       Chr(13) + Chr(10) + "C:\ComponentC" + Chr(13) + Chr(10)

script_path = "runscript.bat """ + multstring + """

Shell(script_path)
批次:

set "scriptargs=%~1"
echo "%scriptargs%"
setlocal enableDelayedExpansion
echo !scriptargs!
我得到的输出:

C:\组件A

需要输出:

C:\组件A

C:\组件B

C:\ComponentC


多行字符串不能通过命令行参数传递给批处理文件,但可以在环境变量中包含新行

VBS可以使用换行符定义环境变量,然后调用的批处理脚本可以使用延迟扩展读取和显示该值

测试.vbs

set wshShell = CreateObject("WScript.Shell")
set Env=wshShell.Environment("PROCESS")

multstring="C:\Component_A" + Chr(13) + Chr(10) + "C:\ComponentB" + _
            Chr(13) + Chr(10) + "C:\ComponentC" + Chr(13) + Chr(10)

Env("arg1") = multstring

script_path = "runscript.bat arg1"

wshShell.Run script_path
runscript.bat

@echo off
setlocal enableDelayedExpansion
echo !%1!
pause
请注意,这适用于VBS调用的批处理脚本,因为新批处理脚本继承VBS环境的副本


如果批处理脚本调用VBS,则VBS无法通过环境变量将值传递回调用者,因为一旦VBS结束,VBS环境将丢失。

如dbenham所述,最好通过引用传递多行字符串

但是如果您确实需要按值传递它们,这也是可能的 问题是如何访问它们

这是一个经过改编的版本

来自控制台的示例调用(需要空行!):

无法访问回车,因为它们总是被cmd.exe删除


但当使用次优内容时,仍然存在问题。

cmd不适用于多行字符串。一次只有一行。您可以通过管道
|
将输入传输到要处理的脚本,但这需要一些奇特的脚本逻辑。我建议您只需将输入连接到带有分隔符的单行中(如
),然后在脚本中使用
@DavidRuhmann解析它,这正是我最终要做的。通过一个特殊的分隔符分隔。感谢您的回复。您发布的VBS代码无效-它无法提供您声称的输出。总结代码时,请注意张贴工作示例。@DavidRuhmann-cmd可以处理环境变量中的多行字符串。但多行值确实不能作为命令行参数传递。吹毛求疵:您可以将多行字符串传递给批处理文件,但访问它们有点棘手1)您的
SET“var=content”REM中有引号…
注释正在杀死代码的内容。2) 您正在向结果中添加尾随空格。3) 我不明白您如何区分\n和\r\n。4) 您在某些行上有尾随空格问题5)您没有显示如何调用脚本-我相信它需要引用变量的延迟扩展
“!argVar!”
,这意味着您仍将通过引用从VBS中传递值,必须通过
CMD/V:ON/C…
1)运行批处理,我不知道您指的是哪一行?2) 不,没有尾随空格,由示例3)证明您无法检测批处理的
\r
,4)哪一行?5) 我添加了一个示例,通过值传递
得到了(并修复了它),我使用其他代码进行了测试,而不是发布的代码:-)当我用4行测试参数时,第一行添加了2个尾随空格,第二行和第三行添加了1个尾随空格,最后一行是OK。我通过在末尾添加
echo(!param!>test.txt
进行测试,并使用十六进制编辑器查看内容。现在我看到param.tmp已将所有
\r
剥离,然后每行都以
\r\n
终止。因此,是的,使用此技术无法检测
\r
@echo off
setlocal DisableExtensions DisableDelayedExpansion
REM Write the complete parameter to a temp file
@echo on
@(for %%A in (%%A) do (
    @goto :break
    REM # %1 #
    REM
)) > param.tmp
:break
@echo off

REM receive all lines from the temp file and store them in an array
setlocal EnableExtensions
set cnt=0
for /F "skip=3 delims=" %%a in (param.tmp) DO (
    set /a cnt+=1
    setlocal EnableDelayedExpansion
    for /F %%n in ( "!cnt!" ) do (
        setlocal DisableDelayedExpansion
        set "param%%n=%%a"
    )
)
setlocal EnableDelayedExpansion
set \n=^


REM Build the \n variable with a linefeed character, the two empty lines are required

REM Build from the array a single parameter
set /a cnt-=2
set "param1=!param1:~6!"    REM Remove the 'REM #' from the first line
set "param%cnt%=!param%cnt%:~0,-3!" REM Remove the trailing '#' from the last line
set "param="
for /L %%n in (1 1 !cnt!) do (
    if %%n GTR 1 set "param=!param!!\n!"
    set "param=!param!!param%%n:~1!"
)
echo The param is: '!param!'
test.bat ^"hello^

Line2^

Line3"