1

I want to open local file on Windows 10 with url parameters.

Opening webpages has been answered in several questions (ie escaping amperstands).

But how to open local files?

The following opens the file without any parameters:

start "" "file:///C:/test.html?par1=1&par2=test"

I have found that without using start it works, but I have no idea why:

"C:\Program Files\Google\Chrome\Application\chrome.exe" "file:///C:/test.html?par1=1&par2=test"

Can anybody explain why it works without start and why does not work with start?

--EDIT--

So from the comments of Gerhard and Mofi I realized that the start command needs the title as the first parameter and then the executable with its parameters. So as they wrote it should have "" after the start command like so:

start "" "C:\Program Files\Google\Chrome\Application\chrome.exe" "file:///C:/test.html?par1=1&par2=test"
Richard
  • 59
  • 6
  • 3
    Are you sure?`;)` Your examples are different. The one explicitly runs `chrome.exe` and the other just `start`s the file, where the latter simply `start`s the file with the default program you have associated with the file type. So to imply that start is not working is incorrect. Try `start "" "C:\Program Files\Google\Chrome\Application\chrome.exe" "file:///C:/test.html?par1=1&par2=test"` as per the reference __"opening webpages has been answered in severarl questions.."__ does not apply at all here where the characters are double quoted, so you can remove the escape caret as per my example. – Gerhard Nov 25 '20 at 08:37
  • 1
    PS! if chrome is in the path, then you can just run it without the full path in the command with something in the line of: `start "" chrome.exe "file:///C:/test.html?par1=1&par2=test"` – Gerhard Nov 25 '20 at 08:41
  • The caret character `^` escapes `&` for being interpreted as literal character if the ampersand is __not__ inside an argument string enclosed in `"`. `&` and `^` are interpreted as literal characters on being specified inside a double quoted argument string. For that reason `^&` is wrong in both command lines as the URL of local file with parameters is enclosed in `"`. – Mofi Nov 25 '20 at 08:51
  • `start` uses the function [ShellExecuteEx](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecuteexa) to start the application associated with a file extension like `.html` or `.docx` or a protocol like `file:` or `http:` and pass the arguments used on `start` command line as arguments to the application. So the question is: Which application is associated with protocol `file:`? But there is special problem with protocol `file:` as it can be seen on comparing the file associations of `file:` and `http:` or `https:`. – Mofi Nov 25 '20 at 09:04
  • The answer lies in the [thread](https://stackoverflow.com/a/1327444) you linked; you would have found it if you had read more than just the first two answers… – aschipfl Nov 25 '20 at 09:09
  • Run in a command prompt window `reg query HKCR\http\shell\open\command /ve` and `reg query HKCR\https\shell\open\command /ve`. It can be seen which application is associated with these two protocols. Then run `reg query HKCR\file\shell\open\command /ve` and an error message is output as there is no such key. Run next `reg query HKCR\file /s` and it can be seen that there is not much defined for protocol `file`. The reason is that `file` in registry is also for `file` as type of a directory entry like `Directory` (only directories) and `Folder` (directories and virtual folders). – Mofi Nov 25 '20 at 09:11
  • But it is best to download and extract the free Sysinternals (Microsoft) tool [Process Monitor](https://learn.microsoft.com/en-us/sysinternals/downloads/procmon) to a directory of a local drive, start it, __add__ the two filters *Process Name is cmd.exe* and *Process Name is chrome.exe* (or whatever executable opens the local HTML file), run next in a command prompt window `start "" "file:///C:/test.html?par1=1&par2=test"`, switch back to Process Monitor, stop capturing with Ctrl+E and look on all the file system and registry entries from top to bottom. – Mofi Nov 25 '20 at 09:21
  • 1
    If you double click in Process Monitor log on first line with `chrome.exe` in column `Process Name` and switch to tab `Process`, you could see that `cmd.exe` used `"C:\Program Files\Google\Chrome\Application\chrome.exe" "C:\test.html"` to start Chrome and pass the file name and only the file name without `file:///` and without `?par1=1^&par2=test` and with `/` replaced by ``\`` to Chrome. The reason can be seen above as `cmd.exe` tried to find out which file could be meant by the argument `file:///C:/test.html?par1=1&par2=test` for starting using Windows File I/O functions. – Mofi Nov 25 '20 at 09:43
  • Please read the Microsoft documentation about [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file) for an explanation why `cmd.exe` splits up `"file:///C:/test.html?par1=1&par2=test"` on its approach to find an executable or script file with valid full qualified file name matching the argument to start. `start` is designed for starting executables and for that reason `cmd.exe` tries everything it can do to really find a file which can be started before using `ShellExecuteEx`. That is helpful in most cases, but not in this special case. – Mofi Nov 25 '20 at 09:43
  • 1
    You can use `start "" "%ProgramFiles%\Google\Chrome\Application\chrome.exe" "file:///C:/test.html?par1=1&par2=test"` in the batch file. If you want to open the local HTML file with the parameters in default browser associated with protocol `file`, you have to query the registry in your batch file to find out which executable is associated with protocol `file:` and use `start` with the executable file name and with `"file:///C:/test.html?par1=1&par2=test"` as argument for the browser executable. – Mofi Nov 25 '20 at 09:44

0 Answers0