windows 创建批处理文件以备份指定文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1474366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:11:56  来源:igfitidea点击:

Creating a batch file to back up a specified folder

windowsbatch-filezipbackup7zip

提问by Tarik

I am actually pretty new to this batch file thing. I know it's important to know at least the basic commands. How do I do the following?

我实际上对这个批处理文件很陌生。我知道至少了解基本命令很重要。我如何执行以下操作?

  • Zipping a specified folder.
  • Move the folder to another place.
  • When zipping it, the ZIP file name will be the current date and if there is another zipped file with the same name, it should be named like 20090924-2.
  • 压缩指定的文件夹。
  • 将文件夹移动到另一个位置。
  • 压缩时,ZIP 文件名将是当前日期,如果有另一个同名的压缩文件,则应命名为 20090924-2。

PS: 7-Zipis installed on my computer as an archive software.

PS7-Zip作为存档软件安装在我的电脑上。

回答by Peter Mortensen

The batch script listed below will do it (I have tested it and it works to specifications). The directory to zip and move is specified as its parent directory and the name of the directory (PARENT_FOLDERTOZIP and FOLDERTOZIP in the beginning) - I couldn't figure out how to copy entire directories (I think XCOPY can only copy content of directories and sub-directories.). The copy location is specified as FOLDERTARGETLOCATION, and the directory to place the compressed files in is specified as ZIPDIR.

下面列出的批处理脚本将执行此操作(我已经对其进行了测试,并且符合规范)。要压缩和移动的目录被指定为其父目录和目录名称(开头的 PARENT_FOLDERTOZIP 和 FOLDERTOZIP) - 我不知道如何复制整个目录(我认为 XCOPY 只能复制目录和子目录的内容-目录。)。复制位置指定为 FOLDERTARGETLOCATION,放置压缩文件的目录指定为 ZIPDIR。

The location of 7-Zip is configured through SEVENZIP_EXE.

7-Zip 的位置是通过 SEVENZIP_EXE 配置的。

Note also that getting the current date in the required format depends on the short date format in regional settings. I have listed three different versions for ISO-8601, Central European and U.S.A. The active one in the listing is for the U.S.A. (the "set FDATE=" line). If a different one is needed then just copy-paste from one of the other two.

另请注意,以所需格式获取当前日期取决于区域设置中的短日期格式。我为 ISO-8601、中欧和美国列出了三个不同的版本。列表中的活跃版本是针对美国的(“set FDATE=”行)。如果需要不同的,则只需从其他两个中的一个复制粘贴即可。

That said it should be noted that this kind of thing is much easier with Perl, Pythonor PowerShell.

也就是说,应该注意的是,这种事情在PerlPythonPowerShell 中要容易得多。



@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:\toDelete09-09-24a


rem Configuration
  set SEVENZIP_EXE=D:\Program Files-Zipz.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