2015-02-06 29 views
1

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

输入:
C:\ ComponentA

C:\以componentB

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:\ ComponentA

输出通缉:

C:\ ComponentA

C:\以componentB

C: \ ComponentC

+2

** cmd不适用于多行字符串**一次只能有一行。您可以将输入“|”输入到要处理的脚本中,但这需要一些奇特的脚本逻辑。我建议你只需用分隔符将输入连接成一行(比如';'),然后用'for'在脚本中解析它。 – 2015-02-06 15:53:35

+0

@DavidRuhmann这正是我最终做的。通过特殊分隔符分隔。感谢您的回应。 – user2970916 2015-02-06 16:33:01

+0

您发布的VBS代码无效 - 它无法提供您声称的输出。汇总代码时请注意发布工作示例。 – dbenham 2015-02-07 04:14:23

回答

2

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

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

test.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环境就会丢失。

+0

Nitpicking:您可以将多行字符串传递到批处理文件,但访问它们有点棘手。 – jeb 2015-02-07 06:47:03

0

正如dbenham所说,通过引用传递多行字符串是一个好主意。

但是,如果你真的需要通过值通过它们,这也是可能的
问题是要访问它们。

这是How to receive even the strangest command line parameters?的改编版本。从控制台

@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" 

回车无法访问,因为他们总是被删除CMD.EXE

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

+0

1)您在'SET'var = content“REM ...”中引用了引用代码的内容。 2)您正在为结果添加尾随空格。 3)我看不出你如何区分\ n从\ r \ n。 4)你在某些行上有空间问题5)你不会显示如何调用你的脚本 - 我相信它需要引用一个变量''!!argVar!''延迟扩展,这意味着你仍然会通过VBS引用的值,并且必须通过'CMD/V:ON/C ...'运行批处理。#1 – dbenham 2015-02-07 13:48:12

+0

1)我不知道要引用哪条线? 2)不,不存在尾随空格,通过示例验证3)根本不能检测到具有批处理的'\ r'4)哪些行? 5)我添加了一个smaple,并通过''value'传递' – jeb 2015-02-07 14:08:53

+0

1)明白了(并修复了它),我使用其他代码测试了比发布的版本:-) – jeb 2015-02-07 14:18:54

相关问题