0

I have a batch script that copy files over a network. I want to log the time, error , and the result. So far I can log the successful result of the xcopy, log of timestamp works. My problem is it can't copy a file. And I also want to log the error if any.

here is my work in progress script.

@echo off

cd %~d0\
set curDrive=%~d0\

Title TEST File Backup Program v2.9.2

set logpath=%~d0\Backuplog\
Set t=%time:~0,2%%time:~3,2%
set filelog=FILE_%date:~0,3%%date:~4,2%%date:~7,2%%date:~10,4%_%t: =0%.log
set errorlog=erFILE_%date:~0,3%%date:~4,2%%date:~7,2%%date:~10,4%_%t: =0%.log

if exist %logpath% (goto Y) else (goto N)

:N
md %logpath%

:Y
NET USE \\hostname [password] /user:domain\domainuser

Set srcfileserver=\\hostname
Set destfileserver=%curDrive%hostname

rem Exclude files/folders with these name(s)
echo \RecycleBin\>%curDrive%xludbak.txt

echo ************************************************* >> %logpath%%filelog%
echo File Backup Started: %date%  %time% >> %logpath%%filelog%
echo ************************************************* >> %logpath%%filelog%

echo My Shared Folder started... >> %logpath%%filelog%
date /t >> %logpath%%filelog%
time /t >> %logpath%%filelog%

for /f "tokens=*" %%i in ('xcopy %srcfileserver%\"My Shared Folder"\ %destfileserver%\"My Shared Folder"\/c/f/y/z/exclude:%curDrive%xludbak.txt') do (
echo [%date%, %time%] %%i >> %logpath%%filelog% 2>> %logpath%%errorlog%
)

echo My Shared Folder finished... >> %logpath%%filelog%
date /t >> %logpath%%filelog%
time /t >> %logpath%%filelog%

thanks in advance

  • 1
    I'm struggling to get what you want, but probably it's a [duplicate](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Sep 11 '20 at 11:20
  • this batch script copies file over a network. I just want to log the time per result of the xcopy command and to log also the error if any. – FlexAIOSys Sep 11 '20 at 11:26
  • ... and because you use `%time%` and `%date%` within a loop, you have to delay-expand them to get the correct values. If that's not your issue, please be more specific. – Stephan Sep 11 '20 at 11:28
  • So far I can log the result of the xcopy, the log of timestamp works. My problem is it can't copy a file. Without the FOR command, it copies the file successfully. I also want to log the error of xcopy if any. – FlexAIOSys Sep 11 '20 at 11:41
  • To begin with, I'd consider changing `'xcopy %srcfileserver%\"My Shared Folder"\ %destfileserver%\"My Shared Folder"\/c/f/y/z/exclude:%curDrive%xludbak.txt'` to `'xcopy "%srcfileserver%\My Shared Folder" "%destfileserver%\My Shared Folder\" /c /f /y /z /exclude:"%curDrive%xludbak.txt"'` – Compo Sep 11 '20 at 12:54
  • @Compo, basically I totally agree with you, except for the `/EXCLUDE` option which does unfortunately not support quoted paths (if I recall correctly). @FlexAIOSys, I generally recommend not to use the badly designed `/EXCLUDE` option and to switch from `xcopy` to `robocopy`, which features well designed exclusion options… – aschipfl Sep 11 '20 at 14:28
  • I'll take your word for it about the quoting of the `/exclude` option @aschipfl, _(which in this case doesn't matter as the variable `%curDrive%` is only a drive letter anyhow and there's no spaces or poison characters in the filename)_. I haven't used `XCopy` since before `RoboCopy` superceded it, and certainly not in the last fifteen years, so I agree that `robocopy.exe` is what I'd be using too. – Compo Sep 11 '20 at 14:34
  • [Here](https://stackoverflow.com/a/42399461/5047996) you can see what I meant by ›badly designed‹ in my previous comment… – aschipfl Sep 11 '20 at 14:46

0 Answers0