2022年5月18日 星期三

如何用BAT與Python來下載、解壓縮、環境設定Sqlite3

        想要學習Python 3.9.0 與 sqlite3 。面對Sqlite3的下載、解壓縮、檔案資料夾與後續的環境設定,總覺得很麻煩。希望能夠一鍵完成所有的步驟,包含Sqlite3的下載、解壓縮、檔案資料夾與後續的環境設定。懶,唯一的理由。就是讓一切變得簡單。
        由於單純的BAT,在下載的時候需要用到Wget.exe。在Python 3.9.0 環境中,可以利用wget套件來進行下載。因此改用Python wget 套件。整個程式就是利用BAT 執行Python程式來進行下載、解壓縮、搬移檔案與資料夾,最後再利用BAT移到系統資料夾與設定環境變數。
        修正第二版,由於Python 程式下載完成後,需要一段時間讓檔案成型。否則下載過程中會形成暫存檔,讓後面的解壓縮程序出現錯誤。導致無法解壓縮。因此,在Python 程式新增暫停2秒的程序。另外,在程式中也改寫成Function,讓後續的程序可以利用。

下載檔案。解壓密碼:demo1234
教學影片:


修正第二版:

檔案名稱:Python390Sqlite3Install.bat
檔案內容:
@echo off
if not exist %HomeDrive%%HomePath%\AppData\Local\Programs\Python\Python39 (
 start https://skjhcreator.blogspot.com/2022/05/batpython-390.html
 echo "電腦內尚未安裝Python 3.9.0。請前往下載"
 pause
) else (
echo "電腦內已安裝程式,不需要安裝 Pyhon 3.9.0"
echo "安裝套件 wget"
python -m pip install wget
cd %~dp0program\
python -m Sqlite3DownloadUnzip.py
timeout /t 5
call MoveSystem.bat
timeout /t 2
call Sqlite3EnvSetting.bat
timeout /t 2
)
exit

檔案名稱:Sqlite3DownloadUnzip.py
檔案內容:
import os
import wget
from zipfile import ZipFile
import shutil
import time

def UrlPathReadFunction(DownloadsFilePath,DownloadsFileName,DownloadsFileNameSet):
    OpenFile = open('Urlpath.txt','r')
    ReadFile = OpenFile.readlines()
    for i in ReadFile:
        i = i.replace('\n','')
        UrlPath.append(i)
        temp = i.split('/')
        temp = temp[-1].replace('\n','')
        FileName.append(temp)
    OpenFile.close()
    FileNameSet = set(FileName)
    return FileNameSet,UrlPath

def CheckBetweenDownloadAndUrlPath(FileNameSet,DownloadsFileNameSet):
    StopDownloadSet = FileNameSet.difference(DownloadsFileNameSet)
    TempDownloadsFileName = []
    for i in StopDownloadSet:
        TempDownloadsFileName.append(i)
    return TempDownloadsFileName

def DownloadFunction(TempFileName,UrlPath):
    #  如果同名,就不下載。
    for i in range(0,len(TempFileName)):
        print("Download "+TempFileName[i]+". Start!")
        wget.download(UrlPath[i],str(DownloadsFilePath+TempFileName[i]))
        print("Download "+TempFileName[i]+". OK!")

def UnZipFunction(CheckFileName):
    for i in CheckFileName:
        i = CheckFilePath+i
        with ZipFile(i,'r') as zip:
            zip.printdir()
            zip.extractall('temp')

def MoveFilesToSqlite3():
    MoveFiles = []
    MoveFilePath = os.getcwd()+'\\temp\\'        
    MoveFiles = os.listdir(MoveFilePath)
    MoveDir = MoveFiles[0]
    RenamedFile01 = MoveFiles[1].replace(".def","")
    del MoveFiles[0]
    for file in MoveFiles:
        shutil.move(MoveFilePath+file,MoveFilePath+MoveDir)
    os.rename(MoveFilePath+MoveDir,MoveFilePath+RenamedFile01)

# Sqlite3 下載
StopDownloadSet = set()
ReadFile = []
FileName = []
UrlPath = []
DownloadsFileName = []
DownloadsFilePath = os.getcwd()+'\\Downloads\\'
DownloadsFileName = os.listdir(DownloadsFilePath)
DownloadsFileNameSet = set(DownloadsFileName)
# 讀取UrlPath.txt檔案內容,取得下載路徑與檔名
FileNameSet,UrlPath = UrlPathReadFunction(DownloadsFilePath,DownloadsFileName,DownloadsFileNameSet)
# 檢查Downloads資料夾檔案名稱與UrlPath檔案內檔案名稱,是否有同名。如果同名,就不下載。
TempFileName = []
TempFileName = CheckBetweenDownloadAndUrlPath(FileNameSet,DownloadsFileNameSet)
DownloadFunction(TempFileName,UrlPath)
time.sleep(2)
# 檢查Downloads資料夾內的壓縮檔,並解壓縮到temp資料夾
CheckFileName = []
CheckFilePath = os.getcwd()+'\\Downloads\\'
CheckFileName = os.listdir(CheckFilePath)
UnZipFunction(CheckFileName)
# 將temp資料夾內的檔案更名並移動到temp\sqlite3
MoveFilesToSqlite3()


檔案名稱:MoveSystem.bat
檔案內容:
@echo off
REM :: BatchGotAdmin (Run as Admin code starts)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
REM :: BatchGotAdmin (Run as Admin code ends)
REM :: Your codes should start from the following line
move %~dp0temp/sqlite3 %WinDir%
timeout /t 2
exit

檔案名稱:Sqlite3EnvSetting.bat
檔案內容:
@echo off
REM :: BatchGotAdmin (Run as Admin code starts)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
REM :: BatchGotAdmin (Run as Admin code ends)
REM :: Your codes should start from the following line
set PythonPath=%PATH%%WinDir%\sqlite3\;
echo %PythonPath%
setx /m PATH "%PythonPath%"
echo %PATH%
echo 完成系統環境變數設定 
timeout /t 2
exit

檔案名稱:UrlPath.txt
檔案內容:
https://www.sqlite.org/2022/sqlite-dll-win64-x64-3380500.zip
https://www.sqlite.org/2022/sqlite-tools-win32-x86-3380500.zip



第一版:

檔案名稱:Python390Sqlite3Install.bat
檔案內容:
@echo off
if not exist %HomeDrive%%HomePath%\AppData\Local\Programs\Python\Python39 (
 start https://skjhcreator.blogspot.com/2022/05/batpython-390.html
 echo "電腦內尚未安裝Python 3.9.0。請前往下載"
 pause
) else (
echo "電腦內已安裝程式,不需要安裝 Pyhon 3.9.0"
echo "安裝套件 wget"
python -m pip install wget
cd %~dp0program\
python -m Sqlite3DownloadUnzip.py
timeout /t 5
call MoveSystem.bat
timeout /t 2
call Sqlite3EnvSetting.bat
timeout /t 2
)
exit

檔案名稱:Sqlite3DownloadUnzip.py
檔案內容:
import os
import wget
from zipfile import ZipFile
import shutil
# Sqlite3 下載
StopDownloadSet = set()
ReadFile = []
FileName = []
UrlPath = []
DownloadsFileName = []
DownloadsFilePath = os.getcwd()+'\\Downloads\\'
DownloadsFileName = os.listdir(DownloadsFilePath)
DownloadsFileNameSet = set(DownloadsFileName)
OpenFile = open('Urlpath.txt','r')
ReadFile = OpenFile.readlines()
for i in ReadFile:
    i = i.replace('\n','')
    UrlPath.append(i)
    temp = i.split('/')
    temp = temp[-1].replace('\n','')
    FileName.append(temp)
OpenFile.close()
FileNameSet = set(FileName)
#  檢查Downloads資料夾檔案名稱與UrlPath檔案內檔案名稱,是否有同名。
StopDownloadSet = FileNameSet.difference(DownloadsFileNameSet)
DownloadsFileName = []
for i in StopDownloadSet:
    DownloadsFileName.append(i)
#  如果同名,就不下載。
for i in range(0,len(DownloadsFileName)):
    print("Download "+DownloadsFileName[i]+". Start!")
    wget.download(UrlPath[i],str(DownloadsFilePath+DownloadsFileName[i]))
    print("Download "+DownloadsFileName[i]+". OK!")
# 檢查Downloads資料夾內的壓縮檔,並解壓縮到temp資料夾
DownloadsFileName = []
DownloadsFilePath = os.getcwd()+'\\Downloads\\'
DownloadsFileName = os.listdir(DownloadsFilePath)
for i in DownloadsFileName:
    i = DownloadsFilePath+i
    with ZipFile(i,'r') as zip:
        zip.printdir()
        zip.extractall('temp')
# 將temp資料夾內的檔案更名並移動到temp\sqlite3
MoveFiles = []
MoveFilePath = os.getcwd()+'\\temp\\'        
MoveFiles = os.listdir(MoveFilePath)
MoveDir = MoveFiles[0]
RenamedFile01 = MoveFiles[1].replace(".def","")
del MoveFiles[0]
for file in MoveFiles:
    shutil.move(MoveFilePath+file,MoveFilePath+MoveDir)
os.rename(MoveFilePath+MoveDir,MoveFilePath+RenamedFile01)


檔案名稱:MoveSystem.bat
檔案內容:
@echo off
REM :: BatchGotAdmin (Run as Admin code starts)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
REM :: BatchGotAdmin (Run as Admin code ends)
REM :: Your codes should start from the following line
move %~dp0temp/sqlite3 %WinDir%
timeout /t 2
exit

檔案名稱:Sqlite3EnvSetting.bat
檔案內容:
@echo off
REM :: BatchGotAdmin (Run as Admin code starts)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
REM :: BatchGotAdmin (Run as Admin code ends)
REM :: Your codes should start from the following line
set PythonPath=%PATH%%WinDir%\sqlite3\;
echo %PythonPath%
setx /m PATH "%PythonPath%"
echo %PATH%
echo 完成系統環境變數設定 
timeout /t 2
exit

檔案名稱:UrlPath.txt
檔案內容:
https://www.sqlite.org/2022/sqlite-dll-win64-x64-3380500.zip
https://www.sqlite.org/2022/sqlite-tools-win32-x86-3380500.zip

        



資料來源:

沒有留言:

張貼留言

Laravel 12 Model 資料庫中的資料表,並提供與資料庫互動的介面

相關系列文章: 1. 在 windows 10 安裝 laravel 12 studentManagement環境與設定 2. laravel 12 route 路由 3. laravel 12 Blade Templates 網頁模版 4. laravel 12 Control...