How to get rid of The file cannot be copied onto itself.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

How to get rid of The file cannot be copied onto itself.

#1 Post by goodywp » 09 Jun 2022 13:05

Hi ALl,

I have a below code to copy all subfolders files to one newly made subfolder.
folder/
    /Sub1
      /sub2
        /sub3
        .......
          /subfolder

          Code: Select all

          for /r "folder" %%d in (*) do copy "%%d" "folder\subfolder"
          
          It works fine except in the middle having these lines
          The file cannot be copied onto itself.
          0 file(s) copied.

          How can I get rid of these lines?

          Thanks

          ShadowThief
          Expert
          Posts: 1160
          Joined: 06 Sep 2013 21:28
          Location: Virginia, United States

          Re: How to get rid of The file cannot be copied onto itself.

          #2 Post by ShadowThief » 09 Jun 2022 16:06

          Stick a 2>nul at the end of the line to hide the errors

          penpen
          Expert
          Posts: 1991
          Joined: 23 Jun 2013 06:15
          Location: Germany

          Re: How to get rid of The file cannot be copied onto itself.

          #3 Post by penpen » 15 Jun 2022 02:14

          You should copy your files to a temporary folder (preferably on the same volume) and then move them to the final location.

          penpen

          goodywp
          Posts: 250
          Joined: 31 Jul 2017 09:57

          Re: How to get rid of The file cannot be copied onto itself.

          #4 Post by goodywp » 22 Aug 2022 11:40

          Thanks I will take Stick a 2>nul at the end of the line to hide the errors

          Post Reply