Passing selected files to their Default Program?

Everything else that doesn't fall into one of the other PB categories.
CodingBlind
User
User
Posts: 11
Joined: Wed Mar 22, 2023 5:39 pm

Passing selected files to their Default Program?

Post by CodingBlind »

As per subject, I would like to be able to select a group of files in my presently, very simple Explorer application and pass them for processing / playing, in their default application; In particular, mp3 files. Once submitted, I require no further interaction from the executing program.

Using RunProgram(MyFile$) one file can be played.

The obvious solution is to create a .m3u or .pls file. For non-music files, one might create a batchfile .bat.

Is there a better (more technical way of doing this? How would Windows do it?

Many Thanks in advance
Wayne
BarryG
Addict
Addict
Posts: 4268
Joined: Thu Apr 18, 2019 8:17 am

Re: Passing selected files to their Default Program?

Post by BarryG »

Does the default program take command-line parameters for the files being sent? If so, you can use RunProgram in a loop of all files to submit, with each file being RunProgram's parameters flag.

Something like this:

Code: Select all

Dim filelist$(3)
filelist$(1)="One.mp3"
filelist$(2)="Two.mp3"
filelist$(3)="Three.mp3"

tool$="c:\something.exe" ; App that does the processing.
tooldir$=GetPathPart(tool$)

For n=1 To 3 ; Send all files one-by-one to the processing tool.
  RunProgram(tool$,filelist$(n),tooldir$,#PB_Program_Hide|#PB_Program_Wait)
Next

Debug "All files processed!"
Axolotl
Addict
Addict
Posts: 897
Joined: Wed Dec 31, 2008 3:36 pm

Re: Passing selected files to their Default Program?

Post by Axolotl »

If you don't want to specify the related app, you can use different ways to detect the associated file extention.
Keep in Mind, that windows is doing this by the file extensions.
Easy Test with the cli tool

Code: Select all

assoc [.ext[=[fileType]]]
All the stuff you need is in the registry (database)

Code: Select all

Procedure.s GetAssociatedProgram(Extension$)  
; see also Link below 
Datei verknüpfen


Another Idea: (probably wrong)

Code: Select all

Global File$ = "" 
; instead of:
RunProgram(File$) 
; use this 
ShellExecute_(0, @"open", @File$, 0, 0, #SW_SHOW) 
; because 2'nd Parameter "open" can be used for more precise tasks 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
CodingBlind
User
User
Posts: 11
Joined: Wed Mar 22, 2023 5:39 pm

Re: Passing selected files to their Default Program?

Post by CodingBlind »

BarryG,
Thank you for your reply and code snippet.

As suggested by you, the command line provided a solution to this problem. My preferred player is the legacy version of Winamp; and it does play space-separated filenames.

My first reaction was that the command line would not provide enough space. That perception harks back to my foray into 8086 Assembly Language programming in the early '90s. The first instruction of a .com program begins at an offset of 100 Hex, with the FF bytes below that, available for the command line.

Fortunately, under Windows, the command line provides 8191 bytes. Even using UTF8 strings, that is 4094 characters.

Entering ProgName$ and FileNames$ strings as parameters in RunProgram() the compiler returned an 'Incorrect Number of parameters' error, until I added a Working Directory. From my reading of the Hellp topic, all parameters other than ProgName are optional?

Whilst it is unlikely I would be sharing my program, if I did, the problem would be knowing which player the user has on their system. The filetype information is stored in Registry and I'm not going there!

So, I favour the playlist approach.

While I have been composing this verbose message, another reply has arrived. So, I am keen to read another person's thoughts.

Wayne
Post Reply