I want to use robocopy in the bat file and print the data copy result to the LOG file.

Asked 2 years ago, Updated 2 years ago, 153 views

I am thinking of using robocopy in the bat file and extracting a file name containing one character and copying it to another folder.
I also want to copy the file name with blank space, so I wrote the following code.

  • Save source data to: fs_src
  • Save the extracted data to: fs_bk
  • Filename you want to extract: name

robocopy/s"%fs_src%"%fs_bk%"*%name%*.*"

LOG is about to be printed as a txt file because I want to know if the copy result failed or succeeded is
I wrote it as below, but nothing came out.
I wrote a LOG statement after the robocopy statement.

  • LOG output destination:logdir
  • LOG filename: target
  • Date, time, and filename settings for logging:ToDay
  • Save the extracted data to: fs_bk

    /LOG:log\%logdir%\%target%_%ToDay%.txt%%~pI%fs_bk%%~nxI

Save the extracted data to: fs_bk

/LOG:log\%logdir%\%target%_%ToDay%.txt%%~pI%fs_bk%%~nxI

If things go well, you should be able to create a LOG text file like this.

Example: If you run bat on March 30, 2020
  copylog_20200330104845.txt

I would appreciate it if someone could tell me.

windows batch-file

2022-09-30 21:47

1 Answers

Unless you suppress the robocopy command with >NUL2>&1, or run the batch itself hidden, you will see something like this, whether the result is normal or error, so consider what to do from there.
Also, if you don't stop it, the display will disappear after the batch, so please temporarily put pause immediately after robocopy.

Normal Run Example:

log files:C:\Develop\LOG\EXECUTED\COPYLOG_20200401100124.TXT

Error Example:

April 1, 2020 09:55:51 Error 3 (0x00000003) Opening Log File C:\Develop\LOG\EXECUTED\COPYLOG_20200401095551.TXT
The specified path could not be found.

-------------------------------------------------------------------------------
   ROBOCOPY:: Windows Robust File Copy                    
-------------------------------------------------------------------------------

  Start: April 1, 2020 9:55:51
   Copy from - C:\Develop\Working\
     Copy to - C:\Develop\BACKUP\20200401095551\

    File: *robo*.*

  Optional: /S/DCOPY: DA/COPY: DAT/R: 1000000/W:30

------------------------------------------------------------------------------

Error: Invalid parameter #5: "/LOG: LOG\EXECUTED\COPYLOG_20200401095551.TXT"

       Simple Usage:: ROBOCOPY Source/MIR

           Source:: Source directory (drive:\pass or \\server)
                       shared path).
           Destination:: Destination directory (drive:\pass or \\server)
                       US>\\Shared\Path(s
               /MIR:—Mirrors the complete directory tree.

    For detailed usage information, run ROBOCOPY/?


****  The /MIR can copy and delete files.

If executed successfully, you can create log\%logdir%\%target%_%ToDay%.txt under log\%logdir%\%target%_%ToDay%.txt where the batch file exists or the work folder you specified in Run Shell.Look for it.

There are two common causes of errors:

  • The middle folder specified in the output destination path ("LOG" or "LOG\%logdir%") does not exist
  • The folder name contains spaces but does not enclose the entire folder with "

By the way, when it comes to how to specify the path of the question article, %%~pI%fs_bk%%%~nxI attached after the log file name specification seems to be meaningless.

Also, it seems that the log file name includes years, time, minutes, and seconds, but if there are few files to copy and you change the conditions to operate multiple times in a loop, it might be better to use /LOG+: instead of /LOG:.
For example, you can think of this statement:

SETFS_SRC=C:\Develop\Working
SET NAME=robo
SET LOGDIR=EXECUTED
SET TARGET=COPYLOG
SET TODAY=%DATE: /=%%TIME::=%
SET TODAY=%TODAY:=0%
SET TODAY=%TODAY: ~0,14%
SETFS_BK=C:\Develop\BACKUP\%TODAY%
US>MKDIR %FS_BK%
ROBOCOPY "%FS_SRC%" "%FS_BK%" "*%NAME%*.*"/S/LOG+: "LOG\%LOGDIR%\%TARGET%_%TODAY%.TXT"

Reference:
How to create log files by date when backed up with robocopy (+how to delete old files from the past as well)


2022-09-30 21:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.