Windows批处理命令创建备份文件夹并替换文件夹 [英] Windows batch command to create backup folder and replace folder

查看:153
本文介绍了Windows批处理命令创建备份文件夹并替换文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要备份带有日期时间戳的现有文件夹,并用该文件夹中的新内容替换(删除并重新创建).

I need to backup an existing folder with date-time stamp and replace it (delete and recreate) with new content inside the folder.

有人有执行此操作的脚本吗?

Does anyone have a script to do this?

我尝试了以下代码,其中%ApplicationDeploymentFolderPath% = \\servername\foldername

I tried the following code, where %ApplicationDeploymentFolderPath% = \\servername\foldername

IF EXIST %ApplicationDeploymentFolderPath%\Release (

REM Get current date time
@echo off
For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c_%%b_%%a)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
set backup_folder=%mydate%_%mytime%
MD %ApplicationDeploymentFolderPath%\%backup_folder%

REM Copy current folder to backup folder
Copy %ApplicationDeploymentFolderPath%\Release %ApplicationDeploymentFolderPath%\%backup_folder% 

REM Delete Existing Release folder 
RD %ApplicationDeploymentFolderPath%\Release /S /Q
)

MD %ApplicationDeploymentFolderPath%\Release

推荐答案

带有参数/T命令 date以配置的国家/地区为当前用户帐户定义的格式输出当前日期.可以通过引用动态环境变量 DATE(例如,使用%DATE%)来访问完全相同的日期字符串.

The command date with parameter /T outputs the current date in format defined by configured country for current user account. Exactly the same date string can be accessed by referencing dynamic environment variable DATE for example with %DATE%.

带有参数/T命令 time以配置的国家/地区为当前用户帐户定义的格式输出当前时间.通过引用动态环境变量 TIME,例如使用%TIME%,可以访问完全相同的时间字符串.

The command time with parameter /T outputs the current time in format defined by configured country for current user account. Exactly the same time string can be accessed by referencing dynamic environment variable TIME for example with %TIME%.

执行此命令行会发生什么?

What happens on execution of this command line?

For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c_%%b_%%a)

forcmd.exe分别处理批处理文件,是在后台使用%ComSpec% /c'之间的命令行进行的另一命令处理.因此,在Windows中安装C:\Windows

for respectively cmd.exe processing the batch file starts in background one more command process using %ComSpec% /c with the command line between '. So executed in background is following with Windows installed in C:\Windows:

C:\Windows\System32\cmd.exe /c date /t

分别在Windows命令处理器实例执行时捕获命令 date在后台处理此命令过程的 STDOUT 的输出.批处理文件.

The output of command date to handle STDOUT of this command process in background is captured by FOR respectively Windows command processor instance executing the batch file.

使用/作为分配给循环变量abc的字符串定界符,将捕获的行分为三个子字符串,这些变量以下划线作为定界符以相反的顺序连接在一起.

The captured line is split up into three substrings using / as string delimiter assigned to the loop variables a, b and c which are concatenated together in reverse order with underscore as delimiter.

通过将'date /t'替换为"%DATE%",可以更快地完成此任务.在这种情况下, FOR 处理在执行 FOR 之前已经在解析此命令行上运行cmd.exe的日期扩展的 string 日期.因此,在后台没有再启动cmd.exe并捕获其输出只是为了处理同一日期 string 的情况,这使得批处理文件的执行速度更快.

This task can be done much faster by replacing 'date /t' by "%DATE%". In this case FOR processes the date string expanded by already running cmd.exe on parsing this command line before executing FOR. So there is no starting of one more cmd.exe in background and capturing its output just to process the same date string which makes batch file execution a bit faster.

'time /t'也是如此,可以将其替换为"%TIME%".

The same is true for 'time /t' which can be replaced by "%TIME%".

但是,通过使用字符串替换可以完全优化两个 FOR 循环,例如,通过%date:〜-4,4 %% date:〜-10,2 %% date:〜-7,2%_%time:〜0.2 %% time:〜3.2%是什么意思?和与区域相关的日期和时间格式,例如通过在命令提示符窗口中运行来众所周知:

But the two FOR loops could be completely optimized away by using string substitution as described for example by answer on What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean? and region dependent date and time format is well known for example by running in a command prompt window:

echo %DATE% %TIME%

此命令根据配置的国家/地区以德语日期/时间格式在我的计算机上输出:

This command outputs on my computer with German date/time format according to configured country:

24.07.2019 20:15:29,90

在此输出中可以看到,由于日期字符串包含.而不是/并且时间字符串包含逗号,因此原始代码无法在我的Windows计算机上使用我的帐户运行.

It can be seen on this output that the original code would not work on my Windows computer with my account because of date string contains . and not / and time string contains a comma.

因此最好使用区域独立的解决方案,如%date%的答案中非常详细地解释的那样,运行时在批处理文件中会产生不同的结果排定的任务中.缺点是wmic.exe的执行时间比cmd.exe将日期和时间字符串重新格式化为YYYY_MM_DD_HHMM所需的时间长得多.但是,批处理文件很可能不是每天执行一次,因此以这种格式获取日期/时间需要花费几毫秒或一秒钟的时间,这并不重要.

So better would be using a region independent solution as explained very detailed in answer on %date% produces different result in batch file when run from scheduled tasks. The disadvantage is that execution of wmic.exe takes much longer than cmd.exe needs to reformat date and time string to YYYY_MM_DD_HHMM. However, the batch file is executed most likely not very often per day, and so it does not really matter if execution to get date/time in this format takes some milliseconds or about one second.

在这种情况下,实际上并不需要复制整个文件夹.用以下命令重命名就足够了:

Copying the entire folder is not really necessary in this case. It should be enough to rename it with:

ren "%ApplicationDeploymentFolderPath%\release" "%backup_folder%"

如果出于未知原因无法使用命令ren,也可以使用命令move.

The command move could be also used if command ren cannot be used for unknown reasons.

但是,主要问题是缺少有关如何以及何时使用延迟扩展的知识.打开命令提示符,运行set /?并阅读有关 IF的输出帮助说明. 和一个 FOR 示例延迟的环境变量扩展.

However, the main problem is missing knowledge about how and when to use delayed expansion. Open a command prompt, run set /? and read the output help explaining on an IF and a FOR example delayed environment variable expansion.

这里的问题是,backup_folder在执行使用%backup_folder%引用的命令行时未定义,因为所有%variable%的出现都已被Windows命令处理器替换,该命令已在解析整个命令块时以在执行命令 IF 之前,先在 IF 条件下按引用的环境变量的当前值.

The issue here is that backup_folder is not defined on executing the command lines referencing it with %backup_folder% because of all occurrences of %variable% are replaced by Windows command processor already on parsing entire command block starting here with ( on IF condition at top by current value of the referenced environment variable before executing the command IF.

因此在现有发行版文件夹上执行的是:

So executed on existing release folder is:

set backup_folder=
MD \\servername\foldername\

REM Copy current folder to backup folder
Copy \\servername\foldername\Release \\servername\foldername\ 

REM Delete Existing Release folder 
RD \\servername\foldername\Release /S /Q

这可以通过调试批处理文件看到.

另请参阅: Windows命令解释器(CMD.EXE)如何解析脚本?

解决方案是通过更改第一个 IF 条件来避免命令阻塞.

The solution is here avoiding the command block by changing the first IF condition.

快速区域依赖解决方案:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ApplicationDeploymentFolderPath=\\servername\foldername"

if not exist "%ApplicationDeploymentFolderPath%\Release\" goto CreateFolder

ren "%ApplicationDeploymentFolderPath%\Release" "%DATE:~-4%_%DATE:~-7,2%_%DATE:~-10,2%_%TIME:~0,2%%TIME:~3,2%"

:CreateFolder
md "%ApplicationDeploymentFolderPath%\Release"
endlocal

较慢区域独立的解决方案:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ApplicationDeploymentFolderPath=\\servername\foldername"

if not exist "%ApplicationDeploymentFolderPath%\Release\" goto CreateFolder

for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "BackupDateTime=%%I"
set "BackupDateTime=%BackupDateTime:~0,4%_%BackupDateTime:~4,2%_%BackupDateTime:~6,2%_%BackupDateTime:~8,4%"
ren "%ApplicationDeploymentFolderPath%\Release" "%BackupDateTime%"

:CreateFolder
md "%ApplicationDeploymentFolderPath%\Release"
endlocal

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • ren /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • ren /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?

这篇关于Windows批处理命令创建备份文件夹并替换文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆