Launch a .bat file

MrDB4Excel

Active Member
Joined
Jan 29, 2004
Messages
348
Office Version
  1. 2013
Platform
  1. Windows
Is there a way to launch and run a batch file from within a macro-enabled Excel file?
The .bat file exists in the same directory as the .xlsm file. This directory is not the C drive but another folder on my F drive.
Any suggestions are much appreciated.
The content of this .bat file is very simple as follows:
g:
if exist directory name\ (
echo Yes
) else (
echo No
)
rmdir directory name
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
So I guess I need to learn how to fish; i.e. I do not know enough about DOS and VBA combined to make what I need to do to function. In addition, this Run .bat file from Excel VBA, dynamically site for me is like trying to learn Chinese. Very difficult to extract out of the many lines and various ways of doing something I suspect is so simple that I gain nothing from it. Walking away scratching my head and feeling like I am dumber than a box of rocks.
 
Upvote 0
Here is a good video tutorial that shows you how you can use a Sheell command to rub a bat file.

Note: I am not sure if the bat file you posted is actually what you want to do, or just a simple example of a bat file, because you can easily complete those steps in your example directly in VBA without the having to use a bat file. There are lots of examples you can find about that, if you do a simple Google Search on it. Here are a few:
 
Upvote 0
So sorry. I was not clear enough. I know the directory exists, or at least relatively sure it does. I only want to remove the directory. Still lost.
I do not want to create a directory, the main emphasis is to delete it. So these two sites tell me what I already know.
What I do not know is how to get a bat file to run from a macro button inside an excel xlsm file.

Okay, time to be specific. This xlsm file is a file my wife will open when I kick the bucket, when I buy the farm, when it is time for my dirt nap, etcetera. She is not Excel proficient in the least, but she is smart enough to pick a button that says Pick here before you continue. This xlsm file is all the things she must do when I am pushing up daisies. She will not understand to go to a directory where the bat file exists and double click it to run it. She is very PC deficient, got it?
 
Upvote 0
What I do not know is how to get a bat file to run from a macro button inside an excel xlsm file.
All buttons in Excel have VBA code attached to them - that is what they do, call/run VBA code.
So you really have two options here:
1. Have the VBA code call your batch file (Iike shown in the video linked in my last reply).
2. Have the VBA code remove the directory directly (no need to actually use VBA code do this).

There are tons of posts out there that show you how to remove directories/folders in Excel VBA directly that can be found easily with a Google Search, if you want to go with the second option and avoid using a batch file at all.

Once you have created the VBA code that you are going to use, you would simply then add a button to your worksheet, and assign your VBA code to that button.
Microsoft has a step-by-step tutorial showing you exactly how to do that here:

By the way, I would STRONGLY suggest using the simple "Form Control" button, and not the "ActiveX Control" button.
It is simpler, and has less issues.
 
Upvote 0
Followed the YouTube vid and it does not work.
This is what I put in the Inserted Module of the sheet where I want to macro button to exist:
Sub openBatch()
Dim file_path As String
file_path = "F:\Dwight\In The Event Of My Death\DeleteJunk.bat"
Call Shell(file_path, vbNormalFocus)

End Sub
This is what I get
1631548581072.png
 
Upvote 0
Not having much luck with your suggestions. In addition, I did not go to VBA University, therefore for you to suggest "Have the VBA code remove the directory directly (no need to actually use VBA code do this)." is essentially not at all helpful. I would not begin to know what to write for this VBA code.

I suspect in lieu of "Have the VBA code remove the directory directly (no need to actually use VBA code do this)." you meant to write "Have the VBA code remove the directory directly (no need to actually use a batch file to do this)."
 
Upvote 0
I found a similar post with a similar error
In addition, I did not go to VBA University, therefore for you to suggest "Have the VBA code remove the directory directly (no need to actually use VBA code do this)." is essentially not at all helpful. I would not begin to know what to write for this VBA code.
I do not expect you to write the code yourself. You must have missed this part:
There are tons of posts out there that show you how to remove directories/folders in Excel VBA directly that can be found easily with a Google Search, if you want to go with the second option and avoid using a batch file at all.
Just Google "Excel VBA remove directories", and you will find more examples that you will know what to do with.

If you don't want to be bothered in doing that and want someone to write the exact code you need, then you will need to explain (in plain English) exactly what you want the code to do.
Just like you did not go to "VBA University", I did not go to "Batch File University". So while I have a general sense of what your batch file does, I am not exactly sure if it is deleting all of directory G or some subset of it.
 
Upvote 0
Sorry for the insinuation. I did Google (actually I hate Google, I use DuckDuckGo) Excel VBA remove directories and got the answer right off and it works perfectly.
Sometimes at my age, major brain farts prohibit thinking straight. No need for the batch file, the VBA is perfect. Thanks for your patience with this old fart.
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,453
Members
452,514
Latest member
cjkelly15

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