How do I create a .bat file for each row

ktmman

New Member
Joined
Dec 29, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
This is my first time with VBA so please bear with me

I am trying to open an mp4 file with potplayer with an command line to open at specific time in excel. There are multiple folders with hundreds of video files and I would like to open an .mp4 file at specific position. I figured the easiest solution to this would be to make a .bat file for each entry.

The command line in potplayer is
PotPlayerMini64.exe ["content"] [/switch]
so the following command will open "video.mp4" at 14 hours, 2 minutes and 6 seconds
PotPlayerMini64.exe "C:\Video output\2022-11-12\video.mp4" /seek=14:02:06
The following .bat file works perfectly
start C:\"Program Files"\DAUM\PotPlayer\PotPlayerMini64.exe "\\192.168.8.223\Vol\"Video output"\2022-11-12\2022-12-14 02-10-39.mp4" /seek=14:2:6

I have figured out how to generate a text file but there are so many variables that I can't figure out how to piece this thing together. The ideal process is to manually make the bat file after I've filled all the cells for that row and repeat the process for the second row.
2022_12_29_12_29_17_Book1_Excel.png
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
This should work for you:
VBA Code:
Option Explicit
Sub CreateAfile()
    Dim lRow As Long, i As Long
    lRow = Cells(Rows.Count, 4).End(xlUp).Row
    Dim filePath As String, fileName As String, fileExt As String, seekHour As String, seekMin As String, seekSec As String, outputPath As String
    Dim fs As Object, a As Object
   
    For i = 2 To lRow
      filePath = Cells(i, 3).Value
      fileName = Left(Cells(i, 4).Value, InStr(Cells(i, 4).Value, ".") - 1)
      fileExt = Right(Cells(i, 4).Value, InStr(Cells(i, 4).Value, ".") - 1)
      seekHour = Cells(i, 7).Value
      seekMin = Cells(i, 8).Value
      seekSec = Cells(i, 9).Value
      outputPath = Cells(i, 11).Value

      Set fs = CreateObject("Scripting.FileSystemObject")
      Set a = fs.CreateTextFile(outputPath & "\" & fileName & "x" & seekHour & "-" & seekMin & "-" & seekSec & ".bat", True)
      a.WriteLine ("start C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe """ & filePath & "\" & fileName & fileExt & """ /seek=" & seekHour & ":" & seekMin & ":" & seekSec)
      a.Close
    Next
End Sub
 
Upvote 0
A small adjustment to get more accurate file name:
VBA Code:
Option Explicit
Sub CreateAfile()
    Dim lRow As Long, i As Long
    Dim filePath As String, fileName As String, fileExt As String, seekHour As String, seekMin As String, seekSec As String, outputPath As String
    Dim fs As Object, a As Object

    Set fs = CreateObject("Scripting.FileSystemObject")
    lRow = Cells(Rows.Count, 4).End(xlUp).Row

    For i = 2 To lRow
      filePath = Cells(i, 3).Value
      fileName = Left(Cells(i, 4).Value, InStrRev(Cells(i, 4).Value, ".") - 1)
      fileExt = Right(Cells(i, 4).Value, InStrRev(Cells(i, 4).Value, ".") - 1)
      seekHour = Cells(i, 7).Value
      seekMin = Cells(i, 8).Value
      seekSec = Cells(i, 9).Value
      outputPath = Cells(i, 11).Value

      Set a = fs.CreateTextFile(outputPath & "\" & fileName & "x" & seekHour & "-" & seekMin & "-" & seekSec & ".bat", True)
      a.WriteLine ("start C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe """ & filePath & "\" & fileName & fileExt & """ /seek=" & seekHour & ":" & seekMin & ":" & seekSec)
      a.Close
    Next
End Sub
 
Last edited by a moderator:
Upvote 0
Hi, thank you very much, it worked. For some reason .bat files don't understand spaces in location ("C:\Program Files\...") so I just moved the potplayer install to someplace else. Same thing with the outputPath and filePath. For some reason also the fileExt would put out the name of the file instead of the file extension. Thanks again, I can figure out the rest.

Option Explicit
Sub CreateAfile()
Dim lRow As Long, i As Long
Dim filePath As String, fileName As String, fileExt As String, seekHour As String, seekMin As String, seekSec As String, outputPath As String
Dim fs As Object, a As Object

Set fs = CreateObject("Scripting.FileSystemObject")
lRow = Cells(Rows.Count, 4).End(xlUp).Row

For i = 2 To lRow
filePath = Cells(i, 3).Value
fileName = Left(Cells(i, 4).Value, InStrRev(Cells(i, 4).Value, ".") - 1)
fileExt = Right(Cells(i, 4).Value, InStrRev(Cells(i, 4).Value, ".") - 1)
seekHour = Cells(i, 7).Value
seekMin = Cells(i, 8).Value
seekSec = Cells(i, 9).Value
outputPath = Cells(i, 11).Value

Set a = fs.CreateTextFile(outputPath & "\" & fileName & "x" & seekHour & "-" & seekMin & "-" & seekSec & ".bat", True)
a.WriteLine ("start C:\DAUM\PotPlayer\PotPlayerMini64.exe """ & filePath & "\" & fileName & ".mp4" & """ /seek=" & seekHour & ":" & seekMin & ":" & seekSec)
a.Close
Next
End Sub
 
Upvote 0
Hi, thank you very much, it worked. For some reason .bat files don't understand spaces in location ("C:\Program Files\...") so I just moved the potplayer install to someplace else. Same thing with the outputPath and filePath.
Oh that's why you enclosed them between quotation marks. Maybe you should adjust your strings according that. For instance, double quotation mark means single quotation in vba. Ex: a.WriteLine ("start C:\""Program Files""\DAUM..
For some reason also the fileExt would put out the name of the file instead of the file extension. Thanks again, I can figure out the rest.
Thats weird. It finds the last "." and takes piece from the right.
 
Upvote 0
Ok for extension use instead:
VBA Code:
fileExt = Mid(Cells(i, 4).Value, InStrRev(Cells(i, 4).Value, "."), Len(Cells(i, 4).Value))
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,195
Members
453,021
Latest member
pingpong7117

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