Programmatically passing # in URL to a browser - firefox

I need to pass the character # as part of the URL to the browser and open it programatically. When I do the following:
google-chrome path_to_some_file.pdf#view=Fit
The opened page on the browser is path_to_some_file.pdf%23view=Fit, which is not the intended URL. When I manually delete %23 and type # in the address bar of the browser, then it works. How can I pass the character # to a browser programmatically?

You need to specify a fully qualified file:/// URL in order to include ? query or # hash strings.
More details:
The following approach works in IE10, Firefox 28, and Chrome 36.
If you are doing this from a batch file in Windows with any of those browsers, you can use backward slashes in the path as long as you prefix with file:///. E.g., file:///C:/blah/blah/file.pdf#etc is the proper URL, but if yours ends up formatted file:///C:\blah\blah\file.pdf#etc, that will work too.
Here is how you can get the full path using a batch file.
file:///%CD%/file.pdf#blah
will resolve to a file relative to the working directory (usually the batch file's directory unlike explicitly changed via command prompt or programmatically).
file:///%~dp0/file.pdf#blah
will resolve to a file relative to the batch file's directory. I usually go with this.
You can use ../ to navigate up relative to the batch file's directory. The resulting URL should still work fine.
If you are using something a bit more robust than batch files, you can translate all the \s into /s to create a proper URL.
E.g., in .NET, I think you can do new Uri("C:\blah\file.pdf"), and it'll give you a file URI (I think, not sure), which you can then grab and append the hash onto it.

Related

Bat file to open webpage with parameters to local file

Creating a bat file with:
start http://www.google.com/search?q=test
Does just what I would want it to do, it opens my default web browser (Chrome in my case) and browses to the URL http://www.google.com/search?q=test.
However, a bat file with:
start file:///C:/Users/d92495j/Desktop/OracleCDs/WebLogic/template.html?wbt=1
Only opens my default browser and browses to file:///C:/Users/d92495j/Desktop/OracleCDs/WebLogic/template.html
Note the lack of ?wbt=1. In order to fix this I've tried:
URL encoding the question mark
Running the start command parameters "window name" "file path in quotes"
Putting the file path in
variable and passing the variable to start
But none of those work. How can I get this to work?
I tested this and got the same result. I'm not really sure, but I guess this belongs to the question mark. The local file system of Windows can never have file names with ?, because this is a "wildcard" like *. I think it is possible that the file name is truncated there.
The following codes also doesn't work or produces only error messages:
start "file:///C:/Users/d92495j/Desktop/OracleCDs/WebLogic/template.html?wbt=1"
start file:///"C:/Users/d92495j/Desktop/OracleCDs/WebLogic/template.html?wbt=1"
The best solution I've come up with so far is:
powershell -noprofile -command "[void][System.Diagnostics.Process]::Start('chrome', 'file:///C:/Users/d92495j/Desktop/OracleCDs/WebLogic/template.html?wbt=1')"
This solution has the batch file launch PowerShell and then use the .NET System.Diagnostics.Process.Start method to launch Chrome with the correct parameter. The only downside is that it makes a browser choice for me and I'd prefer it to use my default browser, but I can live with that. I'll accept another answer (that is not much more complex) that uses the default browser.

Redirect default program to another program when a file opens in Windows OS

This is only under windows env.
As I know windows os identifies associated application of a particular file by file extension.
Like wise each file (binary) starting with corresponding symbols ("starting symbols"). For an example .JPG starts with ÿØÿà. Let say I open this .JPG file in a Hex editor or a Text editor and then I change that starting symbols into another file type. for an example I can change ÿØÿà to .Eߣ (.mkv). So when I double click on the .JPG the Windows Photo Viewer says there are some errors or similar message. So I need to get some information about the application that tries to open that kind of a file. If I can, I need to open that file using the application that associated with "starting symbols".
Briefly when I open .JPG I need to open a default video player .mkv files. But It may not work for this example. Because I changed only the "starting symbols" of my .JPG.
Please give me any idea to do this.
Thanks!
When you encrypt the file, give it a new extension. e.g. Picture.jpg becomes Picture.encrypted-jpg. You then register as the handler for encrypted-jpg, decrypt the file, then launch the normal jpg handler.
When the shell is asked to perform a verb on a file, the shell does not use the contents of the file to determine which app to pass it to. The file extension is what determines how the file will be treated.
You wish to use the contents of the file to influence which app processes a shell verb. In order to do so you would need to create a launcher app that reads the file header and then decides which app to pass the file on to. You would assign your launcher app as the handler app for all file extensions that you were interested in.
Although you could do this, it would be much easier just to set the file extension appropriately.
The proper way to do this sort of thing is to replace the files with reparse points.
The downside is that this involves writing a file system filter driver, i.e., an operating system extension, which is a whole level of trouble above and beyond ordinary application programming. (Since Windows already does file encryption, I doubt it would be worth the effort.)

Does Windows cache the contents of .url (Internet Shortcut) files?

I'm implementing a custom URL handler in .NET. To test this, I have created a few different .url files and put them on my Desktop. This generally works fine, but behaves oddly if I change the file's contents, specifically the URL= line. Doing so has no effect — the old URL continues to be opened. Renaming the file, however, works. The file looks like this:
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,0
[InternetShortcut]
URL=myCustomScheme://some/url/pointing/somewhere
IDList=
All shortcuts I create through New → Shortcut receive the same UUID, so changing that probably won't help.
Is there some internal .url file / URL mapping cache in Windows?
I have a reason to believe that URL files are interpreted by some Internet Explorer component at the time of creation (not biblical). They are only interpreted initially, the first time the file is created. Any modifications to the URL file later on will not be committed. This is because the shortcut is not stored in the file. This is why the file can be modified later on so that it becomes empty, as a 0 byte file and the URL file will appear to be working anyway. The shortcut data is stored in the "Web Document" field as a file property in the NTFS file system. The file merely serves the purpose of pointing to it. You might be able to modify these property fields programmatically, which would supposedly "edit the file". It's a painful exercise just to edit what appears to be a simple text file.
Additionally, once a file name has been used for a URL file, it cannot be reused for new files, no matter what disk or path you save it to. So you have to keep assigning unique file names, never used previously, for each new URL file you create. This has to do with how Internet Explorer caches web content. It remembers what file names have been used already and maps those names to previously defined URL addresses. To reuse a name (or when you run out of ideas for new and unique file names) you have to clear Temporary Internet Files.
Windows 7: %localappdata%\Microsoft\Windows\Temporary Internet Files
Windows 8: %localappdata%\Microsoft\Windows\INetCache
To directly answer your question: yes, it does.
Windows 7 Caches your Filenames and sometimes, as you said, the filenames in specific locations.
Start regedit and search for the following
HKEY_USERS\S-1-5-21-x-x-x-x\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\MuiCache
where S-1-5-21-x-x-x-x is your currently logged in User.
There you can see that most (or all?) files have been cached which you have ever accessed.
Maybe you can also deactivate the MUICaching programatically. Maybe this site helps you: Disable Caching
I had a similar issue, and it turns out the culprit was Firefox.
If by chance your web browser is Firefox, your cache directories may be corrupted.
You can either create a new Profile, or take your chances cleaning things out of C:\Users\<username>\AppData\Local\Mozilla\Firefox\Profiles\<profile>\
I've found my desktop .url shortcut contents cached in %LocalAppData%\Microsoft\Windows\Caches.
There are a few (several?) files with filenames like {<SOME_GUID>}.<x>.ver0x<XXXXXXXXXXXXXXXX>.db. Two of them had the .url files cached.
Unfortunately, I've found no information on what they are or how to refresh them. Everybody just deletes them as part of some cache clean-up operation.

How do I make Firefox open a file with an ampersand in the filename?

I've written some Ruby code to inspect ZIP-files as part of an internal company process. The way we usually launch this code is from a web browser. When you click to download the file, you select "open with" and specify the full path to a small batch file. This one-line batch file looks like this:
\\mathworks\public\Matthew_Simoneau\ruby-1.8.7-p72-i386-mswin32\bin\ruby.exe "%~dp0inspect.rb" %1
As far as I know, this technique is the only easy way to launch my Ruby code on a Windows machine which doesn't have Ruby installed, but does have access to the company internal filesystem.
I'm having a problem when the filename of the ZIP file contains an ampersand.
This works fine on IE and Chrome, where the above line gets "expanded" out to the following:
C:\WINNT\Profiles\matthew\Desktop>\\mathworks\public\Matthew_Simoneau\ruby-1.8.7-p72-i386-mswin32\bin\ruby.exe "\\mathworks\public\Matthew_Simoneau\sandbox\inspect\inspect.rb" "C:\WINNT\Profiles\matthew\Local Settings\Temporary Internet Files\Content.IE5\VNATJ3X0\park&park_paper_LMI_neuralN[1].zip"
On Firefox, however, everything after the ampersand in the filename gets dropped on the floor:
H:\>\\mathworks\public\Matthew_Simoneau\ruby-1.8.7-p72-i386-mswin32\bin\ruby.exe "\\mathworks\public\Matthew_Simoneau\sandbox\inspect\inspect.rb" C:\Temp\park
I've tried putting the %1 in quotes in the batch file, but that has no effect.
I suspect this is a bug in Firefox. I've searched the Firefox bug list, but didn't find anything.
Am I doing something wrong here? Is this a Firefox bug? If so, is there a way I can work around it? Is there a more robust way to launch my Ruby code from a web browser?
Update: I filed a bug report with Bugzilla#Mozilla, but there hasn't been a response yet.
Since you apparently have no control over the zip filenames, you could do the following in the Ruby code...
If the filename passed does not exist, look in the same folder for any file with the base passed filename followed by "&*.zip".
This will work for "park&park.zip" as long as there isn't also a zip file already in the folder named, say, "park&foo.zip".
If there is a real potential for filename collision (i.e., the zips aren't being cleaned off the user's machine and ampersands are common), the only other solution might be using a download manager plug-in in Firefox that has filename re-writing capability to fix any ampersands on the way down.
I've just tested something similar with Firefox 3.5.2 on Linux and it works well (i.e. shell script gets the correct path).
You should first try to use a script like that:
echo %1 > c:\temp\test.txt
(hope that's the right syntax, I didn't use cmd for a long time)
…and see if you get right path in that file. If the ampersand is stripped in that file too, and you're using the latest version of Firefox, that means you found bug specific to Windows platform and you should report it.
Use %24 for the & character. See the URL Encoding Table for other such problematic characters: http://webdesign.about.com/library/bl_url_encoding_table.htm
Encode your ampersand for best results...
&
As in
C:\WINNT\Profiles\matthew\Desktop>\\mathworks\public\Matthew_Simoneau\ruby-1.8.7-p72-i386-mswin32\bin\ruby.exe "\\mathworks\public\Matthew_Simoneau\sandbox\inspect\inspect.rb" "C:\WINNT\Profiles\matthew\Local Settings\Temporary Internet Files\Content.IE5\VNATJ3X0\park&park_paper_LMI_neuralN[1].zip"

how to let ruby call default browser to open localfile

In this question, I find that using system('start http://www.google.com') is OK. If the file is in local disk, though, using system('start file:///c:/temp/a.html') doesn't work. How do I have Ruby get the default browser to open a local file?
What do you get when you double click a .html file in Windows Explorer? If it isn't the browser then that is your problem. The 'start' keyword pushes the path through the ShellExecute function, for http:// URLs is knows to open that in a browser, if it is a file it depends on the extension of the file, if your system has .html pointing to notepad for example (because in the past you have set it to notepad) it is always going to open it in that program unless you go and specifically change it.
From a generic work around point of view there is not much you can do, if you can access the Windows registry under Ruby then you can query the HKEY_CLASSES_ROOT\http\shell\open\command default value which contains a command line for the current browser bound to the HTTP protocol, you could use that to construct a full path (replace the %1 with the URL string).

Resources