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
 
I guess the quick test there would be to temporarily disable the antivirus and run the macro.
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Malwarebytes user here. While testing some code regarding this topic my test file was blocked. Tried one of my other projects and it was blocked before even got loaded. After some hours I learned that recently they made some changes and had to disable some new features. I was notified with informational pop-ups though.

Just wanted to let you all know.
 
Upvote 0
I am using Bitdefender and it seems to always block from Excel anything to do with cmd.exe but Bitdefender never blocks regular use of cmd.exe outside Excel so this seems puzzling.
 
Upvote 0
I am using Bitdefender and it seems to always block from Excel anything to do with cmd.exe but Bitdefender never blocks regular use of cmd.exe outside Excel so this seems puzzling.
I'm assuming it's in case it's a malicious macro that ran automatically - so block Office apps (the setting I know of in McAfee) but allow more "manual" methods through. But then they don't offer a way (that I found) to say "the code in this file is ok to run" - it's either ON or OFF for Office programs

Maybe there's a "safe folder" setting and you can put the workbook in there?
 
Upvote 0
Hi,

You may use this code to delete folder by its pathname in the MyFolder string variable.
Space chars in the folder's pathname are supported too.
VBA Code:
Sub KillFolder()
  Dim MyFolder As String
  MyFolder = "G:\Testing"
  CmdString = "CMD /C RMDIR /S /Q " & Chr(34) & MyFolder & Chr(34)
  Shell CmdString, vbHide
End Sub
 
Last edited:
Upvote 0
Hi,

You may use this code to delete folder by its pathname in the MyFolder string variable.
Space chars in the folder's pathname are supported too.
VBA Code:
Sub KillFolder()
  Dim MyFolder As String
  MyFolder = "G:\Testing"
  CmdString = "CMD /C RMDIR /S /Q " & Chr(34) & MyFolder & Chr(34)
  Shell CmdString, vbHide
End Sub
Your code would not work, but I found this to work:
Option Explicit
'VBA Removing specified Directory and its Content
Sub VBAF1_Remove_Directory_and_its_Content()

'Variable declaration
Dim sFolderPath As String, oFSO As Object
'Dim oFSO As FileSystem Object ' removed this line by inserting a Comment Marker [ ' ]

'Define Folder Path
sFolderPath = "G:\Testing\" ' Note the use of the complete path to the folder

'Check if slash is added
If Right(sFolderPath, 1) = "\" Then
'If added remove it from the specified path
sFolderPath = Left(sFolderPath, Len(sFolderPath) - 1)
End If

'Create FSO Object
Set oFSO = CreateObject("Scripting.FileSystemObject")

'Check Specified Folder exists or not
If oFSO.FolderExists(sFolderPath) Then

'Delete All Files
oFSO.DeleteFile sFolderPath & "*.*", True

'Delete All Subfolders
oFSO.DeleteFolder sFolderPath & "*.*", True

'Display Message
MsgBox "Specified directory has deleted.", vbInformation, "Testing" 'changed from VBAF1
Else
'Display Message
MsgBox "Specified directory doesn't exists.", vbInformation, "Testing" 'changed from VBAF1
End If

End Sub
 
Upvote 0
Have you tested it?
Anyway, it's great that you've solved the problem!
Yes, I tested both the one you wrote and the long one I replied with. The short one you wrote triggered my Bitdefender. The long one works flawlessly.
 
Upvote 0

Forum statistics

Threads
1,214,635
Messages
6,120,660
Members
448,975
Latest member
sweeberry

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