@echo off
:: ===============================================================================
:: AppFileName:	Download.cmd
 :: Date:		2025-04-10 05:45:58
:: Version:		1.25.04.10
:: Author:		lsl
::
:: Use curl.exe to download files
:: Parameter:	1.ini\*.ini	(subdir)
::					ini is the default folder
::
::				2.all/del/curl/desktop	/lnk
::					all 	Run download and install
::					del 	Del Downloaded Files and Run all again
::					curl	Use curl.exe to Download
::					desktop	For install app copying lnk file to all users desktop
::					lnk		for portable app use Shortcut.exe to create lnk file
::
:: if For 使用 FilePath FileName PathFilename 不可含()
:: ===============================================================================

rem ====----	Run a command as an Administrator from the Windows command line
set "Driver=%~d0"
set "O_FilePath=%~p0"
set "FilePath=%O_FilePath%"
cd /D "%Driver%%FilePath%"

setlocal
rem ====----	Parameter Example
set CmdFile=%~n0
if /i "%~1"=="" (
	set /a para_a=0
	echo Example:%CmdFile% ini\7-zip.ini all/curl/del/lnk/desktop
	echo 	%CmdFile%	Command File
	echo Parameter
	echo		7-zip.ini	Setting File for download and install
	echo 	ini\		path
	echo 	all		Install all
	echo 	curl		curl download only
	echo 	del		Del old download file
	echo 	lnk/desktop 	copy or create lnk file
	exit /b
) else (
	set /a para_a=1
)

rem ====----	para_b=0 lnk only
if /i "%~2"=="" (
	set /a para_b=0
) else (
	set /a para_b=1
)

rem ====----	desktop lnk EQU ""
if /i "%~2"=="desktop" (
	set /a para_b=0
)

if /i "%~2"=="lnk" (
	set /a para_b=0
)

rem ====----	Dwonload only	No Install all/curl
setlocal

set "iniConfFile=%~1"	& rem ini\xxxx

rem ====----	check .ini
set "iniConfFileExtension=%iniConfFile:~-3%"

if /i %iniConfFileExtension% EQU ini (
	set "Config_ini=%Driver%%FilePath%%iniConfFile%"	& rem 已自動包含全路徑
)

rem ====---- AppsPath	
	For /F "tokens=1 delims=\" %%I in ('echo %iniConfFile% ') do (
		if /i %%I NEQ ini (
			set AppsPath=%%I\
		)
	)

rem ====----	Initialize Variable
call :iniVariable

rem ====----	Read ini File
call :ReadiniFile

rem ====----	initialize	Set Path Variable
call :SetVariable

rem ====----	Must be after SetVariable
if /i "%~2"=="curl" (
	set /a Install=0
	set /a IsCurl=1
) else (
	set /a Install=1
	set /a IsCurl=2
)

rem ====----	Set Color
call :ColorSet

:: 2024-08-11	7-Zip	ShortCut	7z2407-x64.exe
set "app7z=%ProgramFiles%\7-Zip\7z.exe"
set "appShortcut=%SystemDrive%\PortableApps\Shortcut\Shortcut.exe"
set "cmdShortcut=%SystemDrive%\PortableApps\Shortcut\Shortcut.cmd"

rem ====----	7-Zip
setlocal enabledelayedexpansion

if %Install% EQU 1 (
rem 用%%會成未定義的空白

	if /i !AppName! EQU 7-Zip (
		set "File7zVer=%Version:.=%"
		set "Download7z=7z!File7zVer!-x64.exe"

		set /a IsUpdate7z=0
		if exist "%app7z%" (
			if "%FileName%" EQU "!Download7z!" (
				call :Installed7zApp
			)
		) else (
			set /a IsUpdate7z=1
		)

		if !IsUpdate7z! EQU 0 (
			goto End
		)
	)

	if /i !AppName! EQU ShortCut (
		if exist "%appShortcut%" (
			call :InstalledShortcutApp
			if exist "%cmdShortcut%" (
				goto End 7-Zip
			)
		)
	)
)
setlocal

rem ====----	End 7-Zip

rem ====----	Checkdir
call :Checkdir %AppsPath%Downloads\
call :Checkdir %AppsPath%Downloads\%AppName%
call :Checkdir %AppsPath%Downloads\%AppName%\%Version%

rem ====----	%PUBLIC%
if defined ToLinkFolder (
	call :Checkdir %PUBLIC%%ToLinkFolder%
)

rem ====----	Delete old Download File
if /i "%~2"=="del" (
	del /F /Q "%PathFilename%"
)

rem ====----	%IsCurl%=1	Download Only
if %IsCurl% EQU 1 (
	call :RunCurl
	goto End
)

rem ====----	Not downloaded yet
rem ====	不可含()
if not exist "%PathFilename%" (
	call :RunCurl
)

rem ====----	ChkFileExtension	zip 7z rar
rem ====	不可含()
if exist "%PathFilename%" (
	rem 這個時候不應有 .zip。 FileName UnZipFolder use !!
	call :ChkFileExtension
)

rem ====----	UnzipCopyFile	Copy only
if defined UnzipCopyFile (
	set /a IsZip=0
	set /a Install=0
)

rem == 2025-04-16
if defined RunXcopy (
	set /a IsZip=0
)

rem ====----	USE 7-zip extract zip 7z rar
if %IsZip% EQU 1 (
	echo|set /p="[45m"
	echo IsZip[0m %IsZip% [0m
	echo|set /p="[0m"

	echo|set /p="%ClolorYellow_BT%"
	echo call :7z x "%PathFilename%" "%UnZipPath%" "%UnZipFile%"
	echo|set /p="[0m"

	call :7z x "%PathFilename%" "%UnZipPath%" "%UnZipFile%"
	set "CopyUnZip=%UnZipPath%%UnZipFile%"
	echo.
)

rem ====----	ChkInstallFile
call :ChkInstallFile

rem ====----	InstallFilename	SilentInstall
rem ====	start installation
rem ====	2024-09-19 PowerShell	appx	msixbundle

if %Install% NEQ 0 (
	call :RunInstall
)

rem ====----	copy lnk file
if defined LinkFile (
	rem ====----	Set Link Environment for copy LinkFile
	call :CopyLinkFile
)

rem ====----	copy Crack file
if defined ToRegistFile (
	call :CopyToRegistFile
)

goto End

:iniVariable
	set AppName=
	set URL=
	set Download=
	set FileName=
	set Version=
	set FileDate=
	set SilentInstall=
	rem ====	WinNTsetup	WinNTSetup_v533b.rar
	set MediaFire=
	set dButton=

	set UnZipFile=
	set UnZipFolder=
	rem ====	王漢宗中文字型下載.rar
	set UnzipCopyFile=
	rem ====	NTPClock.exe
	set NotZipFile=
	rem ====	winampac3_1_20a.exe	Nvdia
	set ExeUnZip=
	rem ====	DefenderControl	dControl.zip
	set PassWord=

	set LinkEnv=
	set LinkFile=
	set ToLinkFolder=

	set ToRegistEnv=
	set ToRegistFolder=
	set ToRegistFile=

	set RunXcopy=

	exit /b

:ReadiniFile
	rem ====----	setlocal enabledelayedexpansion
	rem ====----	Read ini File
	rem ====	不可含()
	rem xxx set "FilePath=%O_FilePath:^(=^^(%"
	rem xxx set "FilePath=%O_FilePath:^)=^^)%"
	echo|set /p="[45m"
	echo ReadiniFile[0m
	echo|set /p="%Driver%%FilePath%%iniConfFile%"[0m
	echo|set /p="[0m"
	echo.

	rem  修path
	For /F "tokens=1* delims==" %%I in ('findstr /i "^[A-z]" "%Driver%%FilePath%%iniConfFile%"') do (
		set "%%I=%%J"
		rem ====	2024-08-28	()
		if defined %%I (
			if defined %%J (
				rem echo defined
				set JJ=%%J
				set "%%I=!JJ:^)=^^)!"
			)
		)

		if defined %%I (
			set II=%%I
			::	Show only URL and Download
			if /i !II! EQU URL (echo 	%%I	 = %%J)
			if /i !II! EQU Download (echo 	%%I = %%J)
			if /i !II! EQU Version (echo 	%%I  = %%J)
			if /i !II! EQU FileDate (echo 	%%I = %%J)
		)
	)

	echo.
	exit /b

:SetVariable
	rem ====----	Set Path Variable
	::	PathFilename
	::	InstallFilename
	::	DownloadPath

	::	ini AppName		Version			FileName		ZipFolderName
	::	ini UnZipFile	UnzipFolder		UnzipCopyFile	UnZipSetupExe
	::	ini	NotZipFile	ExeUnZip		PassWord
	::	ini	MediaFire	dButton

	::	ini LinkEnv		LinkFile		ToLinkFolder	ToLinkTarget
	::	ini ToRegistEnv	ToRegistFolder 	ToRegistFile

	::	ini RunXcopy ^(RoboCopy^)

	set "FileName=%FileName:^(=^^(%"
	set "FileName=%FileName:^)=^^)%"

	set "FilePath=%~p0"
	set "FilePath=%FilePath:^(=^^(%"
	set "FilePath=%FilePath:^)=^^)%"

	if defined UnzipFolder (
		set "UnzipFolder=%UnzipFolder:^(=^^(%"
		set "UnzipFolder=%UnzipFolder:^)=^^)%"
	)

	if defined UnZipFile (
		set "UnZipFile=%UnZipFile:^(=^^(%"
		set "UnZipFile=%UnZipFile:^)=^^)%"
	)
	rem !FilePath! !UnZipFolder!
	set "PathFilename=%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%\%FileName%"
	rem 未被更改這時是%%
	set "InstallFilename=%PathFilename%"
	set "DownloadPath=%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%\"

	rem 解壓縮後再執行安裝
	set "FileExtension=%FileName:~-3%"
	rem ====	不可含() 也不可加 "" 2024-10-11 21:11
	if /i %FileExtension% EQU zip (
		if defined UnZipFile (
			set "InstallFilename=%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%\%UnZipFile%"
		)
	)

	if defined UnZipFolder (
		set "UnZipPath=%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%!UnZipFolder!"
	) else (
		set "UnZipPath=%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%\"
	)

	if defined ZipFolderName (
		set "DelZipPath=%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%\!ZipFolderName!"
	)

	set /a FileExist=0
	set /a Install=0

	set /a IsZip=0
	set /a IsUnZipFolder=0

	set /a IsCurl=0

	::	MediaFire
	set II=
	set Ihref=
	set Downloadhref=
	exit/b

:RunCurl
	rem bug
	echo.[45mRunCurl[0m
 	echo.

	rem ====----	curl.exe	:SetVariable	----	PathFilename
	if not defined Download (
		echo %ClolorRed_NT%Not defined Download%DefaultClolor%
	)

	rem ====----	MediaFire
	if defined MediaFire (
		goto RunMediaFire
	)

	rem ====	不可含()
	if not exist "%PathFilename%" (
		curl.exe -LJR --url "%Download%" -o "%PathFilename%" -k --show-error --fail
	)

	if %errorlevel% NEQ 0 (
		echo.%ClolorRed_BT%
		echo An curl error was found : "%Download%"
		echo.%DefaultClolor%
		pause
	)

	if exist "%PathFilename%" (
		echo Download: "%PathFilename%"
	 	dir "%pathFilename%" | findstr /I "%Filename%"
	)
	echo.
	exit /b

:RunMediaFire
	rem ====----	1.	Download WinNTSetup_v533b.rar.html
	::	WinNTSetup_v533b.rar.html
	curl.exe -LJR --url "%Download%" -o "%pathFilename%.html" -k --show-error --fail -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59"
	echo.

	if %errorlevel% NEQ 0 (
		echo.%ClolorRed_BT%
		echo An curl error was found : %Download%
		echo.%DefaultClolor%
		pause
	)

	rem ====----	2.	Create WinNTSetup_v533b.rar.href (Find downloadButton)
	::	WinNTSetup_v533b.rar.href
	echo.>"%pathFilename%.href"

	setlocal enabledelayedexpansion
	rem ====	不可含()
	rem 2025-09-22 "tokens=1* delims= "
	For /F "tokens=2* delims= " %%I in ('findstr /i "%dButton%" "%pathFilename%.html"') do (
		if %%I NEQ "" (
		set "II=%%I"
		rem 2025-09-22 set "Ihref=!II:~0,4!"
		set "Ihref=!II:~0,18!"
			rem 2025-09-22 if !Ihref! EQU href (
			if !Ihref! EQU data-scrambled-url (
				echo 	%%I
				echo %%I >>"%pathFilename%.href"
			)
		)
	)

	echo.
	rem ====----	3.	Get download href from WinNTSetup_v533b.rar.ref
	rem ====	不可含()
	rem 2025-09-22 For /F "tokens=1,2 delims==" %%I in ('findstr /i "href" "%pathFilename%.href"') do (
	For /F "tokens=1,2 delims==" %%I in ('findstr /i "data-scrambled-url" "%pathFilename%.href"') do (
		if /i %%J NEQ "" (
			set "Downloadhref=%%J"
		)
	)
	set "Download=%Downloadhref:~1,-2%"
	echo 	Download = %Download%
	setlocal

	echo.
	rem ====----	4.	Download WinNTSetup_v533b.rar
	::	WinNTSetup_v533b.rar
	::	2024-05-29
	rem ====	不可含()
	if not exist "%PathFilename%" (
		echo curl.exe -LJR --url "%Download%" -o "%pathFilename%" -k --show-error --fail
		curl.exe -LJR --url "%Download%" -o "%pathFilename%" -k --show-error --fail -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59"

		if %errorlevel% NEQ 0 (
			echo.%ClolorRed_BT%
			echo An curl error was found : %Download%
			echo.%DefaultClolor%
			pause
		)
	)

	rem ====	不可含()
	if exist "%PathFilename%" (
		echo		Download:	"%PathFilename%"
		dir "%pathFilename%" | findstr /I "%Filename%"
	)

	exit /b

:ChkFileExtension
	echo|set /p="[45m"
	echo ChkFileExtension[0m %Filename:~-3%[0m
	echo|set /p="[0m"

	set "FileExtension=%Filename:~-3%"
	rem ====----	IsZip	Filename Extension is .zip
	::	不可加""

	::	zip
	if /i %FileExtension% EQU zip (
		set /a IsZip=1
	)

	::	rar
	if /i %FileExtension% EQU rar (
		set /a IsZip=1
	)

	::	7z
	set "FileExtension=%Filename:~-2%"
	if /i %FileExtension% EQU 7z (
		set /a IsZip=1
	)

	::	ExeUnZip
	if defined ExeUnZip (
		echo.ExeUnZip
		set /a IsZip=1
	)
	echo.
	exit /b

:ChkInstallFile
	echo|set /p="[45m"
	echo ChkInstallFile[0m
	echo|set /p="[0m"

	rem ====----	INI	UnZipFolder
	::	Check if the unzipped subdirectory exists
	::	Does the downloaded file have a specified decompression directory?
	::	2024-08-28	!UnZipFolder!	()

	rem !UnZipFolder! !FilePath!

	if defined UnZipFolder (
		echo %AppsPath%Downloads\%AppName%\%Version%!UnZipFolder!
		rem ::	1. Check the unzipped directory	::會報錯

		if defined UnzipFolder (
			call :Checkdir %AppsPath%Downloads\%AppName%\%Version%!UnZipFolder!
		)
		rem ::	1+2=3	 ::會報錯
		set /a IsUnZipFolder=2
	)

	rem ====----	IsZip	zip檔解壓縮到在INI定義的自建資料夾	IsUnZipFolder
	::	1+2=3
	::	檢查檔案已經解壓縮到次目錄 1 2 3
	::	NEQ 0 是zip檔	並且有指定解壓縮目錄
	rem ====----	IsCopyUnZip
	set /a IsCopyUnZip=%IsZip%+%IsUnZipFolder%
	if %IsCopyUnZip% NEQ 0 (
		set "CopyUnZip=%UnZipPath%%UnZipFile%"
	)

	rem ====----	Check Download Finished and Run Install
	::	檢查是否有下載的檔案
	rem ====	不可含()
	if exist "%PathFilename%" (
		if %Install% NEQ 0 (
			echo.%ClolorYellow_BT%"%PathFilename%"%DefaultClolor%
		)
		set /a FileExist=1
	)

	rem ====----	Install	InstallFilename
	::	檔案已解壓縮 並有參數 %~2
	::	para_b 是否需要安裝
	set /a Install=%FileExist%*%para_b%

	rem ====----	Install
	::	是否需要安裝
	if not defined SilentInstall (
		set /a Install=0
	) else (
		set SilentInstall=%SilentInstall:""= %
	)

	rem ====----	Install Silent
	::	安裝檔是EXE InstallFilename
	::	要使用安裝檔的檔名	下載檔名改成解析縮後的檔名
	::	exe

	set "FileExtension=%UnZipFile:~-3%"
	rem ====	不可含()
	if /i "%FileExtension%" EQU exe (
		if defined UnZipFile (
			set "InstallFilename=%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%\%UnZipFile%"
		)
	)

	::	msi
	if /i "%FileExtension%" EQU msi (
		set "InstallFilename=%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%\%UnZipFile%"
	)

	::	msix
	if /i "%FileExtension%" EQU six (
		set "InstallFilename=%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%\%UnZipFile%"
	)

	::	2024-09-18	Intel Driver
	if defined UnZipSetupExe (
		if exist %Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%!UnZipFolder!%UnZipSetupExe% (
			set "InstallFilename=%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%!UnZipFolder!%UnZipSetupExe%"
		)
	)

	echo.
	exit /b


:RunInstall
	echo|set /p="[45m"
	echo RunInstall[0m
	echo "%InstallFilename%"[0m
	echo|set /p="[0m"

	rem 	:: FileExtension 2024-10-04
	set "FileExtension=%InstallFilename:~-3%"

	::	msi
	if /i !FileExtension! EQU msi (
		echo|set /p="[92m"
		echo call :RunCall msiexec /i "%InstallFilename%" %SilentInstall%
		echo|set /p="[0m"

		call :RunCall msiexec /i "%InstallFilename%" %SilentInstall%
		echo|set /p=%ClolorYellow_BT%
		echo."%InstallFilename%"
		echo|set /p=%DefaultClolor%
	)

	::	exe	FileExtension 有2次設定 ChkFileExtension 429 ChkInstallFile 465
	if /i !FileExtension! EQU exe (
		echo|set /p="[92m"
		echo call :RunCall "%InstallFilename%" %SilentInstall%
		echo|set /p="[0m"

		call :RunCall "%InstallFilename%" %SilentInstall%
	)

	set "FileExtension=%InstallFilename:~-4%"
	::	appx
	if /i !FileExtension! EQU appx (
		echo|set /p="[92m"
		echo call :RunCall PowerShell Add-AppxPackage -Path "%InstallFilename%" %SilentInstall%
		echo|set /p="[0m"

		call :RunCall PowerShell Add-AppxPackage -Path "'%InstallFilename%'" %SilentInstall%
	)

	::	appx
	if /i !FileExtension! EQU msix (
		echo|set /p="[92m"
		echo call :RunCall PowerShell Add-AppxPackage -Path "'%InstallFilename%'"
		echo|set /p="[0m"

		call :RunCall PowerShell Add-AppxPackage -Path "'%InstallFilename%'"
	)

	::	msixbundle
	set "FileExtension=%InstallFilename:~-10%"
	if /i !FileExtension! EQU msixbundle (
		echo|set /p="[92m"
		echo call :RunCall PowerShell Add-AppxPackage -Path "'%InstallFilename%'" %SilentInstall%
		echo|set /p="[0m"

		call :RunCall PowerShell Add-AppxPackage -Path "'%InstallFilename%'" %SilentInstall%
	)

	rem ====	2024-08-28	%FileName%	()
	if %errorlevel% EQU 0 (
		if %Install% NEQ 0 (
			echo|set /p="[92m"
			rem 2024-08-28
			echo "%InstallFilename%" Silent installation successful
			echo|set /p="[0m"
		)
	) else (
		if %Install% NEQ 0 (
			echo|set /p="%ClolorRed_BT%"
			echo "%InstallFilename%"	Silent installation failed
			echo|set /p="%DefaultClolor%"
		)
	)

	echo.
	exit /b

:CopyLinkFile
	echo|set /p="[0m"
	echo|set /p="[45m"
	echo CopyLinkFile[0m
	echo|set /p="[0m"

	setlocal enabledelayedexpansion
	rem ====----	enabledelayedexpansion
	if "%LinkEnv%" EQU "%%ProgramData%%" (
		set "LinkEnv=%ProgramData%"
	)

	if "%LinkEnv%" EQU "%%USERPROFILE%%" (
		set "LinkEnv=%USERPROFILE%"
	)

	if "%LinkEnv%" EQU "%%APPDATA%%" (
		set "LinkEnv=%APPDATA%"
	)

	if "%LinkEnv%" EQU "%%PUBLIC%%" (
		set "LinkEnv=%PUBLIC%"
	)

	rem ====----	LinkEnv
	set "LinkEnv=!LinkEnv!"

	rem ====----	copy linkFile
	rem ====	Public
	rem ====	不可含()
	if exist %LinkEnv%%LinkFile% (
		echo|set /p="[92m"
		echo copy  /Y "%LinkEnv%%LinkFile%" "%Public%%ToLinkFolder%%ToLinkTarget%"
		echo|set /p="[0m"

		copy  /Y "%LinkEnv%%LinkFile%" "%Public%%ToLinkFolder%%ToLinkTarget%"
		echo %ClolorYellow_BT%"%LinkEnv%%LinkFile%"
		echo "%Public%%ToLinkFolder%%ToLinkTarget%"%DefaultClolor%
	)

	if %errorlevel% NEQ 0 (
		echo.%ClolorRed_BT%
		echo A copy lnk error was found : "%LinkEnv%%LinkFile%"
		echo.%DefaultClolor%
		pause
	)

	exit /b

:CopyToRegistFile
	echo|set /p="[0m"
	echo|set /p="[45m"
	echo CopyToRegistFile[0m
	echo|set /p="[0m"
	rem ====----	copy ToRegistFolder
	call :Checkdir %ToRegistEnv%%ToRegistFolder%
	setlocal enabledelayedexpansion
	rem	::	(x86)	不可加^
	if "%ToRegistEnv%" EQU "%%ProgramFiles(x86)%%" (
		set "ToRegistEnv=%ProgramFiles(x86)%"
	)
	set "ToRegistEnv=!ToRegistEnv!"

	if "%ToRegistEnv%" EQU "%%ProgramFiles%%" (
		set "ToRegistEnv=%ProgramFiles%"
	)

	if "%ToRegistEnv%" EQU "%%USERPROFILE%%" (
		set "ToRegistEnv=%USERPROFILE%"
	)
	::	2024-08-08
	if "%ToRegistEnv%" EQU "%%APPDATA%%" (
		set "ToRegistEnv=%APPDATA%"
	)
	::	SystemDrive
	if "%ToRegistEnv%" EQU "%%SystemDrive%%" (
		set "ToRegistEnv=%SystemDrive%"
	)

	::	PUBLIC
	if "%ToRegistEnv%" EQU "%%PUBLIC%%" (
		set "ToRegistEnv=%PUBLIC%"
	)

	::	ALLUSERSPROFILE
	if "%ToRegistEnv%" EQU "%%ALLUSERSPROFILE%%" (
		set "ToRegistEnv=%ALLUSERSPROFILE%"
	)

	if "%ToRegistEnv%" EQU "%%ProgramData%%" (
		set "ToRegistEnv=%ProgramData%"
	)

	::	TEMP
	if "%TEMP%" EQU "%%TEMP%%" (
		set "ToRegistEnv=%TEMP%"
	)

	::	LOCALAPPDATA
	if "%LOCALAPPDATA%" EQU "%%LOCALAPPDATA%%" (
		set "ToRegistEnv=%LOCALAPPDATA%"
	)

	rem ====----	ToRegistEnv

	set "ToRegistEnv=!ToRegistEnv!"

	rem ====----	Xcopy ToRegistFile	 esc color
	if defined RunXcopy (
		set /a IsCopyUnZip=0
		if "%RunXcopy%" EQU "RoboCopy" (
			echo|set /p="[92m"
			echo RoboCopy "%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%%UnzipFolder%" "%ToRegistEnv%%ToRegistFolder%"
			echo|set /p="[0m"

			RoboCopy "%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%%UnzipFolder%" "%ToRegistEnv%%ToRegistFolder%" /S /FP /NS /NC /NDL /NJH /NJS /V > "%iniConfFile%_xcopy.txt"
			rem ====	type .txt
			echo.
			type "%iniConfFile%_xcopy.txt"
			del "%iniConfFile%_xcopy.txt"
			echo %ClolorYellow_BT%"%Driver%!FilePath!%AppsPath%Downloads\%AppName%\%Version%%UnzipFolder%"
			echo %ClolorYellow_BT%"%ToRegistEnv%%ToRegistFolder%"%DefaultClolor%
			echo.
		) else (
			echo|set /p="[92m"
			echo call :7z x "%PathFilename%" "%ToRegistEnv%%ToRegistFolder%" "%UnZipFile%"
			echo|set /p="[0m"

			call :7z x "%PathFilename%" "%ToRegistEnv%%ToRegistFolder%" "%UnZipFile%"
			echo %ClolorYellow_BT%"%PathFilename%"
			echo %ClolorYellow_BT%"%ToRegistEnv%%ToRegistFolder%%ToRegistFile%"%DefaultClolor%
			echo.
		)
	)
	if %errorlevel% NEQ 0 (
		echo.%ClolorRed_BT%
		echo A xcopy error was found : "%PathFilename%"
		echo.%DefaultClolor%
		pause
	)

	rem ====----	copy ToRegistFile
	rem ====	NotZipFile
	if defined NotZipFile (
		set "CopyUnZip=%PathFilename%"
		set /a IsCopyUnZip=1
	)

	rem ====	UnzipCopyFile
	if defined UnzipCopyFile (
		set "CopyUnZip=%PathFilename%"
		set /a IsCopyUnZip=1
	)

	rem ====	if exist "%UnZipPath%" (
	if %IsCopyUnZip% NEQ 0 (
		echo.
		echo|set /p="[92m"
		echo copy  /Y "%CopyUnZip%" "%ToRegistEnv%%ToRegistFolder%%ToRegistFile%"
		echo|set /p="[0m"

		copy  /Y "%CopyUnZip%" "%ToRegistEnv%%ToRegistFolder%%ToRegistFile%"
		::	ToRegistEnv因為有() 所以加""
		echo %ClolorYellow_BT%"%CopyUnZip%"
		echo "%ToRegistEnv%%ToRegistFolder%%ToRegistFile%"%DefaultClolor%
	)

	if %errorlevel% NEQ 0 (
		echo.%ClolorRed_BT%
		echo An copy file error was found : "%CopyUnZip%"
		echo.%DefaultClolor%
		pause
	)
	rem 2025-03-30 2025-04-16
	call :FileExist %DelZipPath%

	if not defined UnzipFolder (
		set NotZipFile=
	) 

	if not defined NotZipFile (
		if %FileIsExist% EQU 1 (
			if defined UnzipFolder (
				call :DelFile "%DelZipPath%"
			)
		)
	)
	exit /b

:ColorMsg
	echo|set /p=[0m
	exit/b

:ColorSet
	::	1 Normal Text
	set "ClolorBlack_NT=[30m"
	set "ClolorRed_NT=[31m"
	set "ClolorGreen_NT=[32m"
	set "ClolorYellow_NT=[33m"
	set "ClolorBlue_NT=[34m"
	set "ClolorMagenta_NT=[35m"
	set "ClolorCyan_NT=[36m"
	set "ClolorWhite_NT=[37m"

	::	2 Normal Background
	set "ClolorBlack_NB=[40m"
	set "ClolorRed_NB=[41m"
	set "ClolorGreen_NB=[42m"
	set "ClolorYellow_NB=[43m"
	set "ClolorBlue_NB=[44m"
	set "ClolorMagenta_NB=[45m"
	set "ClolorCyan_NB=[46m"
	set "ClolorWhite_NB=[47m"

	::	3 Bright Text
	set "ClolorBlack_BT=[90m"
	set "ClolorRed_BT=[91m"
	set "ClolorGreen_BT=[92m"
	set "ClolorYellow_BT=[93m"
	set "ClolorBlue_BT=[94m"
	set "ClolorMagenta_BT=[95m"
	set "ClolorCyan_BT=[96m"
	set "ClolorWhite_BT=[97m"

	::	4 Bright Background
	set "ClolorBlack_BB=[100m"
	set "ClolorRed_BB=[101m"
	set "ClolorGreen_BB=[102m"
	set "ClolorYellow_BB=[103m"
	set "ClolorBlue_BB=[104m"
	set "ClolorMagenta_BB=[105m"
	set "ClolorCyan_BB=[106m"
	set "ClolorWhite_BB=[107m"

	::	0 DefaultClolor
	set "DefaultClolor=[0m"

	exit /b

:Checkdir
	rem ====----	https://stackoverflow.com/questions/18639663/how-to-pass-command-line-parameters-with-space-in-batch-file

	if exist "%*" (
		exit /b
	)
	if not exist "%*" (
		mkdir "%*"
	)
	if exist "%*" (
		echo "%*" Created
	)
	exit /b

:7z
	rem ====----	parameters 1 2 3 4
	rem ====	mode file outpath file
	rem 2025-03-30
	call :FileExist %~2

	if %FileIsExist% EQU 0 (
		goto End
	)

	echo|set /p="[92m"
	echo "%ProgramFiles%\7-Zip\7z.exe" %1 -y %2 -o%3 %4 -p%password% -aoa
	echo|set /p="[0m"
	"%ProgramFiles%\7-Zip\7z.exe" %1 -y %2 -o%3 %4 -p%password% -aoa > nul

	exit /b

::	2024-08-11
:Installed7zApp
	if exist "%app7z%" (
		call :Chk7zVer
	)
	exit /b

::	2024-08-11
:InstalledShortcutApp
	if exist "%appShortcut%" (
		call :AppShortcutFile
	)
	exit /b

::	2024-08-24
:Chk7zVer
	set Version_N=%Version:.=%
	set Get7zVer=%TEMP%\7zVer.txt
	"%app7z%" -h > "%Get7zVer%"

	findstr "%AppName% %Version%" "%TEMP%\7zVer.txt" > %TEMP%\7zVerStr.txt"

	For /F "tokens=1 delims=^^:" %%I in (%TEMP%\7zVerStr.txt) do (
		set "II=%%I"
		set "IIDot=!II:~6,5!"
		set "II=!II:~6,2!!II:~9,2!"
		set "_7zVer=!II!"
		set /a "_7zVer_N=_7zVer*1"

		if !_7zVer_N! GTR %Version_N% (
			echo. 	!app7z! !IIDot! GTR %FileName% %Version%
		) else (
			if !_7zVer_N! EQU %Version_N% (
				echo. 	!app7z! !IIDot! EQU %FileName% %Version%
			) else (
				echo|set /p="%ClolorCyan_BT%"
				echo. 	!app7z! !IIDot! update to %Version%
				echo|set /p="%DefaultClolor%"
				set IsUpdate7z=1
			)
		)
	)
	exit /b

::	2024-08-11
:AppShortcutFile
	echo|set /p="%ClolorCyan_BT%"
	echo %appShortcut%
	echo|set /p="%DefaultClolor%"
	exit /b

::	2025-03-30
:FileExist
	set /a FileIsExist=0
	if exist "%*" (
		set /a FileIsExist=1
		rem echo.	%FileIsExist%
	) else (
		set /a FileIsExist=0
		rem echo.	%FileIsExist%
		exit /b
	)
	exit /b

:DelFile
	echo.[92mrd /Q /S %*[0m
	rd /Q /S %*
	exit /b

:RunCall
	%*
	exit /b

:End
	echo.
	set para_a=
	set para_b=
	set para_c=
	exit /b
	rem	%ComSpec% /k