Export internal associations to registry?

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
Laurent
Junior Member
Junior Member
Posts: 30
Joined: 2007-01-17, 12:18 UTC
Contact:

Export internal associations to registry?

Post by *Laurent »

Over the years I've accumulated a lot of internal associations in Total Commander. Windows changes but those stay the same and always point to the right program.

However, now and then I need to open a file outside Total Commander and in that case the file is almost never associated correctly with the right application.

So what I'm wondering: is there any way to export all these internal association to the Windows registry, so that each file is correctly associated system-wide? I remember I've tried doing something like this in Autohotkey a few years ago but it got too complicated and I eventually gave up. I'm wondering if someone had a similar idea and made a working script? Otherwise, any suggestion on how to make the TC associations match the system ones?
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 50918
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Post by *ghisler(Author) »

You could try to extract them from wincmd.ini and create a registry script (.reg) to import them.

The minimum you need is the following (e.g. for text files);

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.txt]
@="txtfile"
[HKEY_CLASSES_ROOT\txtfile\shell\open\command]
@="c:\\windows\\System32\\notepad.exe %1"

Note the double backslashes in file system paths. Quotes also need to be escaped, e.g.
@="\"c:\\Program Files\\editor\\notepad.exe\" \"%1\""
Author of Total Commander
https://www.ghisler.com
Laurent
Junior Member
Junior Member
Posts: 30
Joined: 2007-01-17, 12:18 UTC
Contact:

Post by *Laurent »

Thanks for the info, I'll give that a try. I've also found my AHK script, which seems surprisingly complex, not sure what I was doing.

Anyway, I'll see where I can get with the .reg file.
Laurent
Junior Member
Junior Member
Posts: 30
Joined: 2007-01-17, 12:18 UTC
Contact:

Post by *Laurent »

Ok if someone's interested, I've ended updating and fixing my Autohotkey script.

Before running it, check that the path to wincmd.ini is valid and that the paths in replaceVariables() are correct. Then right-click the file and "Run as administrator".

The script handles system and totalcmd variables such as "%COMMANDER_PATH%" or "%wincmd%" and also assigns an icon if one's available.

Not sure if anyone beside me will use that, but if there's any problem let me know :)

Code: Select all

#SingleInstance force

; BEFDORE RUNNING:
;
; - Check that paths in `replaceVariables()` are correct
; - Check that path of wincmd.ini is correct

escapeString(s)
{
	output = %s%
	StringReplace, output, output, \, \\, All
	StringReplace, output, output, ", \", All
	
	return output
}

escapeArrayKey(s)
{
	output = %s%
	StringReplace, output, output, -, _, All
	StringReplace, output, output, ., _, All
	StringLower, output, output
	return output
}

replaceVariables(s)
{
	output = %s%
	StringReplace, output, output, `%COMMANDER_PATH`%, D:\Programmes\TotalCmd, All
	StringReplace, output, output, `%COMMANDER_DRIVE`%, D:, All
	StringReplace, output, output, `%windir`%, c:\windows, All
	return output
}

checkError(e, message)
{
	if (e = 1)
	{
		MsgBox Error: %message%
	}
}



iniPath = %A_ScriptDir%\..\wincmd.ini
filterIndex := 1
finalString =
doneExtensions = 


extensionDescriptions =

Loop, HKEY_CLASSES_ROOT,,1
{
	StringLeft, firstChar, A_LoopRegName, 1
	if (firstChar != ".")
	{
		continue	
	}
	
	RegRead, groupId, HKEY_CLASSES_ROOT, %A_LoopRegName%
	RegRead, groupName, HKEY_CLASSES_ROOT, %groupId%
	
	if (groupName = "")
	{
		continue	
	}
	
	StringMid, extension, A_LoopRegName, 2
	
	key := escapeArrayKey(extension)		
	extensionDescriptions%key% := groupName
}

	
Loop {
	IniRead, filterValue, %iniPath%, Associations, Filter%filterIndex%

	if (filterValue = "ERROR") {
		break	
	}

	IniRead, command, %iniPath%, Associations, Filter%filterIndex%_open
	IniRead, iconPath, %iniPath%, Associations, Filter%filterIndex%.icon

	if (iconPath = "ERROR") {
		iconPath =
	}
	
	StringLeft, firstChar, filterValue, 1
	
	extensions =
	groupId = 
	
	if (firstChar = ">") {
		StringMid, groupId, filterValue, 2
		IniRead, extensions, %iniPath%, searches, %groupId%_SearchFor
	} else {
		StringMid, groupId, filterValue, 3
		extensions = %filterValue%
	}
	
	filterIndex := filterIndex + 1
		
	StringReplace, groupId, groupId, %A_SPACE%, _, All
	StringReplace, groupId, groupId, ", _, All   ;"
	groupId = x_%groupId%_file

	
	Loop, Parse, extensions, %A_Space%
	{
		StringMid, extension, A_LoopField, 2
		IfInString, doneExtensions, %extension%
		{
			continue
		}
		
		doneExtensions = %doneExtensions% %extension%
		
		StringMid, extensionNoDot, extension, 2
		groupId = x_%extensionNoDot%_file
			
		key := escapeArrayKey(extensionNoDot)
		desc := extensionDescriptions%key%
		if (desc = "")
		{
			StringUpper, extensionUppercase, extensionNoDot
			desc = %extensionUppercase% file
		} else {
			desc = %desc%	
		}

		iconPath := replaceVariables(iconPath)
		command := replaceVariables(command)
		
		RegWrite, REG_SZ, HKEY_CLASSES_ROOT, %extension%,, %groupId%
		checkError(ErrorLevel, extension)
		RegWrite, REG_SZ, HKEY_CLASSES_ROOT, %groupId%,, %desc%		
		checkError(ErrorLevel, groupId)
		RegWrite, REG_SZ, HKEY_CLASSES_ROOT, %groupId%\DefaultIcon,, %iconPath%
		RegWrite, REG_SZ, HKEY_CLASSES_ROOT, %groupId%\shell\open\command,, %command%
		RegWrite, REG_SZ, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\%extension%\UserChoice, Progid, %groupId%
	}

}
Post Reply