1

I am trying to run shell commands from VBA and get output into a csv file. Below is the code I am using:

Dim wsh as Object
Set wsh = VBA.CreateObject("WScript.Shell")  
plink_path="C:\plink.exe"
key_path="putty key path"
pass_query="select * from test"
command1 = Replace(plink_path & " hadoop@11.11.11.11 -i " & key_path & " mysql -uuser -ppass -e 'use radar;" & pass_query & "'", Chr(10), " ")
wsh.Run command1 & ">E:/anurag.csv", 0, True

But I am not able to view output file in the E drive. When I run the above command manually from a cmd prompt I do get an output in the E drive.

cxw
  • 16,685
  • 2
  • 45
  • 81
anurag
  • 590
  • 3
  • 8
  • 27
  • 1
    If it sends output to STDOUT, [something like this might](https://msdn.microsoft.com/en-us/library/cbxxzwb5(v=vs.84).aspx) be useful. – Comintern Dec 07 '16 at 13:44

1 Answers1

1

Two thoughts:

  1. Try E:\anurag.csv instead of E:/anurag.csv
  2. Use cmd to invoke plink, since cmd usually processes the redirections. Replace the wsh.Run line with:

    command1 = command1 & ">E:\anurag.csv"
    command1 = "cmd /c """ & command1 & """"
    wsh.Run command1, 0, True
    

    The first line completes the command you wanted to execute and the second wraps it in a cmd /c call.

    • If this doesn't work, try changing /c above to /c /s per this answer.

YMMV - not tested

Community
  • 1
  • 1
cxw
  • 16,685
  • 2
  • 45
  • 81
  • It worked perfectly when i used command1 = "cmd /c """ & command1 & """".Thumbs up for that. – anurag Dec 07 '16 at 16:11