Skip to content

Instantly share code, notes, and snippets.

@danwagnerco
Last active September 20, 2015 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danwagnerco/637408ef0d6a322bf990 to your computer and use it in GitHub Desktop.
Save danwagnerco/637408ef0d6a322bf990 to your computer and use it in GitHub Desktop.
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" & _
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