Help with copying all .pdf and .docx files on all of the computer's drives to a single directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Shaioki
Posts: 1
Joined: 17 Dec 2016 14:25

Help with copying all .pdf and .docx files on all of the computer's drives to a single directory

#1 Post by Shaioki » 17 Dec 2016 14:28

I'm trying to copy all .pdf and .docx files on all drives to a single directory. Preferably retaining the original folder structures.
This is what I currently have:

Code: Select all

set dst=(DESTINATION DIRECTORY)
for /f "tokens=2 delims==" %%d in ('wmic logicaldisk get name /format:value') do (
    xcopy /C /Q /G /Y /E %%d\*.pdf %dst% >>nul
    xcopy /C /Q /G /Y /E %%d\*.docx %dst% >>nul
)


The FOR loop lists the drive letters of all drives on the computer.
The script has to be able to work for any computer, regardless of the number of drives. Any help would be greatly appreciated :)

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Help with copying all .pdf and .docx files on all of the computer's drives to a single directory

#2 Post by penpen » 18 Dec 2016 08:47

I would first create a list of files to copy in order to avoid an endless loop (untested and not full code):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion

>"filelist.txt" (
   for /f "tokens=2 delims=:=" %%d in ('wmic logicaldisk get name /format:value') do (
      for /R "%%d:\" %a in (*.pdf *.docx) do echo %%~fa
   )
)
:: @todo process filelist using "for /F"

endlocal
goto :eof
(Maybe you need 'push "%%d:\"' + 'popd' around the for/R loop.)


penpen

Post Reply