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?
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