关于批量压缩(含有特殊字符)?

请问一下各位大能大佬批量是有什幺问题吗…怎幺不能压缩下面文档…
压出来都只有1KB
小弟猜测是因为 & 的關係

ABC_&__v1.2.3_b123_arm7+arm64.apk

以下是小弟的批量能帮我修改一下能正常压缩上面文档名

@echo off
set var2=123 
chcp 65001
set "ZIP=D:\TOOLS\NoSetup\7-zip x64\7z.exe"

:loop
if exist "%~1\" (
    "%ZIP%" a -t7z "%~n1.7z" "%~1\*" -scsUTF-8 -p"%var2%" -mhe=on -mx0 -xr!desktop.ini -xr!Thumb.db
) else (
  "%ZIP%" a -t7z "%~n1.7z" "%~1" -scsUTF-8 -p"%var2%" -mhe=on -mx0
)
shift
if not "%~1" == "" goto loop

先谢谢各位大佬了

P.S.对了…因为批量是放在sendto里…所以能大量个别压缩文档

我暂时只看到了一个引入延迟变量的办法可以正确获取路径。但我太菜了,这样引入延迟变量以后,参数里的-xr!这里的感叹号会被认为也是延迟变量,没想到怎么解决。 我知道了,用两个^^就可以了,我还以为一个就行。

以下是我的代码,请自己修改zip路径和密码:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set "ZIP=C:\Program Files\7-Zip\7z.exe"
set pwd=123
chcp 65001
set "params=!cmdcmdline:~0,-1!"
set "params=!params:*" =!"
set count=0
for %%G IN (!params!) do (
  set /a count+=1
  set "item_!count!="%%~G""
)
for /L %%n in (1,1,!count!) DO (
  set "source=!item_%%n:~1,-1!"
  for %%A in ("!source!") do set "filename=%%~nA"
  set "archive=!filename!.7z"
  if exist "!source!\" (
    "%ZIP%" a -t7z "!archive!" "!source!\*" -scsUTF-8 -p"%pwd%" -mhe=on -mx0 -xr^^!desktop.ini -xr^^!Thumb.db
  ) else if exist "!source!" (
    "%ZIP%" a -t7z "!archive!" "!source!" -scsUTF-8 -p"%pwd%" -mhe=on -mx0
  ) else (
    echo !source! does not exist.
	pause
	exit
  )
)
endlocal
exit

参考资料:
1、dragdrop file named with ampersand problem
2、Why ^^! and not ^! for literal escapes during delayed variable expansion?

1 个赞

非常感谢大佬您了…您是我的神…牛阿… :+1:
可以了…没有问题…真的非常感谢…小众大神众多… :100: