Reputation: 323
I have a batch program that can be used for opening a specific file format I created. The file format is a actually a zip file with the .zip
changed to .gcif
.
When you run my btch file there is an input field for entering the location of the .gcif
file you want to open. The .gcif
is then renamed to .zip
. The 7z.exe (which is in the same directory as the batch file) then unzips the file and it is processed by my batch file.
But I would like it to also work when the user finds any .gcif
file, right clicks it, and selects Open With
my batch file. Is there any way for batch file to detect that ithas been tasked to open that specific file?
Upvotes: 0
Views: 182
Reputation: 323
I figured it out. When a file is opened with a batch file, the file location is passed on to the batch file as a parameter. The parameter can then be accessed using %1
.
For example, if I opened the file C:/document.txt
in a batch file containing this code:
@ECHO OFF
ECHO %1
The output would display C:/document.txt
.
Upvotes: 1
Reputation: 51683
You can register a windows file handler in your system.
You might need admin rights and might need to modify the registry.
Essentially you "connect" your filextension .gcif with your batch file so windows knows that any file of this extension is connected to your batchfile
You can read something about it on the msdn site here: How to Register a File Type for a New Application
If you plan to associate one or more file types with a new application, you must define a ProgID for each file type that you want to associate with the application.
To create a ProgID for each unique file type that your application handles, use these steps.
Instructions
Step 1: Note that some file types have multiple extensions that point to the same ProgID; for example:
HKEY_CLASSES_ROOT\App.jpeg (your ProgID)
HKEY_CLASSES_ROOT\.jpg = App.jpeg (the file type mappings)
HKEY_CLASSES_ROOT\.jpeg = App.jpeg
Step 2:
Remove the ProgID values when you install and uninstall your program.Step 3:
Leave the file type mappings unchanged at uninstall time. Doing so works because file type mappings are stored per user in HKEY_CLASSES_ROOT.ext, and the system identifies the case where the ProgID value is missing and ignores it. Leaving file type mappings unchanged avoids the need to have conditional code that only removes the file type mapping if the value still points to your ProgID. It is important to avoid doing so in cases where it might have been changed by another application and you thus cannot easily remove the value.Step 4:
Specify a unique value for the file type description of each file type ProgID by doing one of the following:Leave the default value of the ProgID empty, in which case the system uses the .ext file. Provide a localized value via FriendlyTypeName and, for compatibility with old applications that read the registry directly, be sure to provide the default value of the ProgID as the file type description (that is, use the same value that is referred to by the FriendlyTypeName in the English resource). Remarks If you plan to associate the file with an existing application, locate an application ProgID in the registry.
To accomplish something similar you can open your file once with Explorer and chose "Open with..." - locate your batch file and choose "always open with this application" checkbox.
The first option here is more for when you want to provide 1-click-open experience to customers for your application when they install it on their system.
Upvotes: 0