1

I've been looking for the correct code for my batch file to start a program and open a file by extension only. I've been searching and all I can find is opening a file by filename only, without extension. I would like to do it the other way around. I've tried multiple

start "C:\program files\folder\program name.exe" d:\folder\filename.extension

I've found some answers for other programs and tried them but they didn't work. I've tried to replace filename by * but it didn't work as well.

The reason I'm asking is becasue the filename will change every buildnumber but the extension is unique in that folder. Hope you can help me out. Thanks

user1527079
  • 33
  • 1
  • 4

2 Answers2

2

You should be able to do:

start "" filename.extension

The double double quotes provide the name for the window for some weird, illogical, Microsoft-y reason.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • by `"weird, illogical, Microsoft-y"` I think your forgetting the purpose of the `start` command. Originally, it was used to create a new thread in dos. Hence, ***logically*** if you were creating a new thread: you would want to **name** it first and assign its task later. *EVERYTHING HAS A REASON* **:D** – Monacraft Jun 23 '14 at 08:34
  • Thank you for your reply. I think this doesn't do what I want. This still requires to know the filename. I want to open it by file extension only. The filename contains the buildnumber so it will different every build. – user1527079 Jun 23 '14 at 09:54
1

You can do as follows

rem ** go to the folder 
CD d:\folder

rem ** find all the filenames with required extension using a for-loop

FOR /F "tokens=1 delims=" %%A in ('DIR /b *.extension') do (
    rem ** Added some delay before start as per OP comments
    timeout 5
    rem ** use start command
    start "C:\program files\folder\program name.exe" %%A
)

Edit : Added Timeout for some delay before start as per OP's comment

Community
  • 1
  • 1
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
  • Thanks for your reply. I'm not sure why but it doesn't work. It shows the CMD screen but before I can read anything it closes again. Nothing happens. – user1527079 Jun 23 '14 at 10:10
  • I've discovered why it didn't work. I tried to debug it and put 'Pause' after every line. It worked just fine. Apparentle it needs a short pause between going to the folder and starting the application. – user1527079 Jun 23 '14 at 10:35
  • Yes I did. I added a 1 second pause and now it works fine! Thank you. – user1527079 Jun 23 '14 at 10:52
  • How is it possible this works? As @MarkSetchell mentioned, `start` needs the additional `""` to work. – Thomas Weller Jun 23 '14 at 10:58
  • @ThomasW. Please read [about the rabbit ears](http://stackoverflow.com/a/17204218/2982225), emphasis **unless it's the only parameter** – Infinite Recursion Jun 23 '14 at 11:08
  • You have two parameters, the Exe and %%A, so rabbit ears are needed. `start "notepad.exe"` (if %%A is empty) and `start "notepad.exe" test.txt` (if %%A is test.txt) both don't work. The latter one will start the default editor. – Thomas Weller Jun 23 '14 at 11:57