Create Bat file from Excel

EmtelNOC

New Member
Joined
Oct 17, 2022
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
how to make VBA copy every cell from a column to a different .bat file. The location and file name are in the excel. For Example:

A contain FileName

B contains location

C has the script for the Batch file

A & B are different values.

Please help
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this macro. Values start in row 2.
VBA Code:
Public Sub Create_Bat_Files()

    Dim r As Long
    
    With ActiveSheet
        For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
            Open .Cells(r, "B").Value & IIf(Right(.Cells(r, "B").Value, 1) <> "\", "\", "") & .Cells(r, "A").Value For Output As #1
            Print #1, .Cells(r, "C").Value
            Close #1
        Next
    End With
    
End Sub
 
Upvote 0
check the comments for the one starting with <<<<<

VBA Code:
Option Explicit

Sub CreateBatchFiles()
    Dim rIn As Range
    Dim lR As Long
    
    Set rIn = Range("A1").CurrentRegion
    
    For lR = 1 To rIn.Rows.Count    ' <<<<<<  If A1 is header row, then change lR= 1 to LR=2
        data_to_text_file rIn.Cells(lR, 1), rIn.Cells(lR, 2), rIn.Cells(lR, 3)
    Next lR
End Sub

Private Sub data_to_text_file(sPath As String, sFName As String, sCmd As String)

    Dim iTextFile As Integer
    Dim sMyFile As String
    
    
    'check if path ends with path separator
    sPath = CheckPath(sPath)
    'check if filename ends with .BAT
    sFName = CheckFName(sFName)
    'path to the text file
    sMyFile = sPath & sFName
    
    'define FreeFile to the variable file number
    iTextFile = FreeFile

    'using append command to add text to the end of the file
    Open sMyFile For Output As iTextFile

    'add data to the text file
    
    Print #iTextFile, sCmd

    'close command to close the text file after adding data
    Close #iTextFile

End Sub

Private Function CheckPath(sPath As String) As String
    Dim sSep As String
    
    If sPath Like "*/*" Then
        sSep = "/"
    Else
        sSep = "\"
    End If
    
    If Not Right(sPath, 1) Like sSep Then
        sPath = sPath & sSep
    End If
    CheckPath = sPath
End Function

Private Function CheckFName(sFName As String) As String
    
    
    If Not sFName Like "*.bat" Then
        sFName = sFName & ".bat"
    End If
    CheckFName = sFName
End Function
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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