Solved

pushd command in VB  macro

Posted on 2013-11-01
5
1,326 Views
Last Modified: 2013-11-02
Folks

using a VB macro,how do I run the following commands in one go?



C:\>CD C:\

C:\>pushd c \\servername\folder\

Z:\\servername\folder\test.bat

thank in advance!
0
Comment
Question by:rutgermons
  • 2
  • 2
5 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 39618103
That commands are syntactically incorrect. What you mean is:
cd C:\
pushd \\servername\folder
test.bat

Open in new window

but even then the first line is superfluous (unless the batch file accesses relatives paths on drive C:, which would be very, ehm, dumb to do).
In VBS, you can use WScript.Shell.Run to execute batch commands in cmd.exe:
WScript.CreateObject("WScript.Shell").Run("cmd /c pushd \\servername\folder & test.bat")

Open in new window

You can provide a "window visibility option" as second parameter of the .Run, see http://msdn.microsoft.com/en-us/library/d5fk67ky(v=vs.84).aspx for details.
0
 
LVL 12

Expert Comment

by:Harry Lee
ID: 39618112
The best way to do this is probably create a Batch file to carry all the commands.

Copy con vbrun.bat
c:
cd\
pushd c \\servername\folder\
call z:\\servername\folder\test.bat

Open in new window

use F6 to save the bat file.

in the VB Script,

Function VBRun()
dim shell
set shell=createobject("wscript.shell")
shell.run "VBrun.bat"
set shell=nothing
End Function

Open in new window

0
 

Author Comment

by:rutgermons
ID: 39618161
qlemo

i tried to out ur statement in the macro but it stats object is required? pardon my ignorance, could u advise how next?
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39619321
"VB Macro", so you are in Visual Basic for Applications (that is, MS Office)?
Because it works great in VB Script. For VBA however we have to remove the WScript (and we can do the same in VBS, btw):
Sub test()
CreateObject("WScript.Shell").Run ("cmd /k pushd \\servername\folder & test.bat")
End Sub

Open in new window

I've changed /c to /k in cmd.exe for testing purposes - if there is an error message, you will be able to see it, as the cmd window will remain open. After testing you should change it back to /c, so the cmd window closes when finished.
0
 

Author Comment

by:rutgermons
ID: 39619544
Thank you Qlemo! this works perfectly well
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now