Windows 11下使用7-Zip压缩指定文件并移动替换的脚本无响应问题求助
Windows 11下使用7-Zip压缩指定文件并移动替换的脚本无响应问题求助
我遇到了一个脚本执行的问题,希望能得到大家的帮助。先说明一下我的目录结构:
- 父文件夹:
Project Project的子文件夹:data、rawDatarawData里包含:index.html、settings.html、user.html、script.js、style.css以及其他文件
我的需求是写一个Windows 11的脚本,仅将上述列出的文件gzip压缩,然后从rawData移动并替换到data文件夹中。
我尝试了两种脚本,但都没有任何反应:
尝试的批处理脚本
@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目录下确实有7-Zip的程序,但两个脚本运行后都没有任何效果——既没有压缩文件,也没有执行移动操作,甚至连报错提示都没有。有没有大佬能帮我排查一下问题出在哪?
备注:内容来源于stack exchange,提问作者George Florian
相关产品推荐
相关产品推荐

