1

This works with CMD:

rar a c:\new.rar c:\*.*

This works running script.vbs, but does not do too much!:

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

It is no longer returning errors, but file new.rar is not being created either, it says 'a' is not a valid parameter

doggy
  • 75
  • 1
  • 8

2 Answers2

1

(1) Always echo the command before you try to feed it to run:

>> 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:\*.*

Obviously your concatenation puts a spurious "&" in the result.

(2) Never let you be persuaded to put random additions like "cmd /c cd /d" into you command.

(3) Build your command line in a structured way. E.g:

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

output:

cscript y.vbs
"C:\Program Files (x86)\WinRAR" a "c:\some path with spaces\new.rar" "c:\source dir with more spaces\*.*"

See here for background and reasons why.

Update:

This way, blunders (just the path, not the full filespec of rar mentioned) can be spotted and corrected easily:

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
Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • echo, of course!, thanks for tips , code works great, never seen so many quotations, will vote up as soon as I get my rank! such a newb,This is my first script! Thanks! – doggy Apr 23 '15 at 01:35
-1

Try this code and let me know if it works or not for you ?

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
'************************************

EDIT : Check this code :

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
'************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70