[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

pushd command in VB  macro

Posted on 2013-11-01
5
Medium Priority
?
1,622 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 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

834 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