You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows11下批处理失效,求等效PowerShell图片压缩迁移脚本

问题:将Windows 10批处理脚本转换为等效的Windows 11 PowerShell脚本

我有一台设备,用于压缩多位用户从Dropbox上传的图片,之后将其发送至产生流量成本的云存储。原先的批处理脚本在Windows 10任务计划程序中运行正常,但导出到Windows 11后无法工作。我尝试将其转换为PowerShell脚本,但遇到了RoboCopy功能适配的问题。

该脚本核心功能:清空源文件夹中的文件但保留文件夹结构,方便员工后续正确上传更多图片,同时包含清理操作以保证电脑运行顺畅。脚本需每日多次运行,其中2jpeg.exe为图片压缩工具。


原批处理脚本

set "source=C:\kilde1"

set "destination=C:\kilde2"

Set "resize=C:\kilde3"

Set "move=S:\kilde4"

robocopy "%source%" "%destination%" /mov /E /XD .*

del /s /f /q /a:h "%destination%"

robocopy "%destination%" "%destination%" /S /move

2jpeg.exe -src "%destination%\*.*" -dst "%resize%" -oper Resize size:"1920 1080" -options overwrite:yes -options delsrc:yes

robocopy "%destination%" "%resize%" /mov /E /XD .*

robocopy "%resize%" "%move%" /move /E

del /s /f /q /a:h "%move%"

robocopy "%move%" "%move%" /S /move

exit /b

我编写的初始PowerShell代码(丢失文件夹结构)

#Original picture folder
$source = "C:\Kilde1"

#Folder to work with files
$destination = "C:\Kilde2"

#Folder for reducing for 2jpeg - pictures are reducet
$reseize = "C:\Kilde3"

#Folder final store
$move = "S:\Kilde4"

#Move Pictures from Source folder to destination
Get-ChildItem $source | Move-Item -Destination $destination 

等效功能的PowerShell脚本

# 定义路径
$source = "C:\kilde1"
$destination = "C:\kilde2"
$resize = "C:\kilde3"
$move = "S:\kilde4"

# 1. 移动源文件夹文件到目标文件夹,保留目录结构,排除隐藏目录
robocopy $source $destination /mov /E /XD .*

# 2. 删除目标文件夹中的隐藏文件
Get-ChildItem -Path $destination -File -Hidden -Recurse | Remove-Item -Force -ErrorAction SilentlyContinue

# 3. 整理目标文件夹:将子目录文件移到对应父级,删除空目录(模拟robocopy /S /move)
Get-ChildItem -Path $destination -File -Recurse | ForEach-Object {
    $targetPath = Join-Path -Path $destination -ChildPath $_.Name
    if (-not (Test-Path $targetPath)) {
        Move-Item -Path $_.FullName -Destination $targetPath -Force
    }
}
# 删除空目录
Get-ChildItem -Path $destination -Directory -Recurse | Where-Object { (Get-ChildItem $_.FullName -Force).Count -eq 0 } | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

# 4. 调用2jpeg压缩图片,压缩后删除源文件
& "2jpeg.exe" -src "$destination\*.*" -dst $resize -oper Resize size:"1920 1080" -options overwrite:yes -options delsrc:yes

# 5. 移动目标文件夹剩余文件到resize目录,保留结构
robocopy $destination $resize /mov /E /XD .*

# 6. 移动resize目录文件到最终存储目录,保留结构
robocopy $resize $move /move /E

# 7. 删除最终存储目录的隐藏文件
Get-ChildItem -Path $move -File -Hidden -Recurse | Remove-Item -Force -ErrorAction SilentlyContinue

# 8. 整理最终存储目录:子目录文件移到父级,删除空目录
Get-ChildItem -Path $move -File -Recurse | ForEach-Object {
    $targetPath = Join-Path -Path $move -ChildPath $_.Name
    if (-not (Test-Path $targetPath)) {
        Move-Item -Path $_.FullName -Destination $targetPath -Force
    }
}
# 删除空目录
Get-ChildItem -Path $move -Directory -Recurse | Where-Object { (Get-ChildItem $_.FullName -Force).Count -eq 0 } | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

脚本说明

  • 完整保留原批处理的核心逻辑,包括robocopy的移动规则、隐藏文件清理、目录整理、2jpeg调用流程
  • 用PowerShell命令替代部分批处理操作,同时保留robocopy以确保目录结构的正确处理
  • 添加-ErrorAction SilentlyContinue避免因空目录/文件不存在导致脚本中断
  • 确保源文件夹仅清空文件,保留完整目录结构,不影响后续员工上传操作

内容的提问来源于stack exchange,提问作者4Brigade-DK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 12:56:12