windows cmd拉取linux文件夾下的文件,並解壓

前言:nginx靜態文件從linux文件夾下拉取,然後放到windows下,並且解壓

 需要安裝 putty,用pscp命令

del-pull.bat文件,負責刪除本地文件夾下所有文件,並且拉取數據

@echo off
setlocal

set FOLDER_PATH=C:\Users\admin\Desktop\yaya_nginx\web\

echo Deleting files in folder...
rmdir /s /q %FOLDER_PATH%

echo -----Files deleted successfully---
echo  password:---------yaya@197-----------

pscp -r root@8.8.11.112:"/root/home/web" "C:\Users\admin\Desktop\yaya_nginx"

endlocal

 

powershell腳本文件:負責解壓文件

unzip.ps1

# 設置源目錄路徑  
$sourceDir = "C:\Users\admin\Desktop\yaya_nginx\web"  
  
# 獲取源目錄下的所有子目錄  
$subDirs = Get-ChildItem -Path $sourceDir -Directory  
  
# 遍歷每個子目錄  
foreach ($dir in $subDirs) {  
    # 構造dist.zip文件的完整路徑  
    $zipPath = Join-Path -Path $dir.FullName -ChildPath "dist.zip"  
      
    # 如果dist.zip文件存在  
    if (Test-Path -Path $zipPath) {  
        # 構造dist目錄的完整路徑  
        $distPath = Join-Path -Path $dir.FullName -ChildPath "dist"  
          
        # 如果dist目錄不存在,則創建它  
        if (!(Test-Path -Path $distPath -PathType Container)) {  
            New-Item -ItemType Directory -Path $distPath | Out-Null  
        }  
          
        # 使用PowerShell的Expand-Archive命令解壓文件  
        Expand-Archive -Path $zipPath -DestinationPath $distPath -Force  
    } else {  
        # 如果dist.zip文件不存在,則輸出警告信息  
        Write-Warning "dist.zip not found in $($dir.FullName)"  
    }  
}

 

如果都用powershell腳本,完整命令:

# 設置文件夾路徑
$FOLDER_PATH = "C:\Users\admin\Desktop\yaya_nginx\web\"

# 輸出刪除文件夾中的文件信息
Write-Host "--------------------------------Deleting files in folder...-------------------------------------"

# 嘗試刪除文件夾及其內容(PowerShell中沒有rmdir命令,使用Remove-Item)
# 注意:Remove-Item -Recurse -Force 會刪除文件夾及其所有內容,請小心使用
# 如果你只想清空文件夾而不刪除它,請使用不同的方法
try {
    Remove-Item -Recurse -Force $FOLDER_PATH -ErrorAction Stop
    Write-Host "-----Files deleted successfully---"
}
catch {
    Write-Host "Error deleting folder: $_"
}

Write-Host "-------------------------pull  linux files password: yaya@1972 ------------------------------------------"

# 設置父文件夾路徑(如果需要刪除web文件夾後再使用父文件夾路徑)
$FOLDER_PATH_PARENT = Split-Path -Parent $FOLDER_PATH

# 注意:這裏我們假設不刪除web文件夾,所以直接使用$FOLDER_PATH

# 使用pscp(PuTTY Secure Copy)命令從遠程服務器複製文件夾
# 注意:PowerShell沒有內建的pscp命令,你需要確保pscp.exe在你的PATH中,或者指定完整路徑
try {
    # PowerShell中使用&來執行外部命令
    # 使用$FOLDER_PATH(如果web文件夾存在並且你想複製到這裏)
    & pscp -r root@8.8.11.112:"/root/home/web" $FOLDER_PATH
    Write-Host "Files copied from remote server successfully"
}
catch {
    Write-Host "Error copying files from remote server: $_"
}

Write-Host "-------------------------pull  linux files end ------------------------------------------"

Write-Host "-------------------------files unzip ------------------------------------------"
# 設置源目錄路徑
$sourceDir = $FOLDER_PATH

# 獲取源目錄下的所有子目錄
$subDirs = Get-ChildItem -Path $sourceDir -Directory

# 遍歷每個子目錄
foreach ($dir in $subDirs) {
    # 構造dist.zip文件的完整路徑
    $zipPath = Join-Path -Path $dir.FullName -ChildPath "dist.zip"

    # 如果dist.zip文件存在
    if (Test-Path -Path $zipPath) {
        # 構造dist目錄的完整路徑
        $distPath = Join-Path -Path $dir.FullName -ChildPath "dist"

        # 如果dist目錄不存在,則創建它
        if (!(Test-Path -Path $distPath -PathType Container)) {
            New-Item -ItemType Directory -Path $distPath | Out-Null
        }

        # 使用PowerShell的Expand-Archive命令解壓文件
        Expand-Archive -Path $zipPath -DestinationPath $distPath -Force
    } else {
        # 如果dist.zip文件不存在,則輸出警告信息
        Write-Warning "dist.zip not found in $($dir.FullName)"
    }
}
Write-Host "-------------------------files unzip  end ------------------------------------------"

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章