放到和要压缩的子目录同文件夹下,执行,注意你的RAR目录复制代码
- @echo off
- set winrar="c:\Program Files\WinRAR\winrar.exe"
- set t=%date:~0,4%%date:~5,2%%date:~8,2%
- for /f "delims=" %%i in ('dir/ad/b') do (
- %winrar% a -r -m5 "%%i-%t%.rar" "%%i"
- )
- pause
复制代码
- @echo off
- >nul chcp 936
- setlocal enableDelayedExpansion
- set "ftype=zip"
- REM 压缩比 0~5 可选, 0 最低, 5 最高
- set "mmode=5"
- REM 密码
- set "pwd=mypassword"
- REM 对目录压缩
- set "r_param=-r"
- cd /d "%~dp0"
- REM 生成随机标记串
- set "neorand=neorand_"
- for /l %%a in (1 1 5) do (
- set "neorand=!neorand!!random:~-1!"
- )
- echo;随机标记串
- set neorand
- REM 搜索出 winrar 的全路径
- set "filename=winrar.exe"
- call :searchFile winrarFullname
- echo;winrar 的全路径
- echo;winrarFullname={!winrarFullname!}
- for /f "delims=" %%A in ('dir /ad /b') do (
- echo;%%A
- REM echo;"%winrarFullname%" a -ag-YYYYMMDD_HHMMSS -m%mmode% -af%ftype% "%%~dpA!neorand!%%~nxA.%ftype%" -p%pwd% %r_param% -ep1 "%%~A"
- "%winrarFullname%" a -ag-YYYYMMDD_HHMMSS -m%mmode% -af%ftype% "%%~dpA!neorand!%%~nxA.%ftype%" -p%pwd% %r_param% -ep1 "%%~A"
- REM 生成注释文件
- > "%~dp0rarcomment.txt" (
- echo;"%%~nxA.%ftype%" 生成于 %date%_%time%
- echo;pwd
- echo;!pwd!
- )
- REM 找出含有 随机标记串 前缀 日期时间 后缀 的压缩文件名
- set "arcname="
- for /f "delims=" %%a in ('dir /a-d /b "%%~dpA!neorand!%%~nxA*.%ftype%"') do (
- if "!arcname!"=="" (
- set "arcname=%%~fa"
- )
- )
- REM echo;含有 随机标记串 前缀 日期时间 后缀 的压缩文件名
- set arcname
- REM 添加注释
- "%winrarFullname%" c -z"%~dp0rarcomment.txt" "!arcname!"
- del "%~dp0rarcomment.txt"
- REM 去掉 随机标记串
- for %%a in ("!arcname!") do (
- echo;%%~nxa
- set "fn=%%~nxa"
- set "fn=!fn:~13!"
- )
- ren "!arcname!" "!fn!"
- )
- title 压缩包已经生成, 按任意键退出...
- <nul set /p "=压缩包已经生成, 按任意键退出..."
- if exist "%windir%\System32\timeout.exe" (
- timeout /t 10
- ) else (
- >nul pause
- )
- exit
- :searchFile winrarFullname
- REM ProgramData=C:\ProgramData
- REM ProgramFiles=C:\Program Files
- REM ProgramFiles(x86)=C:\Program Files (x86)
- REM ProgramW6432=C:\Program Files
- setlocal enableDelayedExpansion
- set "rt="
- for %%d in (C D E) do (
- if "!rt!"=="" (
- for %%V in ("ProgramFiles" "ProgramFiles(x86)" "ProgramData" "ProgramW6432") do (
- if "!rt!"=="" if "!%%~V!" neq "" (
- for /f "delims=" %%p in ('dir "%%d!%%~V:~1!\!filename!" /a /s /b') do (
- if "!rt!"=="" if /i "%%~nxp"=="!filename!" (
- set "rt=%%~p"
- )
- )
- )
- )
- )
- )
- if "!rt!"=="" (
- for %%d in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
- if "!rt!"=="" (
- for /f "delims=" %%p in ('dir %%d:\"!filename!" /a /s /b') do (
- if "!rt!"=="" if /i "%%~nxp"=="!filename!" (
- set "rt=%%~p"
- )
- )
- )
- )
- )
- ( ENDLOCAL & REM RETURN VALUES
- IF "%~1" NEQ "" (SET %~1=%rt%) ELSE (echo;%rt%)
- exit /b
- )
- exit /b
欢迎光临 批处理之家 (http://bbs.bathome.net/) | Powered by Discuz! 7.2 |