由Batch指令reg export導出的檔案*.reg檔為Unicode的格式,所以在使用batch指令處理檔案內容時,將會遇到無法開啟檔案存取的問題,如上一篇文章 Batch指令Findstr無法存取Unicode編碼的檔案 所提,需要藉由Type進行Unicode與Ascii的轉檔動作,以上這些還不是主要的困難,困難點在如何插入字串,如HKEY_LOCAL_MACHINE\SYSTEM要變成HKEY_LOCAL_MACHINE\PE3-SYSTEM,這才是要花時間思考,花了點時間想了一下要使用Batch現有的指令來完成這項任務還真不容易,順便將這部分的程式碼記錄下來,以便日後了解。
@echo off set Filename=%1 set sourcefile=%cd%\1.txt set targetfile=%cd%\2.txt cls ::ASCII字串轉Unicode字串 type %Filename% > %sourcefile% if exist %targetfile% del /q %targetfile% SETLOCAL ENABLEDELAYEDEXPANSION ::確認檔案原本字串的模式 findstr "HKEY_LOCAL_MACHINE\SYSTEM" %sourcefile% if not "%errorlevel%" == "0" set option=0 if not "%errorlevel%" == "1" set option=1 ::將檔案每一行讀進來後進行文字處理 for /f "tokens=* delims=" %%i in (%sourcefile%) do ( echo %%i> temp.txt ::等同於呼叫函數,不過這裡是呼叫LABEL,LABEL執行後會再返回 call :insertstring ) ::Unicode字串轉ASCII字串 CMD /U /C type %targetfile% > %Filename% ::刪除暫存檔 if exist %sourcefile% del /q %sourcefile% if exist %targetfile% del /q %targetfile% if exist temp.txt del /q temp.txt ::等同於C/C++使用return的作用,即成是執行道此就離開了 goto :EOF ::增加字串函數 :insertstring set /p temp=<temp.txt ::第一種模式 HKEY_LOCAL_MACHINE\SOFTWARE -> HKEY_LOCAL_MACHINE\PE3-SOFTWARE :softwarelabel if not "%option%" == "0" goto systemlabel findstr "HKEY_LOCAL_MACHINE\SOFTWARE" temp.txt if "%errorlevel%" == "0" echo %temp:\SOFTWARE=\PE3-SOFTWARE%>> %targetfile% if "%errorlevel%" == "1" echo %temp%>> %targetfile% goto functionend ::第二種模式 HKEY_LOCAL_MACHINE\SYSTEM -> HKEY_LOCAL_MACHINE\PE3-SYSTEM :systemlabel if not "%option%" == "1" goto functionend findstr "HKEY_LOCAL_MACHINE\SYSTEM" temp.txt if "%errorlevel%" == "0" echo %temp:\SYSTEM=\PE3-SYSTEM%>> %targetfile% if "%errorlevel%" == "1" echo %temp%>> %targetfile% :functionend
進階版寫法
@echo off
if "%1" == "" (
echo USAGE : ChangeRegistryPath.bat Sourcefile.reg Targetfile.reg
goto :EOF
)
if "%2" == "" (
echo USAGE : ChangeRegistryPath.bat Sourcefile.reg Targetfile.reg
goto :EOF
)
set Sourcefile=%1
set Targetfile=%2
set sourcetemp=%cd%\1.txt
set targettemp=%cd%\2.txt
cls
::ASCII字串轉Unicode 字串
type %Sourcefile% > %sourcetemp%
::SETLOCAL ENABLEDELAYEDEXPANSION
::將檔案每一行讀進來後進行文字處理
for /f "tokens=* delims=" %%i in (%sourcetemp%) do (
echo %%i> temp.txt
::等同於呼叫函數,不過這裡是呼叫LABEL,LABEL執行後會再返回
call :insertstring
)
::Unicode字串轉ASCII字串
CMD /U /C type %targettemp% > %Targetfile%
::刪除暫存檔
if exist %sourcetemp% del /q %sourcetemp%
if exist %targettemp% del /q %targettemp%
if exist temp.txt del /q temp.txt
::等同於C/C++使用return的作用,即成是執行道此就離開了
goto :EOF
::增加字串PE3-
:insertstring
set /p temp=<temp.txt
::第一種模式 HKEY_LOCAL_MACHINE\SOFTWARE -> HKEY_LOCAL_MACHINE\PE3-SOFTWARE
findstr "HKEY_LOCAL_MACHINE\SOFTWARE" temp.txt
if not "%errorlevel%" == "0" goto systemlabel
echo %temp:MACHINE\SOFTWARE=MACHINE\PE3-SOFTWARE%>> %targettemp%
goto functionend
::第二種模式 HKEY_LOCAL_MACHINE\SYSTEM -> HKEY_LOCAL_MACHINE\PE3-SYSTEM
:systemlabel
findstr "HKEY_LOCAL_MACHINE\SYSTEM" temp.txt
if not "%errorlevel%" == "0" goto other
echo %temp:MACHINE\SYSTEM=MACHINE\PE3-SYSTEM%>> %targettemp%
goto functionend
:other
echo %temp%>> %targettemp%
goto functionend
:functionend
沒有留言:
張貼留言