標籤

bat (52) 作品 (38) python (21) shell (17) windows (11) 虛擬機 (11) php (10) CPP (6) KMS (6) 程式設計 (6) docker (5) 使用教學 (5) xoops (4) 公文 (4) Apache2 (3) Excel (3) juniper (3) 資料庫 (3) mysql (2) 免動手 (2) 資料結構 (2) 軟體廣播 (2) 電腦維修 (2) Android Studio (1) Apple IPAD管理 (1) Arduino (1) CSS (1) LAMP (1) NAS (1) Ubuntu (1) VHD (1) Windows Server (1) 原因 (1) 程式應用 (1) 程式積木 (1) 編輯器 (1) 雲端硬碟 (1)

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

        



資料來源:

沒有留言:

張貼留言

只要點兩下,傳統右鍵選單改回Win11右鍵選單

系列文章: 1. 只要點兩下,就能將Win11 右鍵選單 回復 傳統右鍵選單 2. 只要點兩下,傳統右鍵選單改回Win11右鍵選單 上一篇提到只要點兩下,就能將Win11選單回到傳統選單。但是有沒有方法能夠回到Win11選單呢?                    ...