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

发布于 2024-08-05 22:51:32 字数 291 浏览 2 评论 0原文

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

  • 压缩指定文件夹。
  • 将文件夹移动到另一个地方。
  • 压缩时,ZIP 文件名将是当前日期和 如果有另一个同名的压缩文件,则应命名为 20090924-2。

PS7-Zip 在我的计算机上安装为一个存档软件。

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡水深流 2024-08-12 22:51:32

下面列出的批处理脚本可以做到这一点(我已经测试过
并且它符合规范)。要压缩的目录和
move 被指定为其父目录和名称
目录(PARENT_FOLDERTOZIP 和 FOLDERTOZIP 在
开始) - 我不知道如何复制整个
目录(我认为 XCOPY 只能复制
目录和子目录。)。复制位置是
指定为 FOLDERTARGETLOCATION,以及要放置的目录
中的压缩文件指定为 ZIPDIR。

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

另请注意,获取所需的当前日期
格式取决于区域中的短日期格式
设置。我列出了三个不同的版本
ISO-8601,中欧和美国
列表适用于美国(“set FDATE=”行)。如果一个
需要不同的,然后只需从其中之一复制粘贴
另外两个。

话虽这么说,但值得注意的是,这种事情很多
使用 Perl 更容易,PythonPowerShell >。


@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

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.

The location of 7-Zip is configured through 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.

That said it should be noted that this kind of thing is much
easier with Perl, Python or 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文