How do I remove previous extension<->Program affiliations programatically? - windows

I would like to write a batch file which ensures that when a user clicks on a .JNLP file, it opens using javaws.exe (located in program files/java/... you know the drill)
I have written the following batch file:
ASSOC .jnlp=JNLPFILE
IF EXIST "%ProgramFiles% (x86)" (GOTO x86) ELSE (GOTO x64)
:x86
FTYPE JNLPFILE="%ProgramFiles% (x86)\Java\jre7\bin\javaws.exe" "%1"
goto:eof
:x64
FTYPE JNLPFILE="%ProgramFiles%\Java\jre7\bin\javaws.exe" "%1"
I am testing this all in windows 7 64-bit.
This... doesn't work. Well, that's not entirely accurate. It modifies the registry correctly, and it adds .jnlp to windows list of Recommended programs to run. It does exactly what it should.
But it doesn't solve my problem. See, For testing, I went to Default Programs and associated .jnlp files with Notepad. And when I try to open .Jnlp files, IT tries to open in notepad, even after my code has run.
If I do an open with on a .jnlp, it gives me the option to open with Notepad or javaws.exe If I've run my code with the ASSOC, it adds a SECOND option of javaws.exe
I've tried ASSOC .jnlp="" and FTYPE JNLPFILE="" To try and clear notepad out, but had no luck.
How do I make my batch file blow away prior settings and assert its dominance on the machine?
EDIT: Using the answers below, I have added a single command to the beginning of my batch file, which should take care of my problem and make things work correctly.
REG DELETE HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.jnlp /f

Windows Explorer keeps its own list of file extensions for the user.
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts
To remove programs from this list, delete the program entry from
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.‌​jnlp\OpenWithList
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.jnlp\OpenWithProgIDs
and set the desired UserChoice Progid in
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.jnlp\UserChoice
Also, note that the OpenWithList and OpenWithProgids can be set at multiple levels.
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.xyz\OpenWithList
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.xyz\OpenWithProgIDs
HKCR\.xyz\OpenWithList
HKCR\.xyz\OpenWithProgIDs
HKCR\SystemFileAssociations\FileType\OpenWithList

Related

Windows CMD Start and wait for the default application in a batch file

I am trying to start the default application for a file, wait for it to complete, and then continue with my batch file. The problem is that start, when used below simply creates another command prompt window with the example.doc in the title bar. I can use call instead of start, but then call does not wait for the program to finish before going to the next line. It appears that start needs to have an executable name and will not work with the default application system in windows.
Any ideas how I can make this happen without having to hardcode the windows application in the batch file as well?
set filename=example.doc
start /wait %filename%
copy %filename% %filename%.bak
How do I start the default application for a file, wait for completion, then continue?
It appears that start needs to have an executable name and will not work with the default application system in windows.
start, when used below simply creates another command prompt window with the example.doc in the title bar
start /wait %filename%
The above command won't work because %filename% is used as the window title instead of a command to run.
Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes ""
According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.
Source start
Try the following command instead:
start "" /wait %filename%
Alternative solution using the default open command
Any ideas how I can make this happen without having to hardcode the
windows application in the batch file as well?
One way is to use assoc and ftype to get the default open command used for the file then execute that command.
The following batch file does that for you (so no hard coding of windows applications is needed).
Open.cmd:
#echo off
setlocal enabledelayedexpansion
set _file=example.doc
rem get the extension
for %%a in (%_file%) do (
set _ext=%%~xa
)
rem get the filetype associated with the extension
for /f "usebackq tokens=2 delims==" %%b in (`assoc %_ext%`) do (
set _assoc=%%b
)
rem get the open command used for files of type filetype
for /f "usebackq tokens=2 delims==" %%c in (`ftype %_assoc%`) do (
set _command=%%c
rem replace %1 in the open command with the filename
set _command=!_command:%%1=%_file%!
)
rem run the command and wait for it to finish.
start "" /wait %_command%
copy %_file% %_file%.bak 1>nul
endlocal
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
assoc - Display or change the association between a file extension and a fileType
enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
for - Conditionally perform a command several times.
for /f - Loop command against the results of another command.
ftype - Display or change the link between a FileType and an executable program.
start - Start a program, command or batch script (opens in a new window).
variable edit/replace - Edit and replace the characters assigned to a string variable.
Simply use the filename directly as command, unless that filename is a batch file, in which case use call.
In a batch file invocation of a GUI subsystem executable is blocking, unlike for an interactive command.
Use the start command when you don't want blocking execution.
There is a subtle point about “default application”, namely that a file type can have a registered default application for the graphical shell, e.g. its “Open with…”, without having an assoc/ftype association, or different from that association.
I'm not entirely sure of which registry entries are used for this. I've always had to look it up and research it each time. As I recall it's not well-documented.
But hopefully you're OK with just the assoc/ftype scheme.
A further subtle point about “default application”: on the laptop I'm writing this on the ftype association for text files is to open them in Notepad:
[H:\forums\so]
> assoc .txt
.txt=txtfile
[H:\forums\so]
> ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
[H:\forums\so]
> _
And this is what the graphical shell (Windows Explorer) will do.
But cmd.exe looks inside files, and if it finds an executable signature then it tries to run the text file as an executable, even in Windows 10:
[H:\forums\so]
> echo MZ bah! >oops.txt
[H:\forums\so]
> oops.txt
This version of H:\forums\so\oops.txt is not compatible with the version of Windows you're running. Check your computer's system information and then contact the software publisher.
[H:\forums\so]
> _

Using /c as an argument in CygWin bash script, without translation

I'm trying to run regedit /c reg.txt from within a CygWin bash script to grab the registry contents after which I'll process them with the remainder of the script (yes, I know Powershell is one option but I'd rather stick with the tools I know).
However, when doing this, the regedit program pops up a dialog box complaining about trying to read c:\ and I think this is because CygWin magically converts the /c into the actual drive c:\, similar to the way that cat /h/xyzzy/plugh.txt will show me the file h:\xyzzy\plugh.txt. The dialog box is as follows, you can see the errant parameter underlined:
Is there a way I can stop this translation from happening so that regedit gets the parameters as intended? I've thought of running the regedit separately in a .cmd file but it's a real pain having to maintain two separate files to do a single job (a).
I've also tried running it with cmd /c but that appears to suffer from a similar problem - it just opens up a new command interpreter and waits for input, presumably because the /c is being translated to c:\. While cmd /k dir will list the directory and remain, cmd /c dir does nothing in the cmd.exe instance.
(a) It will work if I use cmd /k otherscript.cmd and then explicitly exit at the end of that script. However, as I said, it's a bit kludgy and I'm pretty certain it would break the second I mount a network share on the k: drive and CygWin starts translating /k into k:\ :-)
As per this MSYS page (upon which MinGW is based, upon which your Git for Windows is based), MSYS will automagically convert POSIX paths to Windows paths for executables not linked to the MSYS DLL.
This includes translating /c into c:\, which is what you're seeing. This explains why both regedit /c reg.txt and cmd /c dir aren't working since the former is attempting to use c:\ as an input to regedit (prompting the warning you see) and the latter is simply starting cmd without executing the arguments.
The method used to prevent translation is to double up the slash, with something like:
regedit //c reg.txt
cmd //c dir
MSYS will remove the leading / and refuse to translate the remainder of the argument, so you'll end up with the /c argument.
You'll still have issues with the first command above since the correct command-line switch to export the registry is /e rather than /c. Change that (and use //e from within MinGW, of course) and it will do what you want.

Batch file to start all programmes in the start folder of XP

I need start all folders in a the Windows "Start/Programs/Startup folder" of an XP machine, explorer is disabled to stop top people playing and remove the Start and Task-bar.
I can run a batch file at start-up but how do I write the batch to run ALL programs in the "Start/Programs/Startup folder" the programs in the folder may change but the batch needs to remain the same
I am able to open each file individually using the below code but I really need to be able to open everything in the folder to avoid problems in the future
start "" /b "C:\Documents and Settings\User\Start Menu\Programs\Startup\PROG.appref-ms"
I have tried the code below, that batch starts but nothing starts
%DIR%=C:\Documents and Settings\Pete\Start Menu\Programs\Startup
for %%a in (%DIR%\*) do "%%a"
Running the batch from the desktop also doesn't run the programs in the start folder, the DIR address is taken from windows explorer when I navigated to the folder with the short cuts in
This is an interesting request--one that I would question the motive behind, but since you asked, here's a way you could accomplish it:
#echo off
set DIR=C:\Your\Directory
for %%a in ("%DIR%\*") do "%%a"

Running batch file with /d option

I've been working for some time on the installer for my application (using Installshield), and some time ago I came upon the problem, when batch file, that I called using LaunchApplication failed to execute (to be specific - it executed, but in the wrong directory). I decided to dig this issue up and stumbled upon this article. The problem, it turns out, lies within Autorun registry key, which is defined in following matter:
cd /d C:\Blahblah\Yadayada
So, before the batch file was actually executed, this command changed directory.
The batch file is most basic one, something like this:
:start
foo.exe %1 --bar %2 --baz %3
if errorlevel 1 goto fail
ECHO Success
goto end
fail:
ECHO Fail
:end
So, basically this batch file expects that it will be launched from the correct directory, and it's not. I'm append INSTALDIR variable to the batchname in LaunchApplication call, just to be clear, and it works perfectly fine when Autorun key is not set up.
And, well, I finally got to the question - is there any way to provide launch options for individual batch file? I know that providing /d option will render Autorun useless, but it only works only on direct call.
For instance, let's assume that I have batch file with simple 'dir' command (let's call it foo.bat), and my Autorun key is defined as shown above. I run command-prompt with /d option (CMD /d), and then run 'dir' directly I'll get content of the folder I'm currently in (e.g. user folder); BUT, if I launch foo.bat, I'll get contents of C:\Blahblah\Yadayada, because Autorun command will execute first, set default folder, and only after that 'dir' command will be called.
So, personally I see a few options here. First - removing Autorun key, and that would be the most fitting and easiest solution if it had to be applied only for one machine - I can't possibly expect every user to take care of their Autorun key for themselves. Second, which should be applicable for everyone (but which I hadn't tested yet), would be providing path to installation folder as extra parameter to batch file, and then changing directory to that:
:start
%4
cd %5
foo.exe %1 --bar %2 --baz %3
...
Where %4 would be a disk letter and %5 would be a path. This seems to be a solution, but I find it counterproductive that I have to implicitly change path to the folder, given that it works perfectly well when Autorun key is absent.
So, I was wondering if there's any workaround about this Autorun key problem. Maybe, like I've mentioned in the title, kind of /d option for batch file so that when it runs it will override global option from Autorun and will actually launch from the place it's supposed to launch, or some kind of technique like that? Also, maybe there's some kind of option in LaunchApplication() function I'm not aware of?

shell:Common Startup as a parameter to XCOPY

I have a simple batch script that copies a file to the startup folder, but it appears that I can't use shell:Common Startup as a parameter to xcopy. I have tried this
xcopy hurrdurr.exe "shell:Common Startup"
and many other variations, and they don't work. As an aside, if this did work, "hurrdurr.exe" would run on every startup right, assuming I got clearance via uac to do the xcopy operation? Would using a environment variable be better? The os in question is Windows XP and proceeding.
I'm not sure why your shell command won't work, but if you need to get your program to load on startup then I would much prefer using the registry, it's cleaner and simpler, and it means you don't have to copy the file somewhere else, especially if that file is dependant on other things.
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v "Hurrdurr" /d "hurrdurr.exe" /f
Just run cmd as admin and it will work :)
If you do want to use the startup folder though, on Win7 you can use
"%appdata%\Microsoft\Windows\Start Menu\Programs\Startup"
I would also use a shortcut as #David suggested instead of copying the actual file.

Resources