Last active
September 20, 2015 02:20
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" & _ | |
Chr(34) | |
lngErrorCode = wsh.Run(strRunBatchCommand, _ | |
WindowStyle:=0, _ | |
WaitOnReturn:=True) | |
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, _ | |
WaitOnReturn:=False) | |
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