使用 7zip 命令行批处理时如何排除文件扩展名

How to exclude file extension when using 7zip cmd line batch

我在 Windows 上的“发送到”文件夹中有一个 .batch 脚本,我用它来将文件夹中的多个文件压缩为 .zip。我遇到的问题是,它在存档文件名中包含扩展名。

我在 google 上搜索,阅读了很多关于 stackexchange 等的帖子,none 发布的解决方案似乎对我有用。

@echo off
for %%A in (*.*) do call :doit "%%A"
goto end

:doit
if "%~x1"==".bat" goto :eof
if "%~x1"==".7z" goto :eof
if "%~x1"==".zip" goto :eof
if "%~x1"==".rar" goto :eof
"C:\Program Files-Zipz.exe" a -tzip %1.zip %1 -sdel
goto :eof

:end

Myfile.txt -> MyFile.txt.zip

如果可能,我想从文件名中删除 .txt。

您可以使用 %~n1,它仅使用第一个参数的文件名。这是一个可能的解决方案:

@echo off
for %%A in (*.*) do (call:doit "%%A")
goto end

:doit
if "%~x1" NEQ ".bat" (
    if "%~x1" NEQ ".7z" (
        if "%~x1" NEQ ".zip" (
            if "%~x1" NEQ ".rar" (
                "C:\Program Files-Zipz.exe" a -tzip %~n1.zip %~n1 -sdel

:end
exit /b 0

我不确定你的压缩器程序的语法,因为你接受的答案看起来是错误的,事实上空格不受保护而且 %~n1 可能丢失它是扩展名.

您或许可以在一行中完成此操作:

@For /F "Delims=" %%A In ('Dir /B/A-D-S-L "%~1"^|FindStr /IVE "\.7z \.bat \.rar \.zip"') Do @"%ProgramFiles%-Zipz.exe" a -tzip "%%~nA.zip" "%%~A" -sdel