0

I'm getting wrong with the older version Microsoft Edge and now I can't open pdf files with it. Though the newer version is available, I don't really like the newer version.Yesterday I learned that I can use Microsoft Edge to open pdf file through command line like below, but I don't know how to dynamically attach the filepath of the pdf to my batch script when I choose to open the pdf with my batch script.

start shell:AppsFolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge file:\\\d:\\xxx.pdf
ishin go
  • 3
  • 1

3 Answers3

1

Open a command prompt, run call /? and read the output help carefully and completely. It explains how batch file arguments can be referenced from within a batch file. Argument 0 is always the batch file itself.

The batch file for your purpose could be coded as:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FileName=%~1"
if not defined FileName goto PrintHelp
set "FileName=%FileName:"=%"
if not defined FileName goto PrintHelp
if "%FileName%" == "/?" goto PrintHelp
set "FileName=%FileName:/=\%"
if not exist "%FileName%" (
    echo ERROR: File not found: "%FileName%"
    echo(
    pause
    goto EndBatch
)
set "FileName=%FileName:\=/%"
if not "%FileName:~0,1%" == "/" set "FileName=///%FileName%"
start "" shell:AppsFolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge "file:%FileName%"
goto EndBatch
:PrintHelp
echo INFO: %~nx0 must be called with name of a file to open in Microsoft Edge.
echo(
pause
:EndBatch
endlocal

I do not really understand why a batch file is needed at all to open PDF files in Microsoft Edge. It would be also possible to simply associate the file extension .pdf with Microsoft Edge to get PDF files opened by default with Microsoft Edge.

The following command line can be executed in a command prompt window to get displayed which application is associated currently with file extension .pdf and which command is used to open a PDF file with %1 being the placeholder for the file name:

for /F "skip=1 tokens=2*" %I in ('%SystemRoot%\System32\reg.exe query HKEY_CLASSES_ROOT\.pdf /ve 2^>nul') do %SystemRoot%\System32\reg.exe query "HKEY_CLASSES_ROOT\%J\Shell\Open\Command" /ve

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • pause /?
  • set /?
  • setlocal /?
  • start /?

See also the Wikipedia article about file URI scheme. It would be necessary to percent encode the file name for a 100% correct file URL, but Microsoft Edge supports also not correct encoded file URLs containing for example a space character instead of %20 or the umlaut ä instead of %c3%a4.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • In fact, I do this because I can not open with with 'open with' microsoft egde as I may misdelete some items in my regedit! – ishin go Apr 30 '21 at 05:42
0

I'd assume that you could open your pdf located in the same directory as your , with the assistance of :

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "filename=myfile! - 100%% virus free.pdf"
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NoLogo -Command "[System.Diagnostics.Process]::Start(\"msedge\",\"`\"%~dp0%filename%`\"\")"

You would replace my example filename with your own on line 2, (taking special care not to touch the closing doublequote). Please note that I have deliberately used a filename of myfile! - 100%% virus free.pdf, to show you that you must double any percent character in your filename, (as the percent character requires special treatment when used in a batch file).

Compo
  • 36,585
  • 5
  • 27
  • 39
0

In fact I got my answer after viewing some dos doc. And below is my solution(which is available for open pdf with 'open with' this batch script. To avoid opening the cmd window(it's ugly to my view), I also convert it to a exe using bat2exe converter.

@echo off
set prefix=file:///
set filepath=%~f1
set truepath=%filepath%%filepath:\=/%
start shell:AppsFolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge %truepath%
ishin go
  • 3
  • 1
  • That code really works? Sorry, but I cannot believe that. The environment variable `prefix` is not used at all. I suppose `prefix` should be the first referenced environment variable on definition of environment variable `truepath` which contains most likely by mistake twice `filepath`. The definition of `prefix` is not needed at all as `truepath` could be defined with `file:///` too. `set filepath=%~f1` is not working with a file name like `C:\Temp\Install & User Guide.pdf` because of the missing double quotes **left** to `filepath` and at end of the line. – Mofi May 01 '21 at 10:39
  • `filepath` is also a misleading variable name as in real is assigned the full qualified file **name** of passed file name and not the *path* of the file. The `"` are missing on third line on definition of `truepath` and on last line around `%truepath%` resulting in batch code **not** working only for `C:\Temp\Install & User Guide.pdf`, but also for many other file names. `truepath` is also a misleading variable name as in real the assigned string (should be) is a file URL. I think, my code is much better. It is longer because it handles also error conditions and is not a "good luck code". – Mofi May 01 '21 at 10:44
  • I'm sure that you hadn't try my code actually and view my code carefully. In fact, prefix is something that appear in Microsoft Edge when you open a pdf (so I call it prefix, and I think it's good when you check it after some time). And in third line I have replaced the '\' with '/' (which also works) and concat it with prefix as a valid filepath for microsoft edge to open (you can check it by yourselft). And for what you have mentioned, actually I tried and found no probelm. – ishin go May 02 '21 at 11:14
  • I advise you to try with yourself rather than justing believing your intuition. – ishin go May 02 '21 at 11:16
  • Please create a copy of any PDF file you have and rename the copy of the PDF file to `Install & User Guide.pdf`. Then open a [command prompt](https://www.howtogeek.com/235101/), enter the batch file name with full path (argument 0) and enter the name of the PDF file with `Install & User Guide.pdf` with full path and both file names enclosed in double quotes which is definitely required for the PDF file name. Then press key ENTER and look what happens. You will see multiple error messages output by the Windows command processor. – Mofi May 02 '21 at 16:04
  • I know very well what `file:///` as I am writing HTML files with a text editor since more than 20 years for usage online and offline. I have posted in my answer the URI to the Wikipedia article about [file URI scheme](https://en.wikipedia.org/wiki/File_URI_scheme). It is fact that your batch file with the five lines defines the environment variable `prefix` with the value `file:///`, but the remaining three lines do not reference the value of the environment variable `prefix`. `truepath` is defined with two times the full qualified file name where first part is with ``\`` and second with `/`. – Mofi May 02 '21 at 16:07
  • You gave me the advise to try your code, but you have never looked on what your posted code really does by removing `@echo off` or change it to `@echo on` or comment out this line with `rem` and run the batch file from within a command prompt window with a PDF file name as argument containing characters which require enclosing the file name in double quotes wherever used in the command prompt window and in the batch file what we batch file coding experts call [debugging a batch file](https://stackoverflow.com/a/42448601/3074564). – Mofi May 02 '21 at 16:10
  • Example: Your batch file with `@echo ON` as first line called with `"C:\Temp\Install & User Guide.pdf"` from within a command prompt window outputs: 1. `set prefix=file:///`, 2. `set filepath=C:\Temp\Install & User Guide.pdf`, 3. `'User' is not recognized as an internal or external command, operable program or batch file.`, 4. `set truepath=C:\Temp\Install C:/Temp/Install`, 5. `start shell:AppsFolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge C:\Temp\Install C:/Temp/Install`. That is definitely not the correct behavior for file `C:\Temp\Install & User Guide.pdf`. – Mofi May 02 '21 at 16:16
  • The batch file works __by chance__ and for a file like `C:\Temp\Windows_Commands.pdf` as this full qualified file name contains no space character, the file URI `file:///` is not really needed by Microsoft Edge, and spacing the file name twice, once with ``\`` as first argument for Microsoft Edge and separated by a space a second time with `/` as second argument, is no problem for MS Edge because of corrections by [Windows file management](https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file) and second file name references also the file opened already by first argument. – Mofi May 02 '21 at 16:22
  • BTW: A 100% valid file URL for `C:\Temp\Install & User Guide.pdf` would be `file:///C:/Temp/Install%20&%20Guide.pdf` as described by the [URL specification](https://url.spec.whatwg.org/) which is a world wide __standard__. So your code works by chance for some file names, but definitely not for all, and it works for those files working by chance only because of automatic corrections in background and the fact that the browsers are very tolerant against violations of the various web specifications as otherwise 99.50% of all web pages would not be displayed anymore by the browsers. – Mofi May 02 '21 at 16:35
  • I got what you mean, you're correct and thanks for your advise. But at least my code works for most situations hahaha which is enough for my persoanl use hahaha. – ishin go May 04 '21 at 01:21