我在Windows 7中使用7-zip,并试图在文件夹中为自动zip文件创建批处理。
我希望将文件夹中的每个文件分别压缩。
您能看一下我的代码并提出最佳建议吗?
我成功地将所有内容从一个文件夹压缩到另一个文件夹,但还需要分别处理每个文件:
@ECHO OFF
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%
Set TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%
ECHO.
ECHO Compressing files and folders in C:\zipush drive and moving to C:\new
ECHO.
7za a -tzip "C:\new-drive-%TODAY%.zip" "C:\zipush*" -mx5
ECHO.
ECHO Delete the files in orginal folder
DEL "C:\zipush\*.*"
PAUSE
以下是您需要完成的循环,以使用的命名约定将每个文件压缩到其自己的zip文件<orignalfilename>.<extension>.zip
,然后如果先前文件的zip成功,则删除原始文件。如果需要,您可以更改%%~NXA.zip
为%%~NA.zip
在新的zip文件中不包含原始文件扩展名。
您还应该确保创建目标目录(如果尚不存在),以便归档命令在循环中按预期方式工作,因此我将其添加到脚本逻辑中,并且还将源目录和目标目录设置为变量。
我还通过FOR循环使用了批量替换来处理各个zip和文件名。从有关该主题的一些学习资料中查看更多资源(FOR /?
)。
脚本示例
@ECHO OFF
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%
SET TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%
SET SrcDir=C:\zipush
SET DestDir=C:\new-drive-%TODAY%
IF NOT EXIST "%DestDir%" MD "%DestDir%"
ECHO.
ECHO Compressing files and folders in C:\zipush drive and moving to C:\new
ECHO.
ECHO Compressing files and folders in C:\zipush drive and moving to C:\new and then delete from C:\zipush
ECHO.
FOR %%A IN ("%SrcDir%\*.*") DO 7za a -tzip "%DestDir%\%%~NXA.zip" "%%~A" -mx5 && DEL /Q /F "%%~A"
ECHO.
PAUSE
FOR /?
从命令提示符
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句