简单的批处理,复制和删除文件并记录到目标网络共享,问题是在日志中我只有文件被复制,没有目标上的文件被删除
md \\network share\"%date%"\
set LOGFILE=\\network share\"%date%"\"%date%"_"log".log
call :LOG > %LOGFILE%
exit /B
:LOG
Xcopy /E /Y C:\source\*.* \\network share\"%date%"\
del C:\source\*.bak /Q
并且日志文件仅包含:
C:\source\1.bak
C:\source\2.bak
2 File(s) copied
答案1
该del
命令不产生任何输出。您应该使用robocopy
,或者一些 powershell 命令,例如Remove-Item
:
remove-item .\2021.zip -verbose
VERBOSE: Performing the operation "Remove File" on target "C:\Users\gasperc\2021.zip".
答案2
for /f delims^= %%i in ('dir /a:-d /b "C:\source\*.bak"
')do del /q "%%~fi" && echo\ Deleted: "%%~dpi" "%%~nxi"
您可以使用for
循环逐个删除并根据需要获取“自定义”输出。
@echo off
md "\\network share\%date%\"
set "LOGFILE=\\network share\%date%\%date%_log.log"
call :LOG > "%LOGFILE%" & exit /B
:LOG
Xcopy /E /Y "C:\source\*.*" "\\network share\%date%\"
for /f delims^= %%i in ('dir /a:-d /b "C:\source\*.bak"
')do del /q "%%~fi" && echo\ Deleted: "%%~dpi" "%%~nxi"