0

I have written the following code and am trying to copy all the files to the location. The following line works when I paste it on cmd but it doesn't work in the batch file:

for %I in (*.m) do (copy /Y %I "%appdata%\Math\include")
pause

Kindly, let me know what I am missing?

I have tried to follow the answers from here: Can I copy multiple named files on the Windows command line using a single "copy" command?
But no use to me. Please share your suggestions.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • 3
    Double the percents, `@For %%I In (*.m) Do @Copy /Y "%%I" "%AppData%\Math\include"`. _Although you don't really need a `For` loop, `Copy /Y *.m "%AppData%\Math\include"`_. – Compo Jul 26 '18 at 08:43

1 Answers1

1

You could just use copy or xcopy:

copy /y *.m newDir

However, if newDir does not exist copy will create a file named newDir

To avoid that xcopy /i can be used instead:

xcopy /y /i *.m newDir

To include subdirectories use xcopy /s:

xcopy /y /i /s *.m newDir

or shorter:

xcopy /yis *.m newDir

There is also the newer Robocopy.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • Yes I got it. But what if the scenario changes and I need to copy more than one I type of extension files? That's why I am trying to use such a looping method. – Jaffer Wilson Jul 27 '18 at 05:35
  • @JafferWilson like `for %I in (com bat exe) do copy "*.%I" ""%appdata%\Math\include"` – Stephan Jul 27 '18 at 08:46
  • @JafferWilson For multiple extensions you could indeed use the loop, or multiple xcopy commands. – Danny_ds Jul 27 '18 at 19:06