-1

I have a batch file that copies some files from different network shared drives to my own computer.

I have set a log file for recording if any error occurs at any point, but am not able to specify the particular drive's name in that error log for which the error has occurred.

My batch code:

@ECHO OFF
set LOGFILE=Backup.log
call :L 2>> %LOGFILE%
EXIT /B

:LOG
***echo --------Drive "c03f"--------- >> Backup.log*** --to print network drive's name in the log file. Please ignore the star mark and the double quotes--
net use p: \\10.210.162.171\Packages && copy p:\run.log d:\Files
:LOG
echo --------Drive "c04f"--------- >> Backup.log
net use p: \\10.210.162.192\Packages && copy p:\run.log d:\Files
:LOG
echo --------Drive "c05f"--------- >> Backup.log
net use p: \\10.210.162.196\Packages && copy p:\run.log d:\Files

Expected Result in the error log file:

--------Drive c03f---------
network name not valid
Cannot connect to the system.
--------Drive c04f---------
network name not valid
Cannot connect to the system.
--------Drive c05f---------
1 File(s) copied

Current result in the error log file: -

network name not valid
Cannot connect to the system.
network name not valid
Cannot connect to the system.
1 File(s) copied

This is what I'm doing and after `:LOG.

It is not giving the network shared drive name the way I want to. Since, there are total three drives and for each one there are same set of commands, I'm not able to deduce for which network shared drive this error has occurred.

There are total three same statements with different drive names and different IPs. I'm doing this in Windows 10. All other network connected PCs are also Windows 10.

double-beep
  • 5,031
  • 17
  • 33
  • 41

1 Answers1

0

Simplest solution would be call :log >>%logfile% 2>&1 to write both STDOUT and STDERR to the logfile. But it seems, you don't want to log STDOUT. So your option is to redirect your echo commands to STDERR in the subroutine:

@ECHO OFF
set LOGFILE=Backup.log
call :L 2>> %LOGFILE%
EXIT /B

:LOG
echo --------Drive "c03f"--------- 1>&2
net use p: \\10.210.162.171\Packages && type p:\run.log >> d:\Files\run.log 
echo --------Drive "c04f"--------- 1>&2
net use p: \\10.210.162.192\Packages && type p:\run.log >> d:\Files\run.log
echo --------Drive "c05f"---------1>&2
net use p: \\10.210.162.196\Packages && type p:\run.log >> d:\Files\run.log

I changed your copy commands to type to avoid overwriting run.log and append instead.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • I tried ``call :log >>%logfile% 2>&1`` before but then when I executed my batch file, the cmd screen didn't show anything, it was a blank screen. Due to this, I wasn't able to deduce that my batch file is running which command. That's why I did ``call :L 2>> %LOGFILE%``. This is the secondary issue that I'm facing in this case. But thanks for this help. I'll try this one. – Abhishek Raj May 15 '19 at 12:00
  • seems you look for something like a `tee` utility (google it, there are several) or [this poor-man's solution](https://stackoverflow.com/a/21072977/2152082) – Stephan May 15 '19 at 12:07
  • However, @Stephan! I have a question that, are you suggesting to create a new `run.log` in location`d:\Files` and then redirect the `p:\run.log` file to that new file?? – Abhishek Raj May 15 '19 at 12:20
  • I can't tell. I don't know, where those files comes from, what they are and what to do with it. (I just focussed on your redirection problem.) But I noticed, they will be overwritten by the code in your question and that didn't make sense to me. – Stephan May 15 '19 at 12:28