I want to use robocopy in the bat file and copy a file with a name that contains spaces.

Asked 2 years ago, Updated 2 years ago, 94 views

As the title suggests, I would like to copy the data using robocopy in the bat file.
As I want to skip errors and leave logs, I am thinking of using robocopy to copy files.

What is currently being done
Copy a file with a name containing the specified keyword from one folder to another

Not done
Files with spaces in the name cannot be copied

Example: I want to copy a file with a name that contains robo

  • Files that can be copied
     robo.txt, robo123.ppt
  • Files that cannot be copied
      robo.txt, robo123.ppt

I wonder if robocopy can't recognize file names including spaces.
I would appreciate it if you could let me know if you know how to copy it in any other way.
Thank you for your cooperation.

"If the name of the file you want to copy has been decided in advance, you can use "" to enclose the name of the file, but
" What should I do if I want to search for a file in a folder and extract a file that contains the specified name?     
You are currently using a for statement.  

 rem// Specify the folder path from which to copy  
set fs_src= ※Any folder path 

rem// Specifying Extraction Criteria Characters
set name = robo 

for /r%fs_src%%%In(*%name%*.*)drobocopy~ 


I summarized it briefly, but I wrote the code like the one above.

I was able to copy the data with the name that contains the blank space using the method you advised me to do.

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

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

Filename you want to extract: name

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

Currently trying to output LOG files at the same time as copying, copying only under the same time as copying…

robocopy/s"%fs_src%"%fs_bk%"*%name%*.*"/LOG:%logdir%\%target%_%ToDay%.txt%~pI%fs_bk%%%~nxI

If you continue to type LOG commands as above, only LOG data was created and you could not copy the data.

windows batch-file

2022-09-30 21:48

3 Answers

Try double-quoting the filename that contains spaces.

"robo.txt", "robo123.ppt"


2022-09-30 21:48

I think we can handle it with the robotopy argument without using the for statement.

 rem// Specify the folder path from which to copy
set fs_src= ※Any folder path

rem // Specify the destination folder path
set fs_dest= ※Any folder path

rem// Specifying Extraction Criteria Characters
set name = robo

robocopy "%fs_src%" "%fs_dest%" "*%name%*.*" 


2022-09-30 21:48

As described in the FOR Help removes " that binds variables by adding through.
After that, you can use " to correctly handle spaces.

 for /r%%I in (*%name%*.*) drobocopy~"%%~I"


2022-09-30 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.