Launch a .bat file

MrDB4Excel

Active Member
Joined
Jan 29, 2004
Messages
334
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
 
Excellent. I am glad you got it working.

Whatever your favorite search engine, it is amazing the amount of things you can find on the internet!
It has given me the courage to do some things that I would have never tried before, like car and dryer repairs.
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I just discovered another problem. The folder I want to be deleted is hidden via Easy File Locker and unless the folder is unhidden the VBA does not complete thus giving me another error.
I assume because the VBA does not know how to deal with a locked and hidden folder.
Thoughts?
1631556487964.png
 
Upvote 0
I just discovered another problem. The folder I want to be deleted is hidden via Easy File Locker and unless the folder is unhidden the VBA does not complete thus giving me another error.
I assume because the VBA does not know how to deal with a locked and hidden folder.
Thoughts?
View attachment 46812
That is an interesting one. Unfortunately, I have no experience with Easy File Locker, and I don't know if Excel VBA can unhide folders made very hidden by it. I tried searching on the internet, but did not have much luck.

I would recommend posting that as a new question so it appears in the "Unanswered threads", and maybe someone has some ideas on how you might be able to do it.

Out of curiosity, does the batch file method work on these folders?
 
Upvote 0
Thanks, Joe4. Interesting indeed because just running a batch file I was able to delete a test folder with the test folder locked and hidden by Easy File Locker. So possibly the answer lies in calling the batch file to run via VBA. So I will look further, thanks for all your help and time.
 
Upvote 0
Thanks, Joe4. Interesting indeed because just running a batch file I was able to delete a test folder with the test folder locked and hidden by Easy File Locker. So possibly the answer lies in calling the batch file to run via VBA. So I will look further, thanks for all your help and time.
Interesting.

I had come across someone who experienced a similar error (the error number 5 that you reported), and it seemed like it *MAY* have been because the path that the batch file was located in had spaces in it, and it didn't seem to like that, without having to do something to address that (and I see that your path has spaces in it). You may want to do a quick test, where you try running the batch file from a folder path that does not have any spaces in it, and see if that makes any difference.

If you cannot get it to work out, feel free to post back here, and when I have a little more time tonight, I may be able to dive in a little deeper and do some more intense testing on my side.
 
Upvote 0
Interesting.

I had come across someone who experienced a similar error (the error number 5 that you reported), and it seemed like it *MAY* have been because the path that the batch file was located in had spaces in it, and it didn't seem to like that, without having to do something to address that (and I see that your path has spaces in it). You may want to do a quick test, where you try running the batch file from a folder path that does not have any spaces in it, and see if that makes any difference.

If you cannot get it to work out, feel free to post back here, and when I have a little more time tonight, I may be able to dive in a little deeper and do some more intense testing on my side.
I changed the location of the bat file, thus changing the syntax as follows:
Sub openBatch()
Dim file_path As String
file_path = “G:\DeleteJunk.bat”
Call Shell(file_path, vbNormalFocus)
End Sub

Here is the text from the bat file:
g:
if exist Testing\ (
echo Yes
) else (
echo No
)
rmdir Testing

I still get error 5
1631566016103.png


I even tried chaning the text in the bat file as follows:
g:
rmdir Testing

Running the bat file outside of Excel works, but inside Excel I still get this error 5

When I select "Debug" I get this. Is there a chance inserting a new Module in the selected sheet is detrimental?
I am lost. I have searched online and cannot seem to get an answer.
1631566321860.png
 
Upvote 0
OK, so I set up the whole scenario on my computer, just like you showed, and it seems to work for me. So I cannot figure out why it won't work for you.
I have using Excel 365, but I have seen people use Shell command on versions of Excel older than Excel 2013, like you are using, so I don't think that should be the issue.

Where have you saved this Excel file? It isn't saved in the same directory you are trying to delete, is it?

One last thing I found for you to try (I really had to dig deep to find it, but some people say this worked):
VBA Code:
Sub openBatch()
    Call Shell("""G:\DeleteJunk.bat""", vbNormalFocus)
End Sub
 
Upvote 0
Yes, I do have Excel 2013. This xlsm file is in F:\Dwight\In The Event Of My Death. Not sure this will help but I shall remove all spaces in this folder.
Again I even tried:
Sub openBatch()
Call Shell("""G:\DeleteJunk.bat""", vbNormalFocus)
End Sub
And again I get the error 5 and this message
1631599400896.png

I will keep looking, maybe I can find something.
 
Upvote 0
Are you using McAfee? It will block some commands and they're not very good at informing the user.
If you are using it, look into Adaptive Threat Protection and see if it's on to check Office apps.
 
Upvote 0
Are you using McAfee? It will block some commands and they're not very good at informing the user.
I did come across another user who mentioned having the same sort of issues with an older version of "Trend Micro" anti-virus too.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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