Copying multiple files by *.extension with xcopy

旧巷老猫 提交于 2019-12-11 02:15:45

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/39451898/copying-multiple-files-by-extension-with-xcopy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!