批处理:大于的文件,小于的文件

时间:2018-10-31 13:33:55

标签: windows batch-file 7zip

我正在寻找创建一个批处理脚本,该脚本将获取所有大于x的图像文件,并将它们压缩为一个.zip。 然后获取所有小于x大小的剩余图像文件,并将它们压缩为一个.zip

我尝试了几种不同的方式,无法解决。我在这里搜索过,所有强大的Google大声笑 我的最新想法是暂时列出文本文件中较大的文件,然后使用该列表进行7zip压缩,但无法弄清楚如何将它们回显/打印/列出为7zip。

@ECHO OFF
SETLOCAL EnableDelayedExpansion

for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
for /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)

SET FOLDER=%~dp1
REM     Sets current directory

SET LOG="log.txt"

pushd %FOLDER%
REM     Changes directory to current

for %%a in ("%cd%") do SET NAME=%%~na
REM     Gets the last directory name and sets it as a variable

SET ZIPNAME=%NAME% - Originals.zip
SET ZIPNAME2=%NAME% - Photos.zip

if exist ".smaller.txt" del /F ".smaller.txt"
if exist ".larger.txt" del /F ".larger.txt"

if exist "%ZIPNAME%" del /F "%ZIPNAME%" | echo %MYDATE%_%MYTIME%: %COMPUTERNAME% deleted %ZIPNAME%>>%LOG%
if exist "%ZIPNAME2%" del /F "%ZIPNAME2%" | echo %MYDATE%_%MYTIME%: %COMPUTERNAME% deleted %ZIPNAME2%>>%LOG%
REM     Deletes old zips if they exists

FOR /F "usebackq delims=;" %%A IN (`dir /b *.jpg *.jpeg *.png *.gif *.bmp`) DO (

    IF %%~zA LSS 1048576 (
        ECHO %%A >>.smaller.txt
    ) ELSE (
        ECHO %%A >>.larger.txt
    )

)

FOR /F "tokens=* delims=" %%x IN (.smaller.txt) DO SET SMALLER=!SMALLER!%%x
FOR /F "tokens=* delims=" %%x IN (.larger.txt) DO SET LARGER=!LARGER!%%x

PAUSE

2 个答案:

答案 0 :(得分:1)

这是我的最终产品。 感谢您的所有帮助!

@ECHO OFF
SETLOCAL EnableDelayedExpansion

FOR /f "tokens=2-4 delims=/ " %%a IN ('date /t') DO (SET mydate=%%c-%%a-%%b)
FOR /f "tokens=1-2 delims=/:" %%a IN ("%TIME%") DO (SET mytime=%%a%%b)
REM     My timestamp

SET FOLDER=%~dp1
REM     Sets current directory

IF NOT EXIST %SYSTEMDRIVE%\Logs MKDIR %SYSTEMDRIVE%\Logs
SET LOG="%SYSTEMDRIVE%\Logs\.picture_log.txt"
REM     Logging file and directory

PUSHD %FOLDER%
REM     Changes directory to current

FOR %%A IN ("%CD%") DO SET NAME=%%~NA
REM     Gets the last directory name and sets it as a variable

SET ZIP_O=%NAME% - Originals.zip
SET ZIP_R=%NAME% - Photos.zip
REM     Zip file names

IF EXIST ".larger.txt" DEL /F ".larger.txt"
IF EXIST ".smaller.txt" DEL /F ".smaller.txt"
REM     Delete old temporary files

IF EXIST "%ZIP_O%" DEL /F "%ZIP_O%" | ECHO %MYDATE%_%MYTIME%: %COMPUTERNAME% deleted %ZIP_O%>>%LOG%
IF EXIST "%ZIP_R%" DEL /F "%ZIP_R%" | ECHO %MYDATE%_%MYTIME%: %COMPUTERNAME% deleted %ZIP_R%>>%LOG%
REM     Deletes old zips if they exists

FOR /F "usebackq delims=;" %%A IN (`dir /b *.jpg *.jpeg *.png *.gif *.bmp`) DO (
    IF %%~zA LSS 1048576 (
        ECHO %%A >>.smaller.txt
    ) ELSE (
        ECHO %%A >>.larger.txt
    )
)
REM     Generate list of files, based off size

FOR /F "tokens=* delims=" %%x IN (.larger.txt) DO SET LARGER=!LARGER!%%x
FOR /F "tokens=* delims=" %%x IN (.smaller.txt) DO SET SMALLER=!SMALLER!%%x
REM     Read lists, put contents into variables

"C:\Program Files\7-Zip\7z" a -mx9 -tzip "%ZIP_O%" @.larger.txt -sdel | ECHO %MYDATE%_%MYTIME%: %COMPUTERNAME% compressed %ZIP_O%>>%LOG%
"C:\Program Files\7-Zip\7z" a -mx9 -tzip "%ZIP_R%" @.smaller.txt -sdel | ECHO %MYDATE%_%MYTIME%: %COMPUTERNAME% compressed %ZIP_R%>>%LOG%
REM     Compress images in separate .zips

IF EXIST ".larger.txt" DEL /F ".larger.txt"
IF EXIST ".smaller.txt" DEL /F ".smaller.txt"
REM     Delete temporary files

答案 1 :(得分:0)

forfiles功能具有@fsize属性,您可以检查。

对于每个足够大的文件,您执行7z.exe a(添加到存档)。