3

I'm making a batch script to copy all .doc, .pdf, .xls etc from disk to a pendrive.

My current code below:

@echo off
Title ""

::All Docs
 XCOPY C:\*.doc W:\xdatabase /C /S /I /F /H > W:\database\XC\AllInf.txt

::All PDFs
 XCOPY C:\*.pdf W:\xdatabase /C /S /I /F /H >> W:\database\XC\AllInf.txt

::All WRI
 XCOPY C:\*.wri W:\xdatabase /C /S /I /F /H >> W:\database\XC\AllInf.txt

::All TXT
 XCOPY C:\*.txt W:\xdatabase /C /S /I /F /H >> W:\database\XC\AllInf.txt

::All PPT
 XCOPY C:\*.ppt W:\xdatabase /C /S /I /F /H >> W:\database\XC\AllInf.txt

::All XLS
 XCOPY C:\*.xls W:\xdatabase /C /S /I /F /H >> W:\database\XC\AllInf.txt

The question is: How can I add more extensions but avoid all that duplication in the code?

Wesley Lomax
  • 2,067
  • 2
  • 20
  • 34
Derley mad
  • 63
  • 1
  • 6

2 Answers2

16

Always true, in all programming languages: If you have to do a thing multiple times in a row, use a loop.

@echo off
Title ""

for %%e in (doc pdf wri txt ppt xls) do (
    XCOPY "C:\*.%%e" W:\xdatabase /C /S /I /F /H > W:\database\XC\AllInf.txt
)

The for loop can be tricky in batch scripting. A handy guide is here: http://ss64.com/nt/for.html

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Oh, im grateful, that exacly what I was asking thks a lot – Derley mad Sep 12 '16 at 14:00
  • @Derley You can also check out robocopy for ways to include multiple sources in one single command. – Tomalak Sep 12 '16 at 14:16
  • @Tomalak This for loop can't be done in one statement? – johny why Sep 09 '21 at 18:50
  • @johny I don't think so. `xcopy` only accepts only one source argument. – Tomalak Sep 09 '21 at 19:14
  • @Tomalak This works on one line: `for %f in (xlsm xlsb) do xcopy SourceDir\*.%f DestinationDir\ /S` – johny why Sep 09 '21 at 23:29
  • 1
    @johnywhy That's a loop, too. That you wrote it on one line makes no difference. – Tomalak Sep 10 '21 at 07:40
  • @Tomalak True, i never said it wasn't a loop. – johny why Sep 10 '21 at 09:24
  • @johnywhy It's also not one statement. But we really don't need to argue about this. Your and my approach are equivalent. Mine is formatted to be used in a batch file, yours is formatted as a one-off command. They're still the same thing. Use whatever is more convenient in any given situation. – Tomalak Sep 10 '21 at 09:34
  • @Tomalak It's my confusion about the difference between "one statement" vs "one command". Anyway, now i'm playing with robocopy. – johny why Sep 10 '21 at 09:46
2

This works on one line, no script-file needed. Needs single percent-sign, not double:

for %f in (xlsm xlsb) do xcopy SourceDir\*.%f DestinationDir\ /S

or in your case

for %e in (doc pdf xls wri txt ppt xls) do XCOPY C:\*.%e W:\xdatabase /C /S /I /F /H > W:\database\XC\AllInf.txt

according to my tests, robocopy supports multiple extensions without a loop.

ROBOCOPY C:\ W:\xdatabase /S *.doc *.pdf *.xls *.wri *.txt *.ppt *.xls

However, in my experience xcopy seems to beat robocopy on speed, even with multiple extensions, but i only tested with a very small batch of files.

johny why
  • 2,047
  • 7
  • 27
  • 52
  • +1 for including robocopy, which helped me as it's clearly an XY question and robocopy's a better fit than xcopy for multiple extensions. – Reg Edit Mar 14 '22 at 12:40