for / D %% A in()do“C:\ Program Files \ 7-Zip \ 7z.exe”a -tzip“%% A.zip”-xr! .bat “%% A”-r -x!*。xls
以上批次会自动压缩目录中的文件..虽然压缩我想以格式(YYYYMMDD.HHMMSS)附加日期和时间。 例如,假设文件夹名称为“University”。如果我运行批处理文件,最终的zip名称应该创建为“University_YYYYMMDD.HHMMSS.zip”..
答案 0 :(得分:8)
此代码将为您提供XP Pro及更高版本中可靠的YY DD MM YYYY HH Min Sec变量。
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "fullstamp=%YYYY%%MM%%DD%.%HH%%Min%%Sec%"
for /D %%A in (*) do "C:\Program Files\7-Zip\7z.exe" a -tzip "%%A_%fullstamp%.zip" -xr!.bat "%%A" -r -x!*.xls
答案 1 :(得分:0)
以下是构建字符串的解决方案:
set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
echo hour=%hour%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%
echo min=%min%
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
echo secs=%secs%
set year=%date:~-4%
echo year=%year%
set month=%date:~3,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%
echo month=%month%
set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%
echo day=%day%
set datetimef=%year%%month%%day%.%hour%%min%%secs%
echo datetimef=%datetimef%
因此,您可以将变量%datetimef%与输出文件的名称连接起来:
for /D %%A in () do "C:\Program Files\7-Zip\7z.exe" a -tzip "%datetimef%_%%A.zip" -xr!.bat "%%A" -r -x!*.xls