我实际上对这个批处理文件很新。我知道至少知道基本命令很重要。我该如何做?
PS :7-Zip作为存档软件安装在我的计算机上。
答案 0 :(得分:4)
下面列出的批处理脚本将执行此操作(我已对其进行了测试 它适用于规格)。 zip和的目录 move被指定为其父目录和名称 目录(PARENT_FOLDERTOZIP和FOLDERTOZIP中的 开始) - 我无法弄清楚如何复制整个 目录(我认为XCOPY只能复制内容 目录和子目录。)。复制位置是 指定为FOLDERTARGETLOCATION,以及要放置的目录 压缩文件中指定为ZIPDIR。
7-Zip的位置是通过SEVENZIP_EXE配置的。
另请注意,获取所需的当前日期 格式取决于区域中的短日期格式 设置。我列出了三个不同的版本 ISO-8601,中欧和美国。活跃的一个 列表是针对U.S.A.(“设置FDATE =”行)。如果一个 需要不同的一个然后只需从其中一个复制粘贴 另外两个。
那说应该注意这种事情很多 Perl,Python或PowerShell更容易。
@echo off
@title=Folder zip and move...
rem Parameters
rem Folder to zip and move
set PARENT_FOLDERTOZIP=T:\to delete
set FOLDERTOZIP=Folder to Compress
rem Target folder for moving the input folder to.
set FOLDERTARGETLOCATION=s:\move Here
rem Where to place compressed folders
set ZIPDIR=D:\toDelete\2009-09-24a
rem Configuration
set SEVENZIP_EXE=D:\Program Files\7-Zip\7z.exe
rem =================== Date ==============================================
rem There is no universal way inside batch itself to get a
rem date that is independent of regional settings (but is
rem quite trivial if an external program or script
rem (Perl/Python) is available).
rem
rem For short date formats:
rem
rem -------------------------------------------------------
rem
rem ISO-8601:
rem 0123456789
rem yyyy-MM-dd/ E.g.: 2009-09-24
rem
rem set FDATE=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%
rem
rem -------------------------------------------------------
rem
rem Central european:
rem 0123456789
rem dd/MM/yyyy E.g.: 24/09/2009
rem
rem set FDATE=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%
rem
rem -------------------------------------------------------
rem
rem US:
rem
rem 0123456789
rem MM/dd/yyyy E.g.: 09/24/2009
rem
rem set FDATE=%DATE:~6,4%%DATE:~0,2%%DATE:~3,2%
set FDATE=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%
set ZIPFILE=%ZIPDIR%\%FDATE%.7z
set FOLDERTOZIP_FULLPATH=%PARENT_FOLDERTOZIP%\%FOLDERTOZIP%
mkdir %FOLDERTARGETLOCATION%
rem Does a zip file already exist?
if exist "%ZIPFILE%" GOTO L_ZIPFILE_EXISTS
GOTO L_ZIPFILENAME_OK
rem Find a compressed file that does not already exist.
:L_ZIPFILE_EXISTS
set RNUM=0
:L_TRYANOTHER
set /a RNUM=%RNUM% + 1
set ZIPFILE=%ZIPDIR%\%FDATE%-%RNUM%.7z
echo Candidate: %ZIPFILE% ...
if exist "%ZIPFILE%" GOTO L_TRYANOTHER
rem Zip the folder!
:L_ZIPFILENAME_OK
"%SEVENZIP_EXE%" a %ZIPFILE% "%FOLDERTOZIP_FULLPATH%"
if exist "%ZIPFILE%" GOTO L_OKZIP
GOTO L_ERROREND
:L_OKZIP
rem Move folder: copy, then delete source.
set DEST_FOLDER=%FOLDERTARGETLOCATION%\%FOLDERTOZIP%
mkdir "%DEST_FOLDER%"
xcopy /Y /S "%FOLDERTOZIP_FULLPATH%"\*.* "%DEST_FOLDER%"\
rmdir /S "%FOLDERTOZIP_FULLPATH%"
GOTO L_END
:L_ERROREND
echo 7-Zipping failed !!!
:L_END
pause