Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file XCOPY覆盖操作?_Batch File_Cmd_Xcopy - Fatal编程技术网

Batch file XCOPY覆盖操作?

Batch file XCOPY覆盖操作?,batch-file,cmd,xcopy,Batch File,Cmd,Xcopy,在bat文件中,我得到了一个简单的命令,如: xcopy "C:\Users\me\Documents\ApplyPatchBat" "C:\Users\me\Desktop\Test\" /S/E/K 此操作将复制所有文件,包括子文件夹中的文件。我知道您可以添加/y使其自动覆盖 如何创建语句如果要复制的文件存在于目标文件夹中,请将目标文件夹中的文件复制到备份文件夹中 我想知道在命令之后是否可以添加IF语句来检测文件是否存在?或者在询问我是否要覆盖时进行错误级别检查P.S.这是为了在程序中应

在bat文件中,我得到了一个简单的命令,如:

xcopy "C:\Users\me\Documents\ApplyPatchBat" "C:\Users\me\Desktop\Test\" /S/E/K 
此操作将复制所有文件,包括子文件夹中的文件。我知道您可以添加/y使其自动覆盖
如何创建语句如果要复制的文件存在于目标文件夹中,请将目标文件夹中的文件复制到备份文件夹中


我想知道在命令之后是否可以添加IF语句来检测文件是否存在?或者在询问我是否要覆盖时进行错误级别检查
P.S.这是为了在程序中应用新的补丁,其中有许多文件夹层,这就是为什么我在xcopy中使用/S/E/K。

xcopy本身无法预先备份,但是通过xcopy的输出和一点for-loop魔术,您可以实现这一点

@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 /k

xcopy本身无法预先备份,但通过xcopy的输出和一点for-loop魔术,您可以实现这一点

@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 /k

xcopy本身无法预先备份,但通过xcopy的输出和一点for-loop魔术,您可以实现这一点

@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 /k

xcopy本身无法预先备份,但通过xcopy的输出和一点for-loop魔术,您可以实现这一点

@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 /k

这在xcopy中无法完成-您可以在xcopy运行之前使用脚本检查和移动文件。如果您希望跟踪在xcopy中无法完成的修订,则最好使用,您可以在xcopy运行之前使用脚本检查和移动文件。您最好使用,如果要跟踪在xcopy中无法完成的修订-可以在xcopy运行之前使用脚本检查和移动文件。最好使用,如果要跟踪在xcopy中无法完成的修订-可以在xcopy运行之前使用脚本检查和移动文件。最好使用,如果你想跟踪修改,这是惊人的,比我要做的要优化得多。只有几个问题才能完全理解它。在第一个例子中,括号内的xcopy是否真的复制了文件?那么%%a将成为文件?%%b将是什么?最后一个问题,x是什么!文件是什么意思?非常感谢你的帮助!第一个xcopy具有/l标志,它只列出文件而不复制。令牌=1,*将使%%a成为源(带尾随“-”空格破折号),而%%b成为目标(带前导空格)。enabledelayedexpansion允许您使用!代替%用于变量,但使用!意味着值将刷新。那么x!文件equ x在问——是!文件空白?谢谢,现在更清楚了:)我知道那个临时变量!签名,但是声明周围的2 x是什么意思呢?这可能有助于为您解释这一部分:这是惊人的,比我正在尝试做的要优化得多。只有几个问题才能完全理解它。在第一个例子中,括号内的xcopy是否真的复制了文件?那么%%a将成为文件?%%b将是什么?最后一个问题,x是什么!文件是什么意思?非常感谢你的帮助!第一个xcopy具有/l标志,它只列出文件而不复制。令牌=1,*将使%%a成为源(带尾随“-”空格破折号),而%%b成为目标(带前导空格)。enabledelayedexpansion允许您使用!代替%用于变量,但使用!意味着值将刷新。那么x!文件equ x在问——是!文件空白?谢谢,现在更清楚了:)我知道那个临时变量!签名,但是声明周围的2 x是什么意思呢?这可能有助于为您解释这一部分:这是惊人的,比我正在尝试做的要优化得多。只有几个问题才能完全理解它。在第一个例子中,括号内的xcopy是否真的复制了文件?那么%%a将成为文件?%%b将是什么?最后一个问题,x是什么!文件是什么意思?非常感谢你的帮助!第一个xcopy具有/l标志,它只列出文件而不复制。令牌=1,*将使%%a成为源(带尾随“-”空格破折号),而%%b成为目标(带前导空格)。enabledelayedexpansion允许您使用!代替%用于变量,但使用!意味着值将刷新。那么x!文件equ x在问——是!文件空白?谢谢,现在更清楚了:)我知道那个临时变量!签名,但是声明周围的2 x是什么意思呢?这可能有助于为您解释这一部分:这是惊人的,比我正在尝试做的要优化得多。只有几个问题才能完全理解它。在第一个例子中,括号内的xcopy是否真的复制了文件?那么%%a将成为文件?%%b将是什么?最后一个问题,x是什么!文件是什么意思?非常感谢你的帮助!第一个xcopy具有/l标志,它只列出文件而不复制。令牌=1,*将使%%a成为源(带尾随“-”空格破折号),而%%b成为目标(带前导空格)。enabledelayedexpansion允许您使用!代替%用于变量,但使用!意味着值将刷新。那么x!文件equ x在问——是!文件空白?谢谢,现在更清楚了:)我知道那个临时变量!签名,但声明周围的2 x是什么意思?这可能有助于为您解释这一部分: