在bat文件中,我得到了一个简单的命令,如:
xcopy "C:\Users\me\Documents\ApplyPatchBat" "C:\Users\me\Desktop\Test\" /S/E/K 它的作用是复制所有文件,包括子文件夹中的文件。我知道你可以添加/y使其自动覆盖。
如果我们要复制的文件存在于目标文件夹中,请将目标文件夹中的文件复制到备份文件夹中。
我想知道是否有任何IF语句可以添加到命令后,以检测文件是否存在?或者当系统询问我是否要覆盖时,可能会进行错误级别检查?
附注。这是为了将新的补丁应用到程序,其中有许多层的文件夹,这就是为什么我在xcopy中使用/S/E/K。
发布于 2014-10-10 08:11:13
xcopy本身不能预先备份,但是使用xcopy的输出和一些for循环魔术就可以实现这一点
@echo off
setlocal ENABLEDELAYEDEXPANSION
set source="C:\Users\me\Documents\ApplyPatchBat"
set target="C:\Users\me\Desktop\Test\"
rem /l = list only, /u = already existing, /y = yes to replace
rem /f = display full source and destination file names, /c = continue on errors
rem /s = subdirectories, /k = keep attributes
rem split the strings at ->, we're only interested in the part after ->
for /f "usebackq tokens=1,* delims=>" %%a in (`xcopy %source% %target% /l /u /y /f /c /s /k`) do (
set file=%%b
rem ignore lines without a destination, e.g. 15 file(s) copied
if x!file! neq x (
rem remove the leading space, original string is source -> destination
set file=!file:~1!
for %%f in ("!file!") do (
if not exist %%~dpf\backup\* md %%~dpf\backup
rem only backup if not already backed up
if not exist %%~dpf\backup\%%~nxf move %%f %%~dpf\backup
)
)
)
xcopy %source% %target% /y /c /q /s /khttps://stackoverflow.com/questions/26273511
复制相似问题