TC key.cmd (Total Commander license key tool) v1.0b2

Discuss and announce Total Commander plugins, addons and other useful tools here, both their usage and their development.

Moderators: white, Hacker, petermad, Stefan2

User avatar
white
Power Member
Power Member
Posts: 4615
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

TC key.cmd (Total Commander license key tool) v1.0b2

Post by *white »

TC key.cmd is a tool to move a Total Commander license to and from the Windows registry.

A license for Total Commander is stored in a file called WINCMD.KEY. Since Total Commander version 7.55, the license key can also be stored in the Windows registry. However, there was no tool (that I knew of) to easily move the license to and from the registry. So I made one myself.

I have only tested this tool on own system with Windows 11 64-bit. So it needs further testing. If you own a license, I would greatly appreciate it if you test the tool on your system and report your findings here.

The tool probably works on Windows Vista and later, except for the handling of TCMDKEY.ZIP files. To unpack TCMDKEY.ZIP files, the batch file uses the Expand-Archive cmdlet available in Powershell 5 and later. PowerShell 5 is installed by default since Windows 10 and can be installed on Windows 7 and 8, see Windows PowerShell System Requirements.

Features:
  • Import a license key into the registry.
  • Export a license key in the registry to a WINCMD.KEY file.
  • Delete a license key in the registry.
  • Support for all 3 registry locations (current user, all users 64-bit and all users 32-bit).
  • It searches for licenses in registry, WINCMD.KEY files and TCMDKEY.ZIP files at known locations.
  • It also searches for a license in the current folder and in the folder where the tool is installed.
  • It searches for WINCMD.INI files at known locations and checks if a location for the license file is defined within.
  • It tries to find out if different names are used for the WINCMD.INI file. It does so by analyzing the Total Commander registry settings and by analyzing the COMMANDER_INI environment variable. It searches for all the found ini filenames at all locations.
  • It determines where Total Commander is installed by analyzing the Total Commander registry settings and the Total Commander environment variables.
  • It shows all found licenses and shows which ones are the same.
  • If more than 1 unique license is found, you can choose which one to import or export.
  • Exported licenses are stored in subfolders of the folder where this tool is installed. Existing WINCMD.KEY files will not be overwritten.
Tip: If you create a button for the tool on the Total Commander toolbar, clear the "Start Path" field. That way, the tool will be started in the current folder (except when run as administrator) and a license in the current folder will be detected.

Tip 2: This tool can also be used to show where your installed license is and to quickly scan a computer for a license.


The tool is listed below. Save the contents to the mentioned filename.

Filename:

Code: Select all

TC key.cmd
Contents:

Code: Select all

@Echo off
Rem
Rem  Total Commander license key tool v1.0b2
Rem
Rem  Created by: Forum user white on the Total Commander forum (https://www.ghisler.ch/board/)
Rem
Rem  Tool to import/export license keys into/from the Windows registry.
Rem  To change registry settings for all users, this tool needs to be run as administrator.
Rem
Rem  This tool searches for license files in a number of locations. If your license file is not found, 
Rem  place your license file and this tool in the same folder, or run this tool from the folder that
Rem  contains your license file.
Rem
Rem  Exported licenses are saved in subfolders of this tool's folder.
Rem
Rem  This tool creates temporary files in the Windows temp folder. One of the temporary files contains
Rem  the found license keys. Close the tool by pressing Q, or the temporary files will not be deleted.
Rem
Rem
Rem  To prevent errors the following programming style is used:
Rem  * No real parameters are used with Call statements, because special characters are handled poorly.
Rem  * Double quotes are not stored in environment variables, or removed as soon as possible.
Rem
Rem  The history of changes is included at the bottom of this file.
Rem
:Main_START

	SetLocal EnableExtensions DisableDelayedExpansion

	Rem  Clear screen and write header
	Cls
	Echo.
	Echo -=[ Total Commander license key tool v1.0b2 ]=-
	Echo.
	Echo  Licenses:
	Echo.

	Rem  Define global variables
	Set "p_tmp=%tmp%\$$$tmp_%~n0$$$"
	Set "G_f_ini_files=%p_tmp%\Ini files.txt"
	Set "G_f_license_folders=%p_tmp%\License folders.txt"
	Set "G_f_license_keys=%p_tmp%\License keys.txt"
	Set "G_f_key_hex=%p_tmp%\tmp_tcmdkey.txt"
	Set "G_f_processed_folders=%p_tmp%\Processed folders.txt"
	Set "G_key_cnt=0"
	Set "G_err_cnt_unp=0"
	Set "G_p_export=%~dp0Export"
	Set "G_p_unzip=%p_tmp%\Unzip key"

	Rem  Set up temp folder
	If not exist "%p_tmp%" (md "%p_tmp%") else Del /q "%p_tmp%"
	Echo "::WINCMD.INI::"> "%G_f_ini_files%"
	If defined COMMANDER_INI For %%I in ("%COMMANDER_INI%") do (
		If /i not "%%~nxI"=="WINCMD.INI" Echo "::%%~nxI::">> "%G_f_ini_files%"
	)
	Rem  If you know different names that were used for wincmd.ini files, add them here
	Rem  Echo "::TC.INI::">> "%G_f_ini_files%"


	Rem  Process name and location of wincmd.ini file defined in registry
	Rem  Store unique filenames of ini files in a temporary file

	Rem  Process HKCU key
	Set "param_1=HKCU\Software\Ghisler\Total Commander"
	Set "param_2="
	Call :ProcessRegistry
	Set "ini_folder_1=%result%"

	Rem  Process HKLM key, 64bit
	Set "ini_folder_2="
	Call :OS_64bit || Goto PROCESS_REG_32BIT
	Set "param_1=HKLM\Software\Ghisler\Total Commander"
	Set "param_2=/reg:64"
	Call :ProcessRegistry
	Set "ini_folder_2=%result%"

:PROCESS_REG_32BIT
	Rem  Process HKLM key, 32bit
	Set "param_1=HKLM\Software\Ghisler\Total Commander"
	Set "param_2=/reg:32"
	Call :ProcessRegistry
	Set "ini_folder_3=%result%"


	Rem  Process folders where ini files and license file could be located
	Rem  If an ini file is found, check if it contains a location for the license file
	Rem  Store found licenses in a temporary file
	
	Rem  Process this tool's folder, export folders and current folder
	Set "param_1=%~dp0" & Call :ProcessFolder
	For /d %%i in ("%G_p_export%*") do Set "param_1=%%~i" & Call :ProcessFolder
	Set "param_1=%CD%" & Call :ProcessFolder
	
	Rem  Process folders of ini files found in registry
	Set "param_1=%ini_folder_1%" & Call :ProcessFolder
	Set "param_1=%ini_folder_2%" & Call :ProcessFolder
	Set "param_1=%ini_folder_3%" & Call :ProcessFolder

	Rem  Process install folder listed in registry HKCU
	Set "param_1=HKCU\Software\Ghisler\Total Commander"
	Set "param_2=InstallDir"
	Set "param_3="
	Call :RegRead
	Set "param_1=%result%" & Call :ProcessFolder & Set "result=%result%"    & Rem Restore value of result after the call
	Rem  Also process folder in the VirtualStore folder
	If defined result If defined LOCALAPPDATA For %%I in (
		"%result%\dummy"
	) do Set "param_1=%LOCALAPPDATA%\VirtualStore%%~pI" & Call :ProcessFolder

	Rem  Process install folder listed in registry HKLM, 64-bit
	Call :OS_64bit || Goto PROCESS_INSTALL_DIR_32BIT
	Set "param_1=HKLM\Software\Ghisler\Total Commander"
	Set "param_2=InstallDir"
	Set "param_3=/reg:64"
	Call :RegRead
	Set "param_1=%result%" & Call :ProcessFolder & Set "result=%result%"    & Rem Restore value of result after the call
	Rem  Also process folder in the VirtualStore folder
	If defined result If defined LOCALAPPDATA For %%I in (
		"%result%\dummy"
	) do Set "param_1=%LOCALAPPDATA%\VirtualStore%%~pI" & Call :ProcessFolder

:PROCESS_INSTALL_DIR_32BIT
	Rem  Process install folder listed in registry HKLM, 32-bit
	Set "param_1=HKLM\Software\Ghisler\Total Commander"
	Set "param_2=InstallDir"
	Set "param_3=/reg:32"
	Call :RegRead
	Set "param_1=%result%" & Call :ProcessFolder & Set "result=%result%"    & Rem Restore value of result after the call
	Rem  Also process folder in the VirtualStore folder
	If defined result If defined LOCALAPPDATA For %%I in (
		"%result%\dummy"
	) do Set "param_1=%LOCALAPPDATA%\VirtualStore%%~pI" & Call :ProcessFolder

	Rem  Process other known locations for ini files and key files
	If defined COMMANDER_INI For %%I in ("%COMMANDER_INI%") do Set "param_1=%%~dpI" & Call :ProcessFolder
	For %%i in (
		"%APPDATA%\Ghisler"
		"%COMMANDER_PATH%"
		"%USERPROFILE%"
		"c:\windows"
		"%SystemRoot%"
		"c:\totalcmd"
		"c:\Total Commander"
		"c:\Program Files\totalcmd"
		"c:\Program Files\Total Commander"
		"c:\Program Files (x86)\totalcmd"
		"c:\Program Files (x86)\Total Commander"
	) do Set "param_1=%%~i" & Call :ProcessFolder
	
	Rem  Also process folders in the VirtualStore folder
	If not defined LOCALAPPDATA Goto Main_SEARCH_REG_FOR_KEYS
	If defined COMMANDER_EXE For %%I in ("%COMMANDER_EXE%") do Set "param_1=%LOCALAPPDATA%\VirtualStore%%~pI" & Call :ProcessFolder
	For %%i in (
		"totalcmd"
		"Total Commander"
		"Program Files\totalcmd"
		"Program Files\Total Commander"
		"Program Files (x86)\totalcmd"
		"Program Files (x86)\Total Commander"
	) do Set "param_1=%LOCALAPPDATA%\VirtualStore\%%~i" & Call :ProcessFolder


:Main_SEARCH_REG_FOR_KEYS
	Rem  Search registry for license keys

	Rem  Search HKCU key
	Set "param_1=HKCU\Software\Ghisler\Total Commander"
	Set "param_2="
	Set "param_3=Registry (current user)"
	Call :RegSearchLicense

	Rem  Search HKLM key, 64bit
	Call :OS_64bit || Goto Main_SEARCH_HKLM_32BIT
	Set "param_1=HKLM\Software\Ghisler\Total Commander"
	Set "param_2=/reg:64"
	Set "param_3=Registry (all users, 64-bit)"
	Call :RegSearchLicense

:Main_SEARCH_HKLM_32BIT
	Rem  Search HKLM key, 32bit
	Set "param_1=HKLM\Software\Ghisler\Total Commander"
	Set "param_2=/reg:32"
	Set "param_3=Registry (all users, 32-bit)"
	Call :RegSearchLicense


	Rem  Display footer
	
	Echo.
	If not "%G_err_cnt_unp%"=="0" Echo  Unable to unpack the tcmdkey.zip file(s)  (requires PowerShell 5)
	If "%G_key_cnt%"=="0" (
		Echo  No license found! Place your license file and this tool in the same folder,
		Echo  or run this tool from the folder that contains your license file.
	) else (
		Echo  Number of unique licenses: %G_key_cnt%
	)


	Rem  Display menu and execute options
	
	Rem  Display menu
	Echo.
	Echo Choose one of these options:
	Echo.
	Echo 1) Import license into registry (current user)
	Echo 2) Import license into registry (all users, 64-bit)  (Run as administrator)
	Echo 3) Import license into registry (all users, 32-bit)  (Run as administrator)
	Echo 4) Export license
	Echo 5) Delete license from registry (current user)
	Echo 6) Delete license from registry (all users, 64-bit)  (Run as administrator)
	Echo 7) Delete license from registry (all users, 32-bit)  (Run as administrator)
	Echo R) Refresh
	Echo Q) Quit and remove temporary files

	Rem  Execute options chosen by user
	Echo.
	Choice /c 1234567RQ /n /m ">"
	If not errorlevel 1 Goto Main_END
	If errorlevel 10 Goto Main_END
	If errorlevel 9 Set "quit=true" & Goto Main_END
	If errorlevel 8 Goto Main_END
	If errorlevel 7 Call :RegDeleteHKLM32 & Goto Main_END
	If errorlevel 6 Call :RegDeleteHKLM64 & Goto Main_END
	If errorlevel 5 Call :RegDeleteHKCU & Goto Main_END
	If errorlevel 4 Call :ExportLicense & Goto Main_END
	If errorlevel 3 Call :RegImportHKLM32 & Goto Main_END
	If errorlevel 2 Call :RegImportHKLM64 & Goto Main_END
	If errorlevel 1 Call :RegImportHKCU & Goto Main_END
	
:Main_END
	Rem  Clean up temp folder
	If exist "%p_tmp%" rd /s /q "%p_tmp%"
	
	Rem  Quit
	If defined quit Exit /b

	Rem  Restart
 	Endlocal
	Goto Main_START


Rem
Rem  Warning and message functions
Rem
:WrnRegDelete
	Echo Before removing a license from the registry, make sure you have a copy of the license somewhere.
	Exit /b
:MsgNotAvailableOn32bit
	Echo This function is not available on a 32-bit operating system.
	Exit /b


Rem
Rem  Function ExpandVars
Rem
Rem  Parameters: String
Rem  Return value: ExpandedString
Rem  
Rem  Expand environment variables in a string.
Rem  There must be no double quotes in the string.
Rem
:ExpandVars
	For /f "tokens=*" %%i in ('Echo "%param_1%"') do Set "result=%%~i"
	Exit /b


Rem
Rem  Function ExportLicense
Rem
Rem  Export a license key to a WINCMD.KEY file. 
Rem  An existing WINCMD.KEY file will not be overwritten. Create a new export folder instead.
Rem
:ExportLicense
	Rem  Get hex encoded license key to export
	Call :GetKey
	If not defined result Exit /b
	Rem  Determine export folder
	Set "ExportLicense_p_export=%G_p_export%"
	Set "ExportLicense_cnt=0"
:ExportLicense_LOOP
	If not exist "%ExportLicense_p_export%\wincmd.key" Goto ExportLicense_EXPORT
	Set /a "ExportLicense_cnt=%ExportLicense_cnt% + 1"
	Set "ExportLicense_p_export=%G_p_export% (%ExportLicense_cnt%)"
	Goto ExportLicense_LOOP
:ExportLicense_EXPORT
	Rem  Export license key to file
	If not exist "%ExportLicense_p_export%" md "%ExportLicense_p_export%"
	Rem  Write hex encoded key to a temporary file
	Echo %result%> "%G_f_key_hex%"
	Rem  Convert hex encoded file to binary file
	>nul 2>&1 Certutil -decodehex "%G_f_key_hex%" "%ExportLicense_p_export%\wincmd.key"
	Rem  Clean up temp file
	If exist "%G_f_key_hex%" Del /q "%G_f_key_hex%"
	Rem  Report result to user
	If exist "%ExportLicense_p_export%\wincmd.key" (
		Echo License exported to: "%ExportLicense_p_export%\wincmd.key"
	) else (
		Echo Export failed!
	)
	Pause
	Set "ExportLicense_p_export="
	Set "ExportLicense_cnt="
	Exit /b


Rem
Rem  Function GetKey
Rem
Rem  Return value: LicenseKey
Rem  
Rem  Get license key from temporary file. If there are more than one license, ask the user which one to use.
Rem
:GetKey
	Set "result="
	Rem  Determine which license to get
	Set "GetKey_key_idx="
	If "%G_key_cnt%"=="0" Goto GetKey_END
	If "%G_key_cnt%"=="1" (Set "GetKey_key_idx=1") else Set /p GetKey_key_idx="Enter a license number shown on screen: "
	If defined GetKey_key_idx Set "GetKey_key_idx=%GetKey_key_idx:"=%"    & Rem Remove quotes
	Rem  Get the license key from file, use Find so it also works when batch file is started in Unicode command prompt (cmd.exe /u)
	If exist "%G_f_license_keys%" For /f "delims=[] tokens=2" %%i in (
		'Find "[%GetKey_key_idx%]" ^<"%G_f_license_keys%"'
	) do Set "result=%%i"
:GetKey_END
	If not defined result (
		Echo License "%GetKey_key_idx%" not found!
		Pause
	)
	Set "GetKey_key_idx="
	Exit /b


Rem
Rem  Function IniRead
Rem
Rem  Parameters: FileName SettingName
Rem  Return value: Value
Rem  
Rem  Read value of a setting from an ini file.
Rem  Quick and dirty implementation. Section headers are ignored.
Rem  Double quotes in the value will be removed.
Rem
:IniRead
	Set "result="
	If not defined param_1 Exit /b
	If not defined param_2 Exit /b
	If not exist "%param_1%" Exit /b
	Rem  Use the Find command first, because Find can handle files containing null characters and files in UTF16 LE format
	For /f "tokens=1* delims==" %%i in (
		'2^>nul Find /i "%param_2%" "%param_1%" ^| Findstr /i /r "^[ 	]*%param_2%[ 	]*="'
	) do Set "result=%%j"
	If not defined result Exit /b
	Rem  Trim and remove quotes, but keep spaces inside quotes
	Set "result=%result:"=[double quote]%"                           & Rem Prevent errors by replacing quotes with markers
	Set "param_1=%result%"
	Call :Trim
	If defined result Set "result=%result:[double quote]=%"          & Rem Remove markers
	Exit /b


Rem
Rem  Function OS_64bit
Rem
Rem  Exitcode:
Rem  	0 (OS is 64-bit)
Rem  	1 (OS is 32-bit)
Rem  
Rem  Determine if Operating System is 64-bit or 32-bit.
Rem
:OS_64bit
	If "%processor_architecture%"=="x86" If not defined processor_architew6432 Exit /b 1
	Exit /b 0


Rem
Rem  Function ProcessFolder
Rem
Rem  Parameters: Folder
Rem
Rem  Process folder where ini file and license file could be located.
Rem  Search license in the folder.
Rem  If an ini file is found, process the ini file.
Rem
:ProcessFolder
	If not defined param_1 Exit /b
	If "%param_1:~-1,1%"=="\" Set "param_1=%param_1:~0,-1%"          & Rem Strip trailing backslash
	If not defined param_1 Exit /b
	If not exist "%param_1%" Exit /b
	Rem  Check if folder already processed
	>nul 2>&1 Find /i "::%param_1%::" "%G_f_processed_folders%"
	If not errorlevel 1 Exit /b
	Echo "::%param_1%::">> "%G_f_processed_folders%"
	Rem  Search license
	Rem  Set "param_1=%param_1%"
	Call :SearchLicense & Set "param_1=%param_1%"                    & Rem Restore value of param_1 after the call
	Rem  Loop through possible ini filenames, use Find so it also works when batch file is started in Unicode command prompt (cmd.exe /u)
	If not exist "%G_f_ini_files%" Exit /b
	For /f "delims=: tokens=2" %%i in ('Find /v "" ^<"%G_f_ini_files%"') do (
		Set "param_1=%param_1%\%%i"
		Call :ProcessIni
	)
	Exit /b


Rem
Rem  Function ProcessIni
Rem
Rem  Parameters: FileName
Rem
Rem  Determine if location of key file is defined in ini file.
Rem  Search that location for license key.
Rem
:ProcessIni
	If not defined param_1 Exit /b
	If not exist "%param_1%" Exit /b
	Rem  Get KeyPath value from ini file
	Rem  Set "param_1=%param_1%"
	Set "param_2=KeyPath"
	Call :IniRead
	If not defined result Exit /b
	If "%result%"=="$" Exit /b                                       & Rem If license in registry, skip
	Rem  Expand environment vars in folder name
	Set "param_1=%result%"
	Call :ExpandVars
	Rem  Search license
	Set "param_1=%result%"
	Call :SearchLicense
	Exit /b


Rem
Rem  Function ProcessKey
Rem
Rem  Parameters: Folder KeyFile
Rem
Rem  Process a license file. 
Rem  Store found license in a temporary file.
Rem
:ProcessKey
	If not defined param_1 Exit /b
	If not defined param_2 Exit /b
	If not exist "%param_1%\%param_2%" Exit /b
	Set "ProcessKey_file=%param_1%\%param_2%"
	Rem  Unpack if key file is packed
	If /i not "%param_2%"=="tcmdkey.zip" Goto ProcessKey_CHECK_KEY_SIZE
	If exist "%G_p_unzip%" Del /q "%G_p_unzip%"
	powershell -Command ^
		$ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference= 'SilentlyContinue';^
		Expand-Archive -LiteralPath '%ProcessKey_file%' -DestinationPath '%G_p_unzip%'
	If errorlevel 1 Echo [License ^<unknown^>] "%ProcessKey_file%" & Set /a "G_err_cnt_unp=%G_err_cnt_unp% + 1"
	Rem  Process unpacked key file
	Set "ProcessKey_file=%G_p_unzip%\wincmd.key"
	If not exist "%ProcessKey_file%" Goto ProcessKey_CLEAN_UP
:ProcessKey_CHECK_KEY_SIZE
	Rem  Check if file size of tmcd.key file is 1024 bytes
	For %%I in ("%ProcessKey_file%") do If not "%%~zI"=="1024" Goto ProcessKey_CLEAN_UP
	Rem  Encode file to hex encoded
	>nul 2>&1 Certutil -f -encodehex "%ProcessKey_file%" "%G_f_key_hex%" 12
	If errorlevel 1 Goto ProcessKey_CLEAN_UP
	Rem  Store key in environment var
	Set "ProcessKey_key="
	If exist "%G_f_key_hex%" For /f "usebackq" %%i in ("%G_f_key_hex%") do Set "ProcessKey_key=%%i"
	If not defined ProcessKey_key Goto ProcessKey_CLEAN_UP
	Rem  Get key index number
	Set "ProcessKey_key_idx="
	If exist "%G_f_license_keys%" For /f "delims=[]" %%i in (
		'Find /i "%ProcessKey_key%" ^<"%G_f_license_keys%"'
	) do Set "ProcessKey_key_idx=%%i"
	If defined ProcessKey_key_idx Goto ProcessKey_DISPLAY_KEY_ID
	Rem  Process new key
	Set /a "G_key_cnt=%G_key_cnt% + 1"
	Set "ProcessKey_key_idx=%G_key_cnt%"
	Echo [%ProcessKey_key_idx%]%ProcessKey_key%>> "%G_f_license_keys%"
:ProcessKey_DISPLAY_KEY_ID
	Rem  Display key id and key location
	Echo [License %ProcessKey_key_idx%] "%param_1%\%param_2%"
:ProcessKey_CLEAN_UP
	Set "ProcessKey_file="
	Set "ProcessKey_key="
	Set "ProcessKey_key_idx="
	Rem  Clean up temp files
	If exist "%G_f_key_hex%" Del /q "%G_f_key_hex%"
	If exist "%G_p_unzip%" rd /s /q "%G_p_unzip%"
	Exit /b


Rem
Rem  Function ProcessRegistry
Rem
Rem  Parameters: KeyName Options
Rem  Return value: FolderIniFile
Rem  
Rem  Get full ini name from registry, split into folder and filename, store unique filenames, return folder name
Rem
:ProcessRegistry
	Set "result="
	If not defined param_1 Exit /b
	Rem  Get full name of wincmd.ini file from registry
	Rem  Set "param_1=%param_1%"
	Set "param_3=%param_2%"
	Set "param_2=IniFileName"
	Call :RegRead
	If not defined result Exit /b
	Rem  Expand environment variables
	Set "param_1=%result%"
	Call :ExpandVars
	If not defined result Exit /b
	Rem  Split off folder and filename
	For %%I in ("%result%") do (
		Set "result=%%~dpI"
		Set "ProcessRegistry_fn=%%~nxI"
	)
	If not defined ProcessRegistry_fn Exit /b
	Rem  Add unique ini filename to list
	>nul 2>&1 Find /i "::%ProcessRegistry_fn%::" "%G_f_ini_files%"
	If errorlevel 1 Echo "::%ProcessRegistry_fn%::">> "%G_f_ini_files%"
	Set "ProcessRegistry_fn="
	Exit /b


Rem
Rem  Function RegDeleteHKCU
Rem
Rem  Delete license key from registry (for current user).
Rem
:RegDeleteHKCU
	Call :WrnRegDelete
	Reg delete "HKCU\Software\Ghisler\Total Commander" /v "Key"
	Exit /b


Rem
Rem  Function RegDeleteHKLM32
Rem
Rem  Delete license key from registry (for all users, 32-bit).
Rem
:RegDeleteHKLM32
	Call :WrnRegDelete
	Reg delete "HKLM\Software\Ghisler\Total Commander" /v "Key" /reg:32
	Exit /b


Rem
Rem  Function RegDeleteHKLM64
Rem
Rem  Delete license key from registry (for all users, 64-bit).
Rem
:RegDeleteHKLM64
	Call :OS_64bit || (
		Call :MsgNotAvailableOn32bit
		Pause
		Exit /b
	)
	Call :WrnRegDelete
	Reg delete "HKLM\Software\Ghisler\Total Commander" /v "Key" /reg:64
	Exit /b


Rem
Rem  Function RegImportHKCU
Rem
Rem  Import license key into registry (for current user).
Rem
:RegImportHKCU
	Call :GetKey
	If defined result Reg add "HKCU\Software\Ghisler\Total Commander" /v "Key" /t REG_BINARY /d "%result%"
	Exit /b


Rem
Rem  Function RegImportHKLM32
Rem
Rem  Import license key into registry (for all users, 32-bit).
Rem
:RegImportHKLM32
	Call :GetKey
	If defined result Reg add "HKLM\Software\Ghisler\Total Commander" /v "Key" /t REG_BINARY /d "%result%" /reg:32
	Exit /b


Rem
Rem  Function RegImportHKLM64
Rem
Rem  Import license key into registry (for all users, 64-bit).
Rem
:RegImportHKLM64
	Call :OS_64bit || (
		Call :MsgNotAvailableOn32bit
		Pause
		Exit /b
	)
	Call :GetKey
	If defined result Reg add "HKLM\Software\Ghisler\Total Commander" /v "Key" /t REG_BINARY /d "%result%" /reg:64
	Exit /b


Rem
Rem  Function RegRead
Rem
Rem  Parameters: KeyName ValueName Options
Rem  Return value: Value
Rem  
Rem  Read value from registry.
Rem  Double quotes in the value will be removed.
Rem
:RegRead
	Set "result="
	If not defined param_1 Exit /b
	If not defined param_2 Exit /b
	For /f "skip=2 delims=) tokens=1*" %%i in (
		'2^>nul Reg query "%param_1%" /v "%param_2%" %param_3% /z'
	) do Set "result=%%j"
	If not defined result Exit /b
	Rem  Trim and remove quotes, but keep spaces inside quotes
	Set "result=%result:"=[double quote]%"                           & Rem Prevent errors by replacing quotes with markers
	Set "param_1=%result%"
	Call :Trim
	If defined result Set "result=%result:[double quote]=%"          & Rem Remove markers
	Exit /b


Rem
Rem  Function RegSearchLicense
Rem
Rem  Parameters: KeyName Options Description
Rem
Rem  Search registry for a license.
Rem  Store found license in a temporary file.
Rem
:RegSearchLicense
	If not defined param_1 Exit /b
	If not defined param_3 Exit /b
	Set "RegSearchLicense_description=%param_3%"
	Rem  Get license from from registry
	Rem  Set "param_1=%param_1%"
	Set "param_3=%param_2%"
	Set "param_2=Key"
	Call :RegRead
	If not defined result Goto RegSearchLicense_END
	Rem  Get key index number
	Set "RegSearchLicense_key_idx="
	If exist "%G_f_license_keys%" For /f "delims=[]" %%i in (
		'Find /i "%result%" ^<"%G_f_license_keys%"'
	) do Set "RegSearchLicense_key_idx=%%i"
	If defined RegSearchLicense_key_idx Goto RegSearchLicense_DISPLAY_KEY_ID
	Rem  Process new key
	Set /a "G_key_cnt=%G_key_cnt% + 1"
	Set "RegSearchLicense_key_idx=%G_key_cnt%"
	Echo [%RegSearchLicense_key_idx%]%result%>> "%G_f_license_keys%"
:RegSearchLicense_DISPLAY_KEY_ID
	Rem  Display key id and key location
	Echo [License %RegSearchLicense_key_idx%] %RegSearchLicense_description%
:RegSearchLicense_END
	Rem  Clean up
	Set "RegSearchLicense_description="
	Set "RegSearchLicense_key_idx="
	Exit /b


Rem
Rem  Function SearchLicense
Rem
Rem  Parameters: Folder
Rem
Rem  Search folder for license files.
Rem  If found, process the license file.
Rem
:SearchLicense
	If not defined param_1 Exit /b
	If not exist "%param_1%\wincmd.key" If not exist "%param_1%\tcmdkey.zip" Exit /b
	Rem  Check if folder already processed
	>nul 2>&1 Find /i "::%param_1%::" "%G_f_license_folders%"
	If not errorlevel 1 Exit /b
	Echo "::%param_1%::">> "%G_f_license_folders%"
	Rem  Process key files
	Rem  Set "param_1=%param_1%"
	Set "param_2=wincmd.key"
	Call :ProcessKey & Set "param_1=%param_1%"                       & Rem Restore value of param_1 after the call
	Set "param_2=tcmdkey.zip"
	Call :ProcessKey
	Exit /b


Rem
Rem  Function Trim
Rem
Rem  Parameters: String
Rem  Return value: TrimmedString
Rem  
Rem  Trim leading and trailing spaces and tab characters.
Rem  Double quotes in the string are not supported (replace them before calling this function).
Rem
:Trim
	Set "result=%param_1%"
:Trim_LEFT
	If not defined result Exit /b
	If not "%result:~0,1%"==" " If not "%result:~0,1%"=="	" Goto Trim_RIGHT
	Set "result=%result:~1%"
	Goto Trim_LEFT
:Trim_RIGHT
	If not "%result:~-1,1%"==" " If not "%result:~-1,1%"=="	" Exit /b
	Set "result=%result:~0,-1%"
	Goto Trim_RIGHT


Rem  
Rem  History: 
Rem  
Rem  2023-03-03 Release v1.0b2
Rem  2023-03-03 Improved message when unable to unpack tcmdkey.zip files.
Rem  
Rem  2023-03-01 Release v1.0b1 (first release)
Rem  

History:

2023-03-03 Release v1.0b2
2023-03-03 Improved message when unable to unpack tcmdkey.zip files.

2023-03-01 Release v1.0b1 (first release)
User avatar
petermad
Power Member
Power Member
Posts: 14791
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: New tool: TC key.cmd

Post by *petermad »

However, there is no tool (that I know of) to easily move the license to and from the registry.
viewtopic.php?p=370621#p370621

But not with all the features of yours.
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
white
Power Member
Power Member
Posts: 4615
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: New tool: TC key.cmd

Post by *white »

petermad wrote: 2023-03-02, 02:58 UTC
However, there is no tool (that I know of) to easily move the license to and from the registry.
viewtopic.php?p=370621#p370621

But not with all the features of yours.
Yes, it doesn't "easily move the license to and from the registry". Have you tried my tool?

Your post says:
petermad wrote: 2020-02-22, 14:25 UTC First you have to convert you wincmd.key into a wincmd.reg Windows registry file - you can do that with this tool: https://madsenworld.dk/tcmd/wincmd_key_to_reg.exe (don't ask me where I got it from - but it works).
The download link is a dead link, but I managed to find it. Shall I update the link to https://madsenworld.dk/tcmd/tools/wincmd_reg.zip ?
The meta data in the exe says: CompanyName Taher Salem. So, it's rather obvious where you got it from ;)

The zip file also contains:
  • key_to_hex.exe - Converts wincmd.key file to hex format. My tool uses Certutil for that, which comes with Windows.
  • key_convert.exe - Imports wincmd.key into registry for current user.
User avatar
petermad
Power Member
Power Member
Posts: 14791
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: New tool: TC key.cmd

Post by *petermad »

Yes please.
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
white
Power Member
Power Member
Posts: 4615
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: New tool: TC key.cmd

Post by *white »

2petermad
Done
User avatar
norfie²
Power Member
Power Member
Posts: 986
Joined: 2006-02-10, 07:27 UTC

Re: New tool: TC key.cmd

Post by *norfie² »

white wrote: 2023-03-01, 16:01 UTC I have only tested this tool on own system with Windows 11 64-bit. So it needs further testing. If you own a license, I would greatly appreciate it if you test the tool on your system and report your findings here.
It works like a charm on W10x64. :mrgreen: Thanx for the useful tool. :D
"War is evil, in so far as it makes more bad people than it takes away."
Immanuel Kant in "Perpetual Peace"
User avatar
petermad
Power Member
Power Member
Posts: 14791
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: New tool: TC key.cmd

Post by *petermad »

2white
Have you tried my tool?
I have now, for some reason It doesn't seem to be able to unpack the keys under Windows 7:

Code: Select all


-=[ Total Commander license key tool v1.0b1 ]=-

 Licenses:

[License 1] "C:\totalcmd\TOOLS\wincmd.key"
Unable to unpack "C:\totalcmd\TOOLS\tcmdkey.zip"
Unable to unpack "C:\Users\Peter Madsen\AppData\Roaming\GHISLER\tcmdkey.zip"
Unable to unpack "C:\totalcmd\tcmdkey.zip"
Unable to unpack "c:\Program Files\totalcmd\tcmdkey.zip"
[License 2] Registry (current user)

 Number of unique licenses: 2

Choose one of these options:

1) Import license into registry (current user)
2) Import license into registry (all users, 64-bit)  (Run as administrator)
3) Import license into registry (all users, 32-bit)  (Run as administrator)
4) Export license
5) Delete license from registry (current user)
6) Delete license from registry (all users, 64-bit)  (Run as administrator)
7) Delete license from registry (all users, 32-bit)  (Run as administrator)
R) Refresh
Q) Quit and remove temporary files

>
I don't know what version of powershell I have under Windows 7

Under Windows 10 it goes better:

Code: Select all

-=[ Total Commander license key tool v1.0b1 ]=-

 Licenses:

[License 1] "C:\totalcmd\tcmdkey.zip"
[License 1] "c:\Program Files\totalcmd\tcmdkey.zip"
[License 1] "c:\Program Files (x86)\totalcmd\tcmdkey.zip"

 Number of unique licenses: 1

Choose one of these options:

1) Import license into registry (current user)
2) Import license into registry (all users, 64-bit)  (Run as administrator)
3) Import license into registry (all users, 32-bit)  (Run as administrator)
4) Export license
5) Delete license from registry (current user)
6) Delete license from registry (all users, 64-bit)  (Run as administrator)
7) Delete license from registry (all users, 32-bit)  (Run as administrator)
R) Refresh
Q) Quit and remove temporary files

>
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
white
Power Member
Power Member
Posts: 4615
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: New tool: TC key.cmd

Post by *white »

norfie² wrote: 2023-03-02, 15:11 UTC It works like a charm on W10x64. :mrgreen: Thanx for the useful tool. :D
Glad you like it. Thanks for testing :)
petermad wrote: 2023-03-02, 15:21 UTC
Have you tried my tool?
I have now, for some reason It doesn't seem to be able to unpack the keys:
As explained above, the Expand-Archive cmdlet needs to be available in Powershell.
white wrote: 2023-03-01, 16:01 UTC The tool probably works on Windows Vista and later, except for the handling of TCMDKEY.ZIP files. To unpack TCMDKEY.ZIP files, the batch file uses the Expand-Archive cmdlet available in Powershell 5 and later. PowerShell 5 is installed by default since Windows 10 and can be installed on Windows 7 and 8, see Windows PowerShell System Requirements.
What Windows version did you try? And what's the Powershell version? Is the Expand-Archive available? Try for example like this:

Code: Select all

Powershell -NoExit -Command Expand-Archive
User avatar
white
Power Member
Power Member
Posts: 4615
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: New tool: TC key.cmd

Post by *white »

petermad wrote: 2023-03-02, 15:21 UTC I don't know what version of powershell I have under Windows 7
Try:

Code: Select all

Powershell -NoExit -Command  Get-Host | Select-Object Version
Besides that it can not handle TCMDKEY.ZIP files, it does seem to work as designed. What Windows 7 version is it? 64-bit or 32-bit? SP 1 ?
User avatar
petermad
Power Member
Power Member
Posts: 14791
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: New tool: TC key.cmd

Post by *petermad »

2white
I tried:

Code: Select all

Powershell -NoExit -Command Expand-Archive
Which results in:
The term 'Expand-Archive' is not recognized as the name of a cmdlet..
.

with:

Code: Select all

Powershell -NoExit -Command Get-Host | Select-Object Version
I get:
'Select-Object' blev ikke genkendt som en intern eller ekstern kommando,
et program eller en batchfil.
But no big deal - I can just test with the unpacked key file - and with that I can create and delete all 3 keys under both Windows 10 and 7 :) :) :)

What Windows 7 version is it? 64-bit or 32-bit? SP 1 ?
Windows 7 Pro 64bit sp1
Last edited by petermad on 2023-03-02, 17:06 UTC, edited 1 time in total.
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
white
Power Member
Power Member
Posts: 4615
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: New tool: TC key.cmd

Post by *white »

petermad wrote: 2023-03-02, 16:07 UTC But no big deal - I can just test with the unpacked key file - and with that I can create and delete all 3 keys under both Windows 10 and 7 :)
Thanks for testing :). So it is Windows 7 64-bit. I hope someone will test how it behaves on a 32-bit system.
User avatar
Dalai
Power Member
Power Member
Posts: 9386
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: New tool: TC key.cmd

Post by *Dalai »

This should work to determine the PowerShell version:

Code: Select all

Powershell -NoExit -Command "Get-Host | Select-Object Version"
If PowerShell (or rather the Windows Management Framework) hasn't been updated on Win7 it's probably version 2.0 which is what it ships with.

Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
User avatar
white
Power Member
Power Member
Posts: 4615
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: New tool: TC key.cmd

Post by *white »

Dalai wrote: 2023-03-02, 16:21 UTC If PowerShell (or rather the Windows Management Framework) hasn't been updated on Win7 it's probably version 2.0 which is what it ships with.
True.
Another way to check the version is by executing the command $PSVersionTable in Powershell and look at the PSVersion value.

Code: Select all

Powershell -NoExit -Command $PSVersionTable
But, the tool is still useful even without the needed Powershell version to unpack TCMDKEY.ZIP files. It's not needed to handle unpacked key files and the tool does at least notify the user if there are TCMDKEY.ZIP files.
User avatar
petermad
Power Member
Power Member
Posts: 14791
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: New tool: TC key.cmd

Post by *petermad »

Well, Powershell is not listed in "Programs and Features". - so I cannot get the version from there.

The properties for powershell.exe and powershell_ise.exe shows file version 6.1.7600.16385 (Windows 7)
The properties for powershell.exe and powershell_ise.exe shows file version 10.0.19041.546 and version 10.0.19041.1 (Windows 10)

Both the windows 10 and Window 7 version is installed in a dir called "v1.0" - so that does not tell much.
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
white
Power Member
Power Member
Posts: 4615
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: New tool: TC key.cmd

Post by *white »

petermad wrote: 2023-03-02, 16:55 UTC The properties for powershell.exe and powershell_ise.exe shows file version 6.1.7600.16385 (Windows 7)
The properties for powershell.exe and powershell_ise.exe shows file version 10.0.19041.546 and version 10.0.19041.1 (Windows 10)
That's the host's version. What does $PSVersionTable tell you?
Post Reply