Search code examples
windowsbatch-filecmdcopyxcopy

Want to copy document and different kind of file with Batch


I am trying to copy .pdf .docx and .zip files with a single click via batch file. Its working perfectly but the problem is it takes so much time to copy the data.For Example, If I want to copy those extension file for the Drive D the batch file will run to find .pdf first than after searching the whole drive for the .pdf than it will go and search for .docx file. I want to do that in one command if it find .pdf file than copy it if .docx than do the same but in one search.

Here is the code

@echo off 
    :: variables 
    /min 
    /w

    SET odrive=%odrive:~0,2%
    set backupcmd=xcopy /s /c /d /e /h /i /r /y 
    echo off 

    %backupcmd% "E:\MIEMS\Dropbox\Bahria\*.pdf" "%drive%\Personal\PICS\Wedding\Barat\MOVIEP"
    %backupcmd% "E:\MIEMS\Dropbox\Bahria\*.docx" "%drive%\Personal\PICS\Wedding\Walima\MOVIED"
    %backupcmd% "E:\MIEMS\Dropbox\Bahria\*.doc" "%drive%\Personal\PICS\Wedding\Walima\MOVIED"
    %backupcmd% "E:\MIEMS\Dropbox\Bahria\*.zip" "%drive%\Personal\PICS\Wedding\Mehndi\MOVIEZ"
    %backupcmd% "E:\MIEMS\Dropbox\Bahria\*.rar" "%drive%\Personal\PICS\Wedding\Mehndi\MOVIEZ"


    %backupcmd% "C:\Users\%USERNAME%\MIEMS\Dropbox\Bahria\*.pdf" "%drive%\Personal\PICS\Wedding\Barat\MOVIEP3"
    %backupcmd% "C:\Users\%USERNAME%\MIEMS\Dropbox\Bahria\*.docx" "%drive%\Personal\PICS\Wedding\Walima\MOVIED3"
    %backupcmd% "C:\Users\%USERNAME%\MIEMS\Dropbox\Bahria\*.doc" "%drive%\Personal\PICS\Wedding\Walima\MOVIED3"
    %backupcmd% "C:\Users\%USERNAME%\MIEMS\Dropbox\Bahria\*.zip" "%drive%\Personal\PICS\Wedding\Mehndi\MOVIEZ"
    %backupcmd% "C:\Users\%USERNAME%\MIEMS\Dropbox\Bahria\*.rar" "%drive%\Personal\PICS\Wedding\Mehndi\MOVIEZ"



    @echo off 
    cls

I want to do something like it will go to Drive E and the specific path and copy the files in a single line of code like in the first search it will copy .pdf if found else find if docx is there yes copy it then..

It will be really helpful if you guys could help me solve this problem.

EDITED ONE

@echo off 
setlocal enabledelayedexpansion
set backupcmd=echo
set drive=N:

SET odrive=%odrive:~0,2%
echo off
set backupcmd=xcopy /c /d /h /i /r /y

set "files=C:\*.pdf C:\*.doc C:\*.docx C:\*.zip C:\*.rar"

for /f "delims=" %%i in ('dir /s /b %files%') do (
  if "%%~xi"==".pdf" set "dest=D"
  if "%%~xi"==".doc" set "dest=D"
  if "%%~xi"==".docx" set "dest=D"
  if "%%~xi"==".zip" set "dest=Z"
  if "%%~xi"==".rar" set "dest=Z"
  if "%%~di"=="C:" if "!dest!"=="Z" set "dest=!dest!3"
  %backupcmd% "%%i"  "%drive%\Personal\PICS\Wedding\Barat\MOVIE!dest!"
)

@echo off 
cls

enter image description here


Solution

  • this Needs some time before starting copying, because it builds the complete list of files before working on them. But it does scan the disks just once.

    @echo off 
    setlocal enabledelayedexpansion
    set backupcmd=echo
    set drive=N:
    set "files=C:\*.pdf C:\*.doc C:\*.docx C:\*.zip C:\*.rar"
    set "files=%files% E:\*.pdf E:\*.doc E:\*.docx E:\*.zip E:\*.rar"
    
    
    for /f "delims=" %%i in ('dir /s /b %files%') do (
      if "%%~xi"==".pdf" set "dest=D"
      if "%%~xi"==".doc" set "dest=D"
      if "%%~xi"==".docx" set "dest=D"
      if "%%~xi"==".zip" set "dest=Z"
      if "%%~xi"==".rar" set "dest=Z"
      if "%%~di"=="C:" if "!dest!"=="Z" set "dest=!dest!3"
      %backupcmd% "%%i"  "%drive%\Personal\PICS\Wedding\Barat\MOVIE!dest!"
    )
    

    Of course, you don't need xcopys Parameters /s and /e anymore.