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 ------------------------------------------"

 

posted @ 2024-05-21 10:11  _Phoenix  阅读(23)  评论(0编辑  收藏  举报