Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
shell脚本无法使用参数工作_Shell_Vbscript_Parameters_Winrar - Fatal编程技术网

shell脚本无法使用参数工作

shell脚本无法使用参数工作,shell,vbscript,parameters,winrar,Shell,Vbscript,Parameters,Winrar,这适用于CMD: rar a c:\new.rar c:\*.* 这可以在运行script.vbs时工作,但不会太多!: Function qq(strIn) qq = Chr(34) & strIn & Chr(34) End Function Set sh = CreateObject("WScript.Shell") MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR")

这适用于CMD:

rar a c:\new.rar c:\*.*
这可以在运行script.vbs时工作,但不会太多!:

Function qq(strIn)
    qq = Chr(34) & strIn & Chr(34)
    End Function
    Set sh = CreateObject("WScript.Shell")
MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & a c:\new.rar c:\*.* "

sh.run MyCmd,1,True

它不再返回错误,但文件new.rar也没有被创建,它说“a”不是有效的参数

请尝试此代码,并让我知道它是否适用于您

Set sh = CreateObject("WScript.Shell")
MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & rar /? & pause"
sh.run MyCmd,1,True
'************************************
Function qq(strIn)
qq = Chr(34) & strIn & Chr(34)
End Function
'************************************
编辑:检查此代码:

Set sh = CreateObject("WScript.Shell")
MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & rar a c:\new.rar c:\*.* & pause"
sh.run MyCmd,1,True
'************************************
Function qq(strIn)
qq = Chr(34) & strIn & Chr(34)
End Function
'************************************
(1) 在尝试将命令馈送到运行之前,始终回显命令:

>> MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & a c:\new.rar c:\*.* "
>> WScript.Echo MyCmd
>>
Cmd /c CD /D "C:\Program Files (x86)\WinRAR" & a c:\new.rar c:\*.*
显然,您的串联在结果中放入了一个虚假的“&”

(2) 千万不要被说服在命令中添加诸如“cmd/c cd/d”之类的随机添加项

(3) 以结构化的方式构建命令行。例如:

Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Dim sCmd  : sCmd  = Join(Array( _
     qq("C:\Program Files (x86)\WinRAR") _
   , "a" _
   , qq("c:\some path with spaces\new.rar") _
   , qq("c:\source dir with more spaces\*.*") _
))
WScript.Echo sCmd
输出:

cscript y.vbs
"C:\Program Files (x86)\WinRAR" a "c:\some path with spaces\new.rar" "c:\source dir with more spaces\*.*"
有关背景和原因,请参阅

更新:

这样,就可以很容易地发现和纠正错误(只是路径错误,而不是提到的rar的完整文件规范):

Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Dim sCmd  : sCmd  = Join(Array( _
     qq("C:\Program Files (x86)\WinRAR\rar.exe") _
   , "a" _
   , qq("c:\some path with spaces\new.rar") _
   , qq("c:\source dir with more spaces\*.*") _
))
WScript.Echo sCmd

向我们展示您如何使用参数进行测试?通过编辑您的第一篇博文谢谢,您的函数用于暂停,但如上所述,也谢谢,我不知道我应该编辑第一篇博文OK检查我的编辑,如果它有效,请不要忘记投票并接受它;)至少在发布前打印您的结果。你离开了这条路!@Ekkehard Horner你为什么对我的代码投否决票?它对我有用??我的回答正是他所期望的目标!!当然是回声!,谢谢你的提示,代码很好用,从来没有见过这么多的引文,我一拿到排名就会投票!真是个新手,这是我的第一个剧本!谢谢