Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This script runs a batch file, but waits for that the batch file to finish before moving on (and eventually modifying the results of the batch file)
Option Explicit
Public Sub RunBatchFileThenRenameFolder()
Dim strNewFolderName As String, strRunBatchCommand As String, _
strRenameFolderCommand As String
Dim lngErrorCode As Long
Dim wks As Worksheet
Dim wsh As WshShell
Set wsh = New WshShell
'Set references up-front
Set wks = ThisWorkbook.Sheets("Sheet1") '<~ assume D3 and E3 are on Sheet1
'Get the new folder name
With wks
strNewFolderName = .Range("D3").Value & " " & .Range("E3").Value
End With
'Run the batch file using the WshShell object
strRunBatchCommand = Chr(34) & _
"C:\wshshell-fun\Create New 2015 Project Folder.bat" & _
lngErrorCode = wsh.Run(strRunBatchCommand, _
WindowStyle:=0, _
If lngErrorCode <> 0 Then
MsgBox "Uh oh! Something went wrong with the batch file!"
Exit Sub
End If
'Rename the just-created folder
strRenameFolderCommand = "cmd /K REN " & Chr(34) & _
"C:\wshshell-fun\2015XXX New Project" & _
Chr(34) & " " & Chr(34) & _
strNewFolderName & Chr(34)
lngErrorCode = wsh.Run(strRenameFolderCommand, _
WindowStyle:=0, _
If lngErrorCode <> 0 Then
MsgBox "Uh oh! Something went wrong with the rename!"
Exit Sub
End If
Msgbox "Folder created and renamed!"
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment