bat file run

stephenpovey

New Member
Joined
Dec 19, 2005
Messages
5
Hi, I'm trying to run a bat file from a vba macro. I've used x = shell("AllCopy.bat",1) and it runs but it doesn't do what its supposed to (i.e copy files into folders). please help!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
This line of code will run the AUTOEXEC.BAT file;
Code:
Shell "C:\WINDOWS\system32\cmd.exe" & " C:\AUTOEXEC.BAT"
If your batch file runs but doesn't do what it is suposed to do that is a different issue.
 
Upvote 0
Just to expand a bit on John's totally accurate reply (if I may):
If you use cmd.exe without specifying the path, shell will actually use the path(s) specified in the system environment variable "Path". So it is generally preferable to use cmd.exe with no path. one other random thought... If you use the command switch /c the bat file window will autoclose. So you could make it run silently like so:
Code:
VBA.Shell "cmd.exe /c c:\autoexec.bat", vbHide

One other thing. If you are on a Windows 98 Machine (and I think ME). You will need to use "command.exe" instead of "cmd.exe".
 
Upvote 0
Oorang,

Didn't know that. Thanks for the info. I will add it to my notes file.
 
Upvote 0
respree,

You are correct, I just pasted an example of the syntax that worked for me in the past. I should have edited it to show his "allcopy.bat" file.
 
Upvote 0
Is it possible to run a .bat file in a random directory?

I have a .bat file that deletes old versions of design files. I am working on creating an app that will run the purge.bat file to a given dir, zip all files in that dir then delete all files not including the zip.

Edit (added text below):
This is basically what I am looking at:
Code:
Shell "cmd.exe cd \\server\constant dir\constant dir\random dir\random.dir\" purge.bat

Open the system window, change directory to a directory dictated by an excel entry, run the .bat file.
 
Upvote 0
Joe, this is an old thread so I'm not sure how it relates.

You have several issues here. UNC paths are not available like that in a DOS Shell. You can't change to a folder and drive fron another drive like that anyway. If you can't do it in a DOS shell, you can't do it with the Shell() command.

If you need a UNC, try using the DOS command Subst and make a virtual drive for the UNC. You can the change to the "drive" in a shell and then use cd but in this case, use VBA's CHDIR.

Even so, I would code the BAT file to accept a parameter and use %1 in the BAT file to do something with the passed string such as a subfolder.

Here is an example:
Code:
Sub t()
ChDir "c:\myfiles\excel"
Shell "cmd /c dir *.* > c:\t.txt", vbHide
End Sub
 
Upvote 0
Kenneth,

Could you please post what
Code:
Shell "cmd /c dir *.* > c:\t.txt", vbHide
means so I can figure out what it is that I need to place into this?

Thank you
 
Upvote 0

Forum statistics

Threads
1,215,692
Messages
6,126,228
Members
449,303
Latest member
grantrob

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top