为 Windows 11 创建一个脚本,使用 7-zip 对一些文件进行 gzip 压缩,然后将它们移动并替换到另一个文件夹中

为 Windows 11 创建一个脚本,使用 7-zip 对一些文件进行 gzip 压缩,然后将它们移动并替换到另一个文件夹中

我有以下目录结构:父文件夹:项目项目的子文件夹:数据,原始数据

rawData 里面我有:index.html、settings.html、user.html、script.js、style.css 和其他文件。

我需要为 Windows 11 编写一个脚本,仅“gzip”上面提到的文件,并将它们从 移动并替换rawDatadata

我尝试了以下方法:

一个蝙蝠脚本:

@echo off
setlocal enabledelayedexpansion

rem Assuming the script is located inside the "rawData" folder
set "DataPath=..\data"

for %%f in (index.html settings.html user.html script.js style.css) do (
    set "SourceFile=%%f"
    set "DestinationFile=!DataPath!\%%f"
    set "GzippedFile=!DataPath!\%%f.gz"

    if exist "!SourceFile!" (
        echo Gzipping !SourceFile!...
        "C:\Program Files\7-Zip\7z.exe" a -tgzip "!GzippedFile!" "!SourceFile!" > nul
        
        if exist "!GzippedFile!" (
            echo Moving !GzippedFile! to !DestinationFile!...
            move /y "!GzippedFile!" "!DestinationFile!"
        ) else (
            echo Error gzipping !SourceFile!
        )
    ) else (
        echo !SourceFile! not found.
    )
)

echo Script completed.
exit /b 0

PowerShell 脚本:

# Set the paths relative to the script's location
$ScriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path
$RawDataPath = Join-Path -Path $ScriptDirectory -ChildPath "..\rawData"
$DataPath = Join-Path -Path $ScriptDirectory -ChildPath "..\data"

# List of files to gzip and move
$FilesToProcess = @("index.html", "settings.html", "user.html", "script.js", "style.css")

# Specify the path to 7-Zip executable
$SevenZipExe = "C:\Program Files\7-Zip\7z.exe"

# Loop through the files
foreach ($file in $FilesToProcess) {
    $SourceFile = Join-Path -Path $RawDataPath -ChildPath $file
    $DestinationFile = Join-Path -Path $DataPath -ChildPath $file
    $GzippedFile = Join-Path -Path $DataPath -ChildPath "$file.gz"

    if (Test-Path $SourceFile) {
        Write-Host "Gzipping $SourceFile..."
        & $SevenZipExe a -tgzip "$GzippedFile" "$SourceFile"
        Copy-Item -Path $GzippedFile -Destination $DestinationFile -Force
        Remove-Item -Path $GzippedFile -Force
    } else {
        Write-Host "$SourceFile not found."
    }
}

Write-Host "Script completed."

这就是我所拥有的C:\Program Files\7-Zip

两个脚本都不起作用。

相关内容